
mysql强制指定索引查询
mysql强制指定索引查询
写法如下:
select * from table_name force index (index_name) where conditions;
其中force index(索引名称),conditions是where条件
实验证明,有时候强制使用索引并不会达到提升速度的优势
explain分析
mysql> set profiling = on; //开启profiles Query OK, 0 rows affected mysql> explain select count(id) from user where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%'; +----+-------------+-------+------------+------+---------------------+------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------------+------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | user | NULL | ALL | PRIMARY,name,status | NULL | NULL | NULL | 5854042 | 40.99 | Using where | +----+-------------+-------+------------+------+---------------------+------+---------+------+---------+----------+-------------+ 1 row in set mysql> explain select count(id) from user force index(status) where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%'; +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------------------------------+ | 1 | SIMPLE | user | NULL | range | status | status | 1 | NULL | 4192541 | 3.7 | Using index condition; Using where; Using MRR | +----+-------------+-------+------------+-------+---------------+--------+---------+------+---------+----------+-----------------------------------------------+ 1 row in set
开始查询
mysql> select count(id) from user where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%'; +-----------+ | count(id) | +-----------+ | 1555437 | +-----------+ 1 row in set mysql> select count(id) from user force index(status) where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%' -> ; +-----------+ | count(id) | +-----------+ | 1555437 | +-----------+ 1 row in set
查看执行时间
mysql>show profiles; +----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+ | Query_ID | Duration | Query | +----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+ | 29 | 2.121844 | select count(id) from user where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%'| | 30 | 13.83298475 | select count(id) from user force index(status) where status in (1,2,3,4) and id>2500000 and name like '哈哈哈哈哈哈%'| +----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set
从2秒变到了13秒
所以当mysql发现不使用索引的时候更快,就直接全表扫描了