mysql> explain select * from thefts where user_id=191 and is_transfer_free =1;
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
| 1 | SIMPLE | thefts | ref | index_thefts_on_user_id_and_status,index_thefts_on_user_id_and_is_transfer_free | index_thefts_on_user_id_and_status | 5 | const | 1 | Using where |
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)mysql> explain select * from thefts where user_id=190 and is_transfer_free =1;
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
| 1 | SIMPLE | thefts | ref | index_thefts_on_user_id_and_status,index_thefts_on_user_id_and_is_transfer_free | index_thefts_on_user_id_and_is_transfer_free | 10 | const,const | 10 | Using where |
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
用的是mysql innodb
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
| 1 | SIMPLE | thefts | ref | index_thefts_on_user_id_and_status,index_thefts_on_user_id_and_is_transfer_free | index_thefts_on_user_id_and_status | 5 | const | 1 | Using where |
+----+-------------+--------+------+---------------------------------------------------------------------------------+------------------------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)mysql> explain select * from thefts where user_id=190 and is_transfer_free =1;
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
| 1 | SIMPLE | thefts | ref | index_thefts_on_user_id_and_status,index_thefts_on_user_id_and_is_transfer_free | index_thefts_on_user_id_and_is_transfer_free | 10 | const,const | 10 | Using where |
+----+-------------+--------+------+---------------------------------------------------------------------------------+----------------------------------------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
用的是mysql innodb
select * from (
select * from thefts where user_id=191) a
where is_transfer_free =1
mysql> explain select * from (select * from thefts where user_id=191) a where is_transfer_free=1;
+----+-------------+------------+--------+---------------------------------------------------------------------------------+------------------------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------------------------------------------------------------------------+------------------------------------+---------+------+------+-------------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | |
| 2 | DERIVED | thefts | ref | index_thefts_on_user_id_and_status,index_thefts_on_user_id_and_is_transfer_free | index_thefts_on_user_id_and_status | 5 | | 1 | Using where |
+----+-------------+------------+--------+---------------------------------------------------------------------------------+------------------------------------+---------+------+------+-------------+
2 rows in set (0.06 sec)
数据库的索引算法不一定就是没有bug的,我的那个表,相同的查询下,数据库如果用索引,是数据库不用索引的几百倍(保守的说,因为我从来都没有等到它不用索引,查询完的时候过),但是数据库依然会不定时的不用索引查询,这个我看了它的执行计划的