1.環境信息
mysql-5.6.17-winx64mysql
2.新建兩張表,tb_big_data和tb_big_data2。tb_big_data數據1100000條,tb_big_data2數據2000條sql
mysql> select count(*) from tb_big_data; +----------+ | count(*) | +----------+ | 1100000 | +----------+ 1 row in set mysql> select count(*) from tb_big_data2; +----------+ | count(*) | +----------+ | 2000 | +----------+ 1 row in set
3.執行join查詢,查看執行join查詢且結果爲49000的查詢時間dom
mysql> select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2;
4.對字段增長普通索引和不加索引,查詢時間相差1000倍性能
mysql> show profiles; +----------+--------------+--------------------------------------------------------------------------------------------------------+ | Query_ID | Duration | Query | +----------+--------------+--------------------------------------------------------------------------------------------------------+ | | 9 | 135.77425125 | select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2 | | 11 | 4.96485125 | ALTER TABLE tb_big_data ADD INDEX index_name (random) | | 12 | 0.01095375 | describe tb_big_data | | 13 | 0.14014425 | select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2 | +----------+--------------+--------------------------------------------------------------------------------------------------------+
5.單列索引和多列索引
未建立索引,對兩個字段過濾查詢須要,結果爲2條數據耗時6.4s,建立多列索引(count,random),耗時0.03s,建立兩個單列索引,耗時0.08s。在對多個字段進行過濾查詢時,多列索引和單列索引的性能仍是不同的。code
mysql> show profiles; +----------+-------------+-------------------------------------------------------------+ | Query_ID | Duration | Query | +----------+-------------+-------------------------------------------------------------+ | 8 | 6.439019 | select count(*) from tb_big_data where count=6 and random=2 | | 9 | 44.570048 | create index index_name on tb_big_data(count,random) | | 10 | 0.0311755 | select count(*) from tb_big_data where count=6 and random=2 | | | 13 | 37.07460275 | create index index_name on tb_big_data(count) | | 15 | 39.00397825 | create index index_name2 on tb_big_data(random) | | 17 | 0.08649375 | select count(*) from tb_big_data where count=6 and random=2 | +----------+-------------+-------------------------------------------------------------+