Mysql

在 Mysql innodb 表中插入非常慢

  • August 18, 2017

我可以在 myisam 表上每分鐘插入 200 萬次,但如果我更改為 innodb,則每分鐘只能插入 3000 次

CREATE TABLE myisam_table ( c1 int default NULL, c2 varchar(30) default NULL, c3 date default NULL) engine=myisam;
CREATE TABLE innodb_table ( c1 int default NULL, c2 varchar(30) default NULL, c3 date default NULL) engine=innodb;

CREATE PROCEDURE load_myisam_table()  
      begin  
   declare v int default 0;  
   while v < 10000000  
   do  
       insert into myisam_table  
       values (v,'testing','2017-01-01');  
        set v = v + 1;  
   end while;  
   end  


CREATE PROCEDURE load_innodb_table()  
      begin  
   declare v int default 0;  
   while v < 10000000  
   do  
       insert into innodb_table  
       values (v,'testing','2017-01-01');  
        set v = v + 1;  
   end while;  
   end  

結果:

mysql -e "select count(*) from asdf.innodb_table;" ; sleep 60;mysql -e "select count(*) from asdf.innodb_table;" ;
+----------+
| count(*) |
+----------+
|      125 |
+----------+
+----------+
| count(*) |
+----------+
|     2975 |
+----------+
mysql -e "select count(*) from asdf.myisam_table;" ; sleep 60;mysql -e "select count(*) from asdf.myisam_table;" ;
+----------+                                                                                     
| count(*) |                                                                                     
+----------+
|   134446 |
+----------+
+----------+
| count(*) |
+----------+
|  2225277 |
+----------+

當沒有事務啟動時,在InnoDB表上執行插入/更新查詢會明顯變慢。我已經執行了您列出的程序,結果load_innodb_table實際上比load_myisam_table. 執行load_innodb_table10m 插入實際上意味著您正在執行 10m 事務,因為每個插入都會隱式啟動一個事務。

引用自:https://dba.stackexchange.com/questions/183791