美文网首页
MySQL 一次加索引导致SQL执行更慢的情况

MySQL 一次加索引导致SQL执行更慢的情况

作者: Always_July | 来源:发表于2022-11-10 15:25 被阅读0次

环境说明

mysql 5.7.2
表数据条数 120万条,该表拥有102个字段,用于记录每天的订单信息的数据,有单量,收入金额,支出金额,机构等字段。

建表语句大致如下

CREATE TABLE `t` (
  `order_day` date DEFAULT NULL COMMENT '统计时间日',
  `join_order_num` bigint(20) DEFAULT NULL COMMENT '委托量',
  `channel_cost` decimal(12,2) DEFAULT NULL COMMENT '渠道商结算价',
  `customer_cost` decimal(12,2) DEFAULT NULL COMMENT '客户结算价',
  `provider_cost` decimal(12,2) DEFAULT NULL COMMENT '服务商采购价',
  `case_prov` varchar(20) DEFAULT NULL COMMENT '省',
  `case_city` varchar(32) DEFAULT NULL COMMENT '市',
  `case_area` varchar(32) DEFAULT NULL COMMENT '区',
  `company_id` bigint(20) DEFAULT NULL COMMENT '渠道商Id',
  `channel_name` varchar(32) DEFAULT NULL COMMENT '渠道商名称',
  `provider_id` bigint(20) DEFAULT NULL COMMENT '服务商Id',
   `provider_name` varchar(32) DEFAULT NULL COMMENT '服务商名称',
  `order_type` varchar(16) DEFAULT NULL COMMENT '订单类型',
  `service_name` varchar(16) DEFAULT NULL COMMENT '服务类型',
  `product_name` varchar(16) DEFAULT NULL COMMENT '产品类型'

  ... 其他字段省略
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=''



由于发现经常使用 order_day 字段查询,所以新建order_day索引。
添加索引SQL:

alter table t add index idx_order_day(order_day);

查询SQL如下,发现当统计时间范围过长的时候,查询耗时剧增

select 
-- sql_no_cache
       order_day
       ,sum(join_order_num) as join_order_num
       ,sum(ifnull(channel_cost,0) + ifnull(customer_cost,0)) as td_income_amount
       ,sum(ifnull(provider_cost,0)) as td_pay_amount
       ,sum(ifnull(channel_cost,0) + ifnull(channel_cost,0)) - sum(ifnull(provider_cost,0)) as gross_profit
from zbcf_analysis.t
 -- ignore index (idx_order_day)
where order_day>='2022-01-01'  and order_day <= '2022-11-11'
group by order_day ;

现象

不使用索引查询耗时 3-4s
使用索引查询耗时 11-12 s

explain结果

  • 不使用索引
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t All idx_order_day 1133492 11.11 Using where; Using temporary; Using filesort
  • 使用索引
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t index idx_order_day idx_order_day 4 1133492 50.00 Using where

用rows × filtered可获得和下一张表连接的行数。使用索引后,filtered 还变大了,这个索引没有达到快速过滤数据的效果,并且增加查询索引的开销。

将时间查询范围改为

order_day>='2022-10-01'  and order_day <= '2022-11-11'
  • 不使用索引
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t All idx_order_day 1133492 11.11 Using where; Using temporary; Using filesort
  • 使用索引
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t index idx_order_day idx_order_day 4 107470 100.00 Using index condition

可以看到 rows 只有 10万了。

不使用索引查询耗时 2.7 s
使用索引查询耗时 0.9s

总结

MySQL可能会选错索引,在使用索引达到不了快速筛选数据的目的时,直接全表扫描时更好的方案。

相关文章

  • MySQL 一次加索引导致SQL执行更慢的情况

    环境说明 mysql 5.7.2表数据条数 120万条,该表拥有102个字段,用于记录每天的订单信息的数据,有单量...

  • 为什么加了索引sql性能提高好几倍?

    在我们日常开发中,针对mysql的sql优化,最简单、最常用的方式就是为查询条件加索引。那么为什么加个索引,sql...

  • mysql学习-20180117

    [需要更新] mysql学习 mysql 5.6 官方版本说加索引时,不会锁表,但是表加索引时,依然会存在2种情况...

  • 联表查询的MySQL语句怎么加索引

    如题,怎么样对下面的sql语句加索引,才能提高其执行速度呢?请看下文分析。 1、在没有加索引的情况下,查询465条...

  • mysql诊断调优常用SQL语句

    在很多时候,我们需要通过SQL语句来查看MySQL执行SQL的情况,例如查看SQL执行队列,是否存在慢查询等等。 ...

  • 01.MySQL架构与SQL执行流程

    MySql笔记,笔记分为四个部分:1.MySQL架构与SQL执行流程2.MySQL索引原理与使用原则[https:...

  • laravel eloquent 强制mysql使用索引

    为什么需要强制索引? `数据库没有使用我们设想的索引进行sql查询,导致查询特别慢。` mysql强制索引查询语句...

  • MySQL索引知多少

    mysql索引 总结关于mysql的索引,查询优化,SQL技巧等 1 索引类型 B-Tree索引 Hash索引 ...

  • MySQL索引

    MySQL中有两种索引,分别是B-Tree索引和Hash索引 为什么要使用索引 因为在无索引的情况下,SQL语句会...

  • Mysql查询优化

    mysql的优化思路: 日志、执行计划、索引、缓存(查询缓存和索引缓存)、SQL优化、分区、分表、分库、修改数据库...

网友评论

      本文标题:MySQL 一次加索引导致SQL执行更慢的情况

      本文链接:https://www.haomeiwen.com/subject/kcmztdtx.html