mysql 再查询数据时,使用where 条件的索引列 若是使用通配符来匹配数据的话,如果是这样的:
mysql> EXPLAIN
select * from np_order_lyz lzy where lzy.order_id like '29628%'
-> ;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | lzy | ALL | order_id | NULL | NULL | NULL | 45241 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set
这样的能使用到索引;
如果通配符是放在前面的,则不会使用索引 如
mysql> EXPLAIN
select * from np_order_lyz lzy where lzy.order_id like '%29628';
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | lzy | ALL | NULL | NULL | NULL | NULL | 45241 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set
如果你经常需要以通配符开头查询的,常用的方法是在数据库中保存需要查询值的反序值 reverse 字段 like reverse '%.com' 就可以使用索引
mysql> EXPLAIN
select * from np_order_lyz lzy where lzy.order_id like '29628%'
-> ;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | lzy | ALL | order_id | NULL | NULL | NULL | 45241 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set
这样的能使用到索引;
如果通配符是放在前面的,则不会使用索引 如
mysql> EXPLAIN
select * from np_order_lyz lzy where lzy.order_id like '%29628';
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | lzy | ALL | NULL | NULL | NULL | NULL | 45241 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set
如果你经常需要以通配符开头查询的,常用的方法是在数据库中保存需要查询值的反序值 reverse 字段 like reverse '%.com' 就可以使用索引
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31369373/viewspace-2121888/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31369373/viewspace-2121888/