複合索引,排第一的效果
阿新 • • 發佈:2018-12-18
mysql> select count(*) from tf_user_index where age = 30; +----------+ | count(*) | +----------+ | 196477 | +----------+ 1 row in set (1.66 sec) mysql> select count(*) from tf_user_index where score = 30; +----------+ | count(*) | +----------+ | 126306 | +----------+ 1 row in set (0.03 sec) mysql> explain select count(*) from tf_user_index where age = 30; +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ | 1 | SIMPLE | tf_user_index | index | NULL | score_sex_age | 6 | NULL | 8962455 | Using where; Using index | +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ 1 row in set (0.00 sec) mysql> explain select count(*) from tf_user_index where score = 30; +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ | 1 | SIMPLE | tf_user_index | ref | score_sex_age | score_sex_age | 4 | const | 241442 | Using index | +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ 1 row in set (0.01 sec)
score排第一,查詢時間很快。age排第三,查詢要1秒多時間。
mysql> select count(*) from tf_user where score = 30; +----------+ | count(*) | +----------+ | 126306 | +----------+ 1 row in set (3.37 sec) mysql> select count(*) from tf_user where age = 30; +----------+ | count(*) | +----------+ | 196477 | +----------+ 1 row in set (2.44 sec)
沒有複合索引,都需要好幾秒時間。複合索引有一定的效果,但是單獨查詢時,對排第一的更有效。
調換一下順序,現在將age排第一。
mysql> select count(*) from tf_user_index where age = 30; +----------+ | count(*) | +----------+ | 196477 | +----------+ 1 row in set (0.05 sec) mysql> select count(*) from tf_user_index where score = 30; +----------+ | count(*) | +----------+ | 126306 | +----------+ 1 row in set (1.82 sec) mysql> explain select count(*) from tf_user_index where score = 30; +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ | 1 | SIMPLE | tf_user_index | index | NULL | age_score_sex | 6 | NULL | 8962455 | Using where; Using index | +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+ 1 row in set (0.00 sec) mysql> explain select count(*) from tf_user_index where age = 30; +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ | 1 | SIMPLE | tf_user_index | ref | age_score_sex | age_score_sex | 1 | const | 408456 | Using index | +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+ 1 row in set (0.00 sec)
排第一很重要。
mysql> explain select count(*) from tf_user_index where age = 30 and score =30;
+----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
| 1 | SIMPLE | tf_user_index | ref | age_score_sex | age_score_sex | 5 | const,const | 2467 | Using index |
+----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> select count(*) from tf_user_index where age = 30 and score =30;
+----------+
| count(*) |
+----------+
| 2468 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from tf_user_index where score =30 and age = 30;
+----------+
| count(*) |
+----------+
| 2468 |
+----------+
1 row in set (0.01 sec)
mysql> select count(*) from tf_user_index where score =30 and sex = 2;
+----------+
| count(*) |
+----------+
| 63031 |
+----------+
1 row in set (2.06 sec)
只要帶到age,查詢就很快。