淺析MyISAM插入數據過程

環境:mysql 5.7.28 mysql

獲取鏈接idsql

mysql> show processlist;  

+----+------+-----------+------+---------+------+----------+------------------+  
| Id | User | Host | db | Command | Time | State | Info |  
+----+------+-----------+------+---------+------+----------+------------------+  
| 2 | root | localhost | NULL | Query | 0 | starting | show processlist |  
+----+------+-----------+------+---------+------+----------+------------------+

獲取到鏈接id是2async

根據鏈接id獲取線程idspa

mysql> select THREAD_ID, thd_id, conn_id, THREAD_OS_ID, name  from performance_schema.threads t, sys.processlist p   where p.thd_id = t.thread_id and conn_id > 0;  
+-----------+--------+---------+--------------+--------------------------------+  
| THREAD_ID | thd_id | conn_id | THREAD_OS_ID | name |  
+-----------+--------+---------+--------------+--------------------------------+  
| 26        | 26      | 1      | 5599         | thread/sql/compress_gtid_table |  
| 27        | 27       | 2     | 6717         | thread/sql/one_connection |  
+-----------+--------+---------+--------------+--------------------------------+

獲取到2號鏈接對應的線程id是6717線程

查看6717打開的全部文件描述符code

ls -alh  /proc/6717/fd

屏幕快照 2019-12-27 下午6.29.28.png

客戶端發送數據orm

insert into user2(name,age) values('a',10);

服務端strace抓到的數據blog

strace -T -tt  -s300   -p 6717
`18:07:59.673626 poll([{fd=4, events=POLLIN|POLLPRI}], 1, 28800000) = 1 ([{fd=4, revents=POLLIN}]) <6.400402>`

`18:08:06.074079 recvfrom(4,``"+\0\0\0"``, 4, MSG_DONTWAIT, NULL, NULL) = 4 <0.000142>`

`18:08:06.074319 gettimeofday({1577441286, 74342}, NULL) = 0 <0.000053>`

`18:08:06.074455 clock_gettime(CLOCK_REALTIME, {1577441286, 74472708}) = 0 <0.000052>`

`//一、讀取到客戶端數據`

`18:08:06.074547 recvfrom(4,``"\3insert into user2(name,age) values('a',10)"``, 43, MSG_DONTWAIT, NULL, NULL) = 43 <0.000047>`

`18:08:06.074673 gettimeofday({1577441286, 74688}, NULL) = 0 <0.000013>`

`18:08:06.074857 clock_gettime(CLOCK_REALTIME, {1577441286, 74875405}) = 0 <0.000011>`

`18:08:06.074938 clock_gettime(CLOCK_REALTIME, {1577441286, 74954987}) = 0 <0.000010>`

`18:08:06.075059 gettimeofday({1577441286, 75076}, NULL) = 0 <0.000011>`

`//二、寫真實數據  51 -> /var/lib/mysql/test/user2.MYD`

`18:08:06.118474 pwrite64(51,``"\3\0\20\0\0\374\2\33\267\0\0\0\0\0\1a\n\0\0\0"``, 20, 480000020) = 20 <0.015135>`

`//三、寫binlog數據  20 -> /var/lib/mysql/mysql-bin.000001`

`18:08:06.134313 write(20,``"\6\330\5^\"\1\0\0\0A\0\0\0\333\0\0\0\0\0\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\2\0\0\0\0\0\0\0\0\1\0\0\0\0\0\0\0CJ\7#\6\330\5^\2\1\0\0\0O\0\0\0*\1\0\0\10\0\2\0\0\0\0\0\0\0\4\0\0!\0\0\0\0\0\0\1 \0\240U\0\0\0\0\6\3std\4!\0!\0\10\0\f\1test\0test\0BEGIN\"d\205\337\6\330\5^\5\1\0\0\0 \0\0\0J\1\0\0\0\0\2\2\33\267\0\0\0\0\0n\310&'\6\330\5^\2\1\0\0\0t\0\0\0\276\1\0\0\0\0\2\0\0\0\0\0\0\0\4\0\0!\0\0\0\0\0\0\1 \0\240U\0\0\0\0\6\3std\4!\0!\0\10\0\f\1test\0test\0insert into user2(name,age) values('a',10)\232\306K\330\6\330\5^\2\1\0\0"``..., 372) = 372 <0.000048>`

`//四、刷盤binlog數據`

`18:08:06.134413 fdatasync(20)           = 0 <0.007731>`

`//五、寫索引數據    50 -> /var/lib/mysql/test/user2.MYI`

`18:08:06.134414 pwrite64(50,``"\0\2\1"``, 3, 24) = 3 <0.000075>`

`...`

`18:08:06.142744 gettimeofday({1577441286, 142789}, NULL) = 0 <0.000040>`

`//六、給客戶端返回結果`

`18:08:06.142851 sendto(4,``"\n\0\0\1\0\1\375\2\33\267\2\0\0\0"``, 14, MSG_DONTWAIT, NULL, 0) = 14 <0.000232>`

`18:08:06.143147 clock_gettime(CLOCK_REALTIME, {1577441286, 143180187}) = 0 <0.000039>`

`18:08:06.143247 gettimeofday({1577441286, 143287}, NULL) = 0 <0.000036>`

`18:08:06.143352 gettimeofday({1577441286, 143395}, NULL) = 0 <0.000038>`

`18:08:06.143438 recvfrom(4, 0x35b61f0, 4, MSG_DONTWAIT, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable) <0.000026>`

`//七、等待客戶端寫入數據   POLLIN  普通或優先級帶數據可讀  POLLPRI 高優先級數據可讀`

`18:08:06.143509 poll([{fd=4, events=POLLIN|POLLPRI}], 1, 28800000`

總結:索引

MyISAM 引擎在一個線程裏完成了數據的寫入,主要流程:ssl

一、讀取到客戶端數據

二、寫真實數據

三、寫binlog數據[若是沒開啓binlog沒有這一步]

四、刷盤binlog數據

五、寫索引數據(沒有建索引也會寫索引數據,不知道是否是會像innodb同樣會默認加主鍵索引

六、給客戶端返回結果

歡迎你們斧正和補充。

相關文章
相關標籤/搜索