关键词搜索

源码搜索 ×
×

避免在sql的where中对字段进行转换(函数转换/操作符转换/隐式转换)------索引会失效从而导致慢查询

发布2017-11-20浏览8466次

详情内容

        看例子:

 

  1. mysql> show create table tb_test;
  2. +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Table | Create Table |
  4. +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | tb_test | CREATE TABLE `tb_test` (
  6. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'test',
  7. `name` varchar(32) NOT NULL COMMENT 'test',
  8. `score` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'test',
  9. PRIMARY KEY (`id`),
  10. KEY `id_score` (`score`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=10005 DEFAULT CHARSET=utf8 COMMENT='测试表, 无实际作用' |
  12. +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  13. 1 row in set (0.00 sec)


        可见,  score是索引, 我们继续看:

 

 

  1. mysql> select * from tb_test where score = 1;
  2. +----+------+-------+
  3. | id | name | score |
  4. +----+------+-------+
  5. | 1 | n1 | 1 |
  6. +----+------+-------+
  7. 1 row in set (0.00 sec)
  8. mysql> select * from tb_test where score + 1 = 2;
  9. +----+------+-------+
  10. | id | name | score |
  11. +----+------+-------+
  12. | 1 | n1 | 1 |
  13. +----+------+-------+
  14. 1 row in set (0.00 sec)
  15. mysql>
  16. mysql>
  17. mysql> show profiles;
  18. +----------+------------+---------------------------------------------------+
  19. | Query_ID | Duration | Query |
  20. +----------+------------+---------------------------------------------------+
  21. | 10061 | 0.00076248 | select * from tb_test where score = 1 |
  22. | 10062 | 0.00316482 | select * from tb_test where score + 1 = 2 |
  23. +----------+------------+---------------------------------------------------+
  24. 15 rows in set (0.00 sec)


        可以看到, 时间差距较大, 为什么呢? 因为对sql的where字段进行转换(函数转换/操作符转换/隐式转换), 会导致索引失效。来explain看看:

 

 

  1. mysql> explain select * from tb_test where score = 1;
  2. +------+-------------+---------+------+---------------+----------+---------+-------+------+-------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +------+-------------+---------+------+---------------+----------+---------+-------+------+-------+
  5. | 1 | SIMPLE | tb_test | ref | id_score | id_score | 4 | const | 1 | |
  6. +------+-------------+---------+------+---------------+----------+---------+-------+------+-------+
  7. 1 row in set (0.00 sec)
  8. mysql> explain select * from tb_test where score + 1 = 2;
  9. +------+-------------+---------+------+---------------+------+---------+------+-------+-------------+
  10. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  11. +------+-------------+---------+------+---------------+------+---------+------+-------+-------------+
  12. | 1 | SIMPLE | tb_test | ALL | NULL | NULL | NULL | NULL | 10224 | Using where |
  13. +------+-------------+---------+------+---------------+------+---------+------+-------+-------------+
  14. 1 row in set (0.01 sec)


       看type, 很显然, 后者进行了全表搜索, 也就是索引失效了。 看看 key和rows也能获知结果。

 

 

 

       要避免, 杜绝。

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载