复合索引,排第一的效果

Posted jiqing9006

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复合索引,排第一的效果相关的知识,希望对你有一定的参考价值。

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,查询就很快。

联合索引中,第一个位置很重要。

以上是关于复合索引,排第一的效果的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 复合索引--转载

Mysqk的四种key

从不使用以 DATETIME 作为复合键第一部分的主键索引

mysql 复合 唯一 索引

mysql 中的外键key值的详解

mysql数据库中标的key的含义