1. 程式人生 > >讓天下沒有難用的資料庫 » mysql給同一個表新增多個索引的測試

讓天下沒有難用的資料庫 » mysql給同一個表新增多個索引的測試

分別給xc表新增ind_name和ind_status的索引:

[email protected] 11:44:13>create index ind_name on xc(name);
Query OK, 6815744 rows affected (1 min 43.75 sec)
Records: 6815744  Duplicates: 0  Warnings: 0

[email protected] 01:53:31>create index ind_status on xc(status);
Query OK, 6815744 rows affected (2 min 4.26 sec)
Records: 6815744  Duplicates: 0  Warnings: 0

[email protected] 11:15:47>alter table xc add index ind_name(name);
Query OK, 6815744 rows affected (1 min 44.40 sec)
Records: 6815744  Duplicates: 0  Warnings: 0

[email protected] 11:20:01>alter table xc add index ind_status(status);
Query OK, 6815744 rows affected (2 min 11.50 sec)
Records: 6815744  Duplicates: 0  Warnings: 0
共:3 min 55.90 sec

將兩個ddl語句合併起來的新增或刪除索引:

[email protected] 11:32:18>alter table xc add index ind_name(name),add index ind_status(status);
Query OK, 6815744 rows affected (1 min 58.07 sec)
Records: 6815744  Duplicates: 0  Warnings: 0

[email protected] 11:40:34>alter table xc drop index ind_name,drop index ind_status;
Query OK, 6815744 rows affected (1 min 28.48 sec)
Records: 6815744  Duplicates: 0  Warnings: 0
可以看到在新增或者刪除secondary index的時候,合併為一個子句的和分開寫的ddl語句的執行時間差不多為兩倍,原因在於add index的時候,innodb掃描原表,在記憶體或者臨時檔案中對行進行排序,合起來的ddl語句只要對原表掃描一次完成操作,分開寫的ddl語句需要完成兩次掃描操作。在對大表進行索引的新增和刪除的時候,這種方法屢試不爽。