索引优化实战二

短命女 2022-09-12 02:53 255阅读 0赞

一 不要在索引列上做任何计算

不在索引列上做任何操作(计算、函数、(自动 or 手动)类型转换),会导致索引失效而转向全表扫描。

二 实战

  1. # 等号左边无计算,索引生效
  2. mysql> explain select * from staffs where name = 'July';
  3. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  4. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  5. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  6. | 1 | SIMPLE | staffs | NULL | ref | index_staffs_nameAgePos | index_staffs_nameAgePos | 74 | const | 1 | 100.00 | NULL |
  7. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  8. 1 row in set, 1 warning (0.00 sec)
  9. # 使用函数,索引失效
  10. mysql> explain select * from staffs where left(name,4) = 'July';
  11. +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
  12. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  13. +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
  14. | 1 | SIMPLE | staffs | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using where |
  15. +----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
  16. 1 row in set, 1 warning (0.00 sec)
  17. # 字符串加单引号,能查出结果
  18. mysql> select * from staffs where name='2000';
  19. +----+------+-----+-----+---------------------+
  20. | id | name | age | pos | add_time |
  21. +----+------+-----+-----+---------------------+
  22. | 3 | 2000 | 23 | dev | 2021-09-19 09:15:14 |
  23. +----+------+-----+-----+---------------------+
  24. 1 row in set (0.00 sec)
  25. # 字符串不加单引号,也能查出结果
  26. mysql> select * from staffs where name=2000;
  27. +----+------+-----+-----+---------------------+
  28. | id | name | age | pos | add_time |
  29. +----+------+-----+-----+---------------------+
  30. | 3 | 2000 | 23 | dev | 2021-09-19 09:15:14 |
  31. +----+------+-----+-----+---------------------+
  32. 1 row in set, 1 warning (0.00 sec)
  33. # 字符串加单引号,索引生效
  34. mysql> explain select * from staffs where name='2000';
  35. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  36. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  37. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  38. | 1 | SIMPLE | staffs | NULL | ref | index_staffs_nameAgePos | index_staffs_nameAgePos | 74 | const | 1 | 100.00 | NULL |
  39. +----+-------------+--------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
  40. 1 row in set, 1 warning (0.00 sec)
  41. # 字符串不加单引号,索引失效:字符串不加单引号,会在 name 列上做一次转换,也就是会自动进行一次类型转换
  42. mysql> explain select * from staffs where name=2000;
  43. +----+-------------+--------+------------+------+-------------------------+------+---------+------+------+----------+-------------+
  44. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  45. +----+-------------+--------+------------+------+-------------------------+------+---------+------+------+----------+-------------+
  46. | 1 | SIMPLE | staffs | NULL | ALL | index_staffs_nameAgePos | NULL | NULL | NULL | 3 | 33.33 | Using where |
  47. +----+-------------+--------+------------+------+-------------------------+------+---------+------+------+----------+-------------+
  48. 1 row in set, 3 warnings (0.00 sec)

三 结论

等号左边无计算。

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 索引优化()

    在上一篇[《索引优化(一)》][Link 1]的文章中,已经介绍了联合索引、索引下推、排序分组和文件排序的优化策略,还介绍了如果通过`trace`工具来查看MySQL选择执行方

    相关 索引优化实战

     一  不要在索引列上做任何计算 不在索引列上做任何操作(计算、函数、(自动 or 手动)类型转换),会导致索引失效而转向全表扫描。 二 实战 等号左边