單個索引與復合索引


單個索引與復合索引

在表中的多個字段組合上創建的索引,只有在查詢條件中使用了這些字段的左邊字段時,索引才會被使用,使用組合索引時遵循最左前綴集合。
如果我們創建了(username,sex,age)的復合索引,那么其實相當於創建了:
(username,sex,age),(username,sex)、(username,age)、(username)四個索引,這被稱為最佳左前綴特性。
因此我們在創建復合索引時應該將最常用作限制條件的列放在最左邊,依次遞減。
例:

select * from test where username='11'
select * from test where username='11' and sex=1
select * from test where username='11' and age=20
select * from test where username='11' and sex=1 and age=20

以上有索引。

select * from test where sex=11
select * from test where sex=1 and age=20

以上無索引。

添加單個索引

ALTER TABLE `tf_user_index` ADD INDEX(`username`);
ALTER TABLE `tf_user_index` ADD INDEX(`sex`);
ALTER TABLE `tf_user_index` ADD INDEX(`age`);

添加聯合索引

ALTER TABLE `tf_user_index` ADD INDEX `username_sex_age` (`username`, `sex`, `age`);
ALTER TABLE `tf_user_index` ADD INDEX `sex_age` ( `sex`, `age`);

mysql> explain select count(*) from tf_user_index where sex='1';
+----+-------------+---------------+------+---------------+------+---------+-------+--------+-------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref   | rows   | Extra       |
+----+-------------+---------------+------+---------------+------+---------+-------+--------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | sex,sex_age   | sex  | 1       | const | 100071 | Using index |
+----+-------------+---------------+------+---------------+------+---------+-------+--------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username='user1';
+----+-------------+---------------+------+---------------------------+----------+---------+-------+------+-----------------------+
| id | select_type | table         | type | possible_keys             | key      | key_len | ref   | rows | Extra                 |
+----+-------------+---------------+------+---------------------------+----------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,username_sex_age | username | 152     | const |    1 | Using index condition |
+----+-------------+---------------+------+---------------------------+----------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where age='11';
+----+-------------+---------------+------+---------------+------+---------+-------+------+-------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref   | rows | Extra |
+----+-------------+---------------+------+---------------+------+---------+-------+------+-------+
|  1 | SIMPLE      | tf_user_index | ref  | age           | age  | 1       | const | 3911 | NULL  |
+----+-------------+---------------+------+---------------+------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username='user1' and sex='1';
+----+-------------+---------------+------+---------------------------------------+----------+---------+-------+------+------------------------------------+
| id | select_type | table         | type | possible_keys                         | key      | key_len | ref   | rows | Extra                              |
+----+-------------+---------------+------+---------------------------------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,username_sex_age,sex_age | username | 152     | const |    1 | Using index condition; Using where |
+----+-------------+---------------+------+---------------------------------------+----------+---------+-------+------+------------------------------------+
1 row in set (0.00 sec)

mysql> mysql> explain select * from tf_user_index where username='user1' and age='18';
+----+-------------+---------------+------+-------------------------------+----------+---------+-------+------+------------------------------------+
| id | select_type | table         | type | possible_keys                 | key      | key_len | ref   | rows | Extra                              |
+----+-------------+---------------+------+-------------------------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,age,username_sex_age | username | 152     | const |    1 | Using index condition; Using where |
+----+-------------+---------------+------+-------------------------------+----------+---------+-------+------+------------------------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username='user1' and sex='1'  and age='18';
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
| id | select_type | table         | type | possible_keys                             | key      | key_len | ref   | rows | Extra                              |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,age,username_sex_age,sex_age | username | 152     | const |    1 | Using index condition; Using where |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where  sex='1'  and age='18' and username='user1';
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
| id | select_type | table         | type | possible_keys                             | key      | key_len | ref   | rows | Extra                              |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,age,username_sex_age,sex_age | username | 152     | const |    1 | Using index condition; Using where |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where  sex='1'  and age='18';
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
| id | select_type | table         | type | possible_keys   | key     | key_len | ref         | rows | Extra |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
|  1 | SIMPLE      | tf_user_index | ref  | sex,age,sex_age | sex_age | 2       | const,const | 1962 | NULL  |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where  age='18' and sex='1';
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
| id | select_type | table         | type | possible_keys   | key     | key_len | ref         | rows | Extra |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
|  1 | SIMPLE      | tf_user_index | ref  | sex,age,sex_age | sex_age | 2       | const,const | 1962 | NULL  |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------+
1 row in set (0.01 sec)

mysql> explain select * from tf_user_index where username like '%user1' and sex='1'  and age='18';
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------------+
| id | select_type | table         | type | possible_keys   | key     | key_len | ref         | rows | Extra       |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | sex,age,sex_age | sex_age | 2       | const,const | 1962 | Using where |
+----+-------------+---------------+------+-----------------+---------+---------+-------------+------+-------------+
1 row in set (0.01 sec)

mysql> explain select * from tf_user_index where username like 'user1%' and sex='1'  and age='18';
+----+-------------+---------------+------+-------------------------------------------+---------+---------+-------------+------+-------------+
| id | select_type | table         | type | possible_keys                             | key     | key_len | ref         | rows | Extra       |
+----+-------------+---------------+------+-------------------------------------------+---------+---------+-------------+------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,age,username_sex_age,sex_age | sex_age | 2       | const,const | 1962 | Using where |
+----+-------------+---------------+------+-------------------------------------------+---------+---------+-------------+------+-------------+
1 row in set (0.01 sec)

mysql> explain select * from tf_user_index where username like 'user1%' and sex like '1%'  and age='18';
+----+-------------+---------------+------+-------------------------------------------+------+---------+-------+------+-------------+
| id | select_type | table         | type | possible_keys                             | key  | key_len | ref   | rows | Extra       |
+----+-------------+---------------+------+-------------------------------------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,age,username_sex_age,sex_age | age  | 1       | const | 3896 | Using where |
+----+-------------+---------------+------+-------------------------------------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username like 'user1%' and sex like '1%'  and age like '18%';
+----+-------------+---------------+-------+-------------------------------------------+------------------+---------+------+--------+--------------------------+
| id | select_type | table         | type  | possible_keys                             | key              | key_len | ref  | rows   | Extra                    |
+----+-------------+---------------+-------+-------------------------------------------+------------------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | tf_user_index | range | username,sex,age,username_sex_age,sex_age | username_sex_age | 152     | NULL | 100071 | Using where; Using index |
+----+-------------+---------------+-------+-------------------------------------------+------------------+---------+------+--------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username='user1' and sex like '1%'  and age like '18%';
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
| id | select_type | table         | type | possible_keys                             | key      | key_len | ref   | rows | Extra                              |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
|  1 | SIMPLE      | tf_user_index | ref  | username,sex,age,username_sex_age,sex_age | username | 152     | const |    1 | Using index condition; Using where |
+----+-------------+---------------+------+-------------------------------------------+----------+---------+-------+------+------------------------------------+
1 row in set (0.00 sec)

用了%號,索引就會失效。
經過測試,加索引,速度會快一些。

mysql> explain select * from tf_user_index where username='user1' or sex='2';
+----+-------------+---------------+-------------+---------------------------------------+--------------+---------+------+--------+----------------------------------------+
| id | select_type | table         | type        | possible_keys                         | key          | key_len | ref  | rows   | Extra                                  |
+----+-------------+---------------+-------------+---------------------------------------+--------------+---------+------+--------+----------------------------------------+
|  1 | SIMPLE      | tf_user_index | index_merge | username,sex,username_sex_age,sex_age | username,sex | 152,1   | NULL | 100072 | Using union(username,sex); Using where |
+----+-------------+---------------+-------------+---------------------------------------+--------------+---------+------+--------+----------------------------------------+
1 row in set (0.00 sec)

mysql> explain select * from tf_user_index where username='user1' or (sex='2' and age='18');
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
| id | select_type | table         | type        | possible_keys                             | key              | key_len | ref  | rows | Extra                                      |
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
|  1 | SIMPLE      | tf_user_index | index_merge | username,sex,age,username_sex_age,sex_age | username,sex_age | 152,2   | NULL | 1934 | Using union(username,sex_age); Using where |
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
1 row in set (0.00 sec)

增加一個score字段之后。

mysql> explain select * from tf_user_index where username='user1' or (sex='2' and age='18' and score=60);
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
| id | select_type | table         | type        | possible_keys                             | key              | key_len | ref  | rows | Extra                                      |
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
|  1 | SIMPLE      | tf_user_index | index_merge | username,sex,age,username_sex_age,sex_age | username,sex_age | 152,2   | NULL | 1934 | Using union(username,sex_age); Using where |
+----+-------------+---------------+-------------+-------------------------------------------+------------------+---------+------+------+--------------------------------------------+
1 row in set (0.00 sec)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM