前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为应用选择和创建最佳索引,加速数据读取 转

为应用选择和创建最佳索引,加速数据读取 转

作者头像
wuweixiang
发布2018-08-14 14:33:15
5820
发布2018-08-14 14:33:15
举报
文章被收录于专栏:吴伟祥吴伟祥

在工作之中,由于SQL问题导致的数据库故障层出不穷,索引问题是SQL问题中出现频率最高的,常见的索引问题包括:无索引,隐式转换,索引创建不合理。

当数据库中出现访问表的SQL没创建索引导致全表扫描,如果表的数据量很大扫描大量的数据,执行效率过慢,占用数据库连接,连接数堆积很快达到数据库的最大连接数设置,新的应用请求将会被拒绝导致故障发生。

隐式转换是指SQL查询条件中的传入值与对应字段的数据定义不一致导致索引无法使用。常见隐式转换如字段的表结构定义为字符类型,但SQL传入值为数字;或者是字段定义collation为区分大小写,在多表关联的场景下,其表的关联字段大小写敏感定义各不相同。隐式转换会导致索引无法使用,进而出现上述慢SQL堆积数据库连接数跑满的情况。

索引使用策略及优化

创建索引

  • 在经常查询而不经常增删改操作的字段加索引。
  • order by与group by后应直接使用字段,而且字段应该是索引字段。
  • 一个表上的索引不应该超过6个。
  • 索引字段的长度固定,且长度较短。
  • 索引字段重复不能过多。
  • 在过滤性高的字段上加索引。

使用索引注意事项

  • 使用like关键字时,前置%会导致索引失效。
  • 使用null值会被自动从索引中排除,索引一般不会建立在有空值的列上。
  • 使用or关键字时,or左右字段如果存在一个没有索引,有索引字段也会失效。
  • 使用!=操作符时,将放弃使用索引。因为范围不确定,使用索引效率不高,会被引擎自动改为全表扫描。
  • 不要在索引字段进行运算。
  • 在使用复合索引时,最左前缀原则,查询时必须使用索引的第一个字段,否则索引失效;并且应尽量让字段顺序与索引顺序一致。
  • 避免隐式转换,定义的数据类型与传入的数据类型保持一致。

无索引案例

无索引案例一

  1. 查看表结构。
    1. mysql> show create table customers;
    2. CREATE TABLE `customers` (
    3. `cust_id` int(11) NOT NULL AUTO_INCREMENT,
    4. `cust_name` char(50) NOT NULL,
    5. `cust_address` char(50) DEFAULT NULL,
    6. `cust_city` char(50) DEFAULT NULL,
    7. `cust_state` char(5) DEFAULT NULL,
    8. `cust_zip` char(10) DEFAULT NULL,
    9. `cust_country` char(50) DEFAULT NULL,
    10. `cust_contact` char(50) DEFAULT NULL,
    11. `cust_email` char(255) DEFAULT NULL,
    12. PRIMARY KEY (`cust_id`),
    13. ) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=utf8
  2. 执行语句。
    1. mysql> select * from customers where cust_zip = '44444' limit 0,1 \G;
  3. 执行计划。
    1. mysql> explain select * from customers where cust_zip = '44444' limit 0,1 \G;
    2. id: 1
    3. select_type: SIMPLE
    4. table: customers
    5. type: ALL
    6. possible_keys: NULL
    7. key: NULL
    8. key_len: NULL
    9. ref: NULL
    10. rows: 505560
    11. Extra: Using where

    执行计划看到type为ALL,是全表扫描,每次执行需要扫描505560行数据,这是非常消耗性能的,那么下面将介绍优化方式。

  4. 添加索引。
    1. mysql> alter table customers add index idx_cus(cust_zip);
  5. 执行计划。
    1. mysql> explain select * from customers where cust_zip = '44444' limit 0,1 \G;
    2. id: 1
    3. select_type: SIMPLE
    4. table: customers
    5. type: ref
    6. possible_keys: idx_cus
    7. key: idx_cus
    8. key_len: 31
    9. ref: const
    10. rows: 4555
    11. Extra: Using index condition

    执行计划看到type为ref,基于索引的等值查询,或者表间等值连接。

无索引案例二

  1. 表结构同上案例相同,执行语句。
    1. mysql> select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name;
  2. 执行计划。
    1. mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
    2. id: 1
    3. select_type: SIMPLE
    4. table: customers
    5. type: ALL
    6. possible_keys: NULL
    7. key: NULL
    8. key_len: NULL
    9. ref: NULL
    10. rows: 505560
    11. Extra: Using filesort
  3. 添加索引。
    1. mysql> alter table customers add index idx_cu_zip_name(cust_zip,cust_name);
  4. 执行计划。
    1. mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
    2. id: 1
    3. select_type: SIMPLE
    4. table: customers
    5. type: ref
    6. possible_keys: idx_cu_zip_name
    7. key: idx_cu_zip_name
    8. key_len: 31
    9. ref: const
    10. rows: 4555
    11. Extra: Using where; Using index

    order by使用字段,而且字段应该是索引字段。

隐式转换案例

隐式转换案例一

代码语言:javascript
复制
  1. mysql> explain select * from customers where cust_zip = 44444 limit 0,1 \G;
代码语言:javascript
复制
  1. id: 1
  2. select_type: SIMPLE
  3. table: customers
  4. type: ALL
  5. possible_keys: idx_cus
  6. key: NULL
  7. key_len: NULL
  8. ref: NULL
  9. rows: 505560
  10. Extra: Using where
代码语言:javascript
复制
  1. mysql> show warnings;
  2. Warning: Cannot use range access on index 'idx_cus' due to type or collation conversion on field 'cust_zip'

上述案例中由于表结构定义cust_zip字段是字符串数据类型,而应用传入的是数字,导致了隐式转换,无法使用索引。

解决方案:
  1. 将cust_zip字段修改为数字数据类型。
  2. 将应用中传入的字符类型改为数据类型。

隐式转换案例二

  1. 查看表结构。
    1. mysql> show create table customers1;
    2. CREATE TABLE `customers1` (
    3. `cust_id` varchar(10) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
    4. `cust_name` char(50) NOT NULL,
    5. KEY `idx_cu_id` (`cust_id`)
    6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    7. mysql> show create table customers2;
    8. CREATE TABLE `customers2` (
    9. `cust_id` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
    10. `cust_name` char(50) NOT NULL,
    11. KEY `idx_cu_id` (`cust_id`)
    12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  2. 执行语句。
    1. mysql> select customers1.* from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x';
  3. 执行计划。
    1. mysql> explain select customers1.* from customers2 left join customers1 on customers1.cust_id=customers2.cust_id where customers2.cust_id='x'\G;
    2. *************************** 1. row ***************************
    3. id: 1
    4. select_type: SIMPLE
    5. table: customers2
    6. type: ref
    7. possible_keys: idx_cu_id
    8. key: idx_cu_id
    9. key_len: 33
    10. ref: const
    11. rows: 1
    12. Extra: Using where; Using index
    13. *************************** 2. row ***************************
    14. id: 1
    15. select_type: SIMPLE
    16. table: customers1
    17. type: ALL
    18. possible_keys: NULL
    19. key: NULL
    20. key_len: NULL
    21. ref: NULL
    22. rows: 1
    23. Extra: Using where; Using join buffer (Block Nested Loop)
  4. 修改COLLATE。
    1. mysql> alter table customers1 modify column cust_id varchar(10) COLLATE utf8_bin ;
  5. 执行计划。
    1. mysql> explain select cust_id,cust_name,cust_zip from customers where cust_zip = '42222'order by cust_zip,cust_name\G;
    2. id: 1
    3. select_type: SIMPLE
    4. table: customers2
    5. type: ref
    6. possible_keys: idx_cu_id
    7. key: idx_cu_id
    8. key_len: 33
    9. ref: const
    10. rows: 1
    11. Extra: Using where; Using index
    12. id: 1
    13. select_type: SIMPLE
    14. table: customers1
    15. type: ref
    16. possible_keys: idx_cu_id
    17. key: idx_cu_id
    18. key_len: 33
    19. ref: const
    20. rows: 1
    21. Extra: Using where

    字段的COLLATE一致后执行计划使用到了索引,所以一定要注意表字段的collate属性的定义保持一致。

总结

在使用索引时,我们可以通过explain查看SQL的执行计划,判断是否使用了索引以及发生了隐式转换,创建合适的索引。索引太复杂,创建需谨慎。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 索引使用策略及优化
    • 创建索引
      • 使用索引注意事项
      • 无索引案例
        • 无索引案例一
          • 无索引案例二
          • 隐式转换案例
            • 隐式转换案例一
              • 解决方案:
            • 隐式转换案例二
            • 总结
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档