Syntax “is true” won’t match index
在试用query_reviewer插件 优化程序时发现,sql查询语句语法使用 “is true” 不会使用索引。当使用 “= true” 时则使用索引。
mysql> show index from users;
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+
| users | 0 | PRIMARY | 1 | id | A | 601 | NULL | NULL | | BTREE | |
| users | 1 | index_users_on_saleman | 1 | saleman | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_profile_id | 1 | profile_id | A | 601 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_manager | 1 | manager | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_on_the_job | 1 | on_the_job | A | 4 | NULL | NULL | YES | BTREE | |
| users | 1 | index_users_on_service | 1 | service | A | 4 | NULL | NULL | YES | BTREE | |
+——-+————+—————————+————–+————-+———–+————-+———-+——–+——+————+———+
mysql> explain select * from users where saleman is true;
+—-+————-+——-+——+—————+——+———+——+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————+——+———+——+——+————-+
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 601 | Using where |
+—-+————-+——-+——+—————+——+———+——+——+————-+
1 row in set (0.00 sec)
mysql> explain select * from users where saleman = true;
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
| 1 | SIMPLE | users | ref | index_users_on_saleman | index_users_on_saleman | 2 | const | 11 | Using where |
+—-+————-+——-+——+————————+————————+———+——-+——+————-+
1 row in set (0.00 sec)