查一次left join没有走索引以及原因分析

 更新时间:2023年03月23日 09:19:23   作者:qq_20009015  
这篇文章主要介绍了查一次left join没有走索引以及原因分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

查一次left join没有走索引的原因

线上有个查询sql,原来是inner join 查询没有问题,后来应业务要求改成left join之后, 查询时间就暴涨了 需要长达24s

通过explain分析,发现订单表没有走索引 ,分析之后解决了,记录下来。

为了简洁起见,这里就将无关的查询字段都用*

具体sql如下

SELECT  * 
 from t_item_detail a
 left join t_order_detail d on a.order_code=d.order_code
 left join t_connection b on a.unique_code = b.funds_unique 
 left join t_capital_detail c on b.capital_unique = c.unique_code 
 where item_receipt_disbursement=1 and a.is_deleted=0
  and order_type_code=00901 group by a.unique_code LIMIT 10 

用explain命令分析如下

发现table d 的type为all, rows居然高达20万行 。

d对应的表为order_detail 表,type为all 说明并没有走索引。

这里首先看关联条件

from t_item_detail a
 left join t_order_detail d on a.order_code=d.order_code

该条件并无问题,然后这两张表的order_code字段是否加索引.

两张表的order_code字段均有索引。

其次再看, 如果两个字段或者两张表的编码不同,也会导致索引失效。

但是这两张表的编码和字段编码也均相同,因此也排除掉。

最后发现,

如果写成

 explain SELECT  * 
 from t_item_detail a
 left join t_order_detail d on a.order_code=d.order_code  and d.order_type_code=00901
 left join t_connection b on a.unique_code = b.funds_unique 
 left join t_capital_detail c on b.capital_unique = c.unique_code 
 where item_receipt_disbursement=1 and a.is_deleted=0
  group by a.unique_code LIMIT 10 

也就是将原来在where条件的order_type_code=00901 写到left join的条件后面

d的索引就生效了,所有的索引都生效了。

查询时间也从原来的24秒 变成了不到1秒。

这是为什么呢?

其实问题就出在这个 d.order_type_code=00901 这个条件上

当有这个条件时候

全文扫描

没有这个条件的时候

从sql的执行顺序来分析:

SELECT  * 
 from t_item_detail a
 left join t_order_detail d on a.order_code=d.order_code
 left join t_connection b on a.unique_code = b.funds_unique 
 left join t_capital_detail c on b.capital_unique = c.unique_code 
 where item_receipt_disbursement=1 and a.is_deleted=0
  and order_type_code=00901 group by a.unique_code LIMIT 10 

这里面的执行顺序为

  • 1.from
  • 2.on
  • 3.join
  • 4.where
  • 5.select
  • 6.group by
  • 7.limit

写的顺序:select … from… where… group by… having… order by… limit [offset,](rows)

执行顺序:from… where…group by… having… select … order by… limit

知道这个,我们再看这个sql

不走索引 有order_type_code条件

SELECT *
from t_item_detail a
left join t_order_detail d on a.order_code=d.order_code
left join t_connection b on a.unique_code = b.funds_unique
left join t_capital_detail c on b.capital_unique = c.unique_code
where item_receipt_disbursement=1 and a.is_deleted=0
and order_type_code=00901 group by a.unique_code LIMIT 10

走索引 没有order_type_code条件

SELECT *
from t_item_detail a
left join t_order_detail d on a.order_code=d.order_code
left join t_connection b on a.unique_code = b.funds_unique
left join t_capital_detail c on b.capital_unique = c.unique_code
where item_receipt_disbursement=1 and a.is_deleted=0
group by a.unique_code LIMIT 10

和走索引有没有order_type_code条件

SELECT *
from t_item_detail a
left join t_order_detail d on a.order_code=d.order_code and d.order_type_cod=‘00901'
left join t_connection b on a.unique_code = b.funds_unique
left join t_capital_detail c on b.capital_unique = c.unique_code
where item_receipt_disbursement=1 and a.is_deleted=0
group by a.unique_code LIMIT 10

会发现 在不走索引有order_type_code条件的那个sql中, 在执行到where的时候,需要去找到条件 order_type_code=00901 ,但是order_type_code这个字段没有索引,所以数据库就去对order_detail进行全表扫描。

因此解决方案

就是给order_type_code加上索引,或者给 left join on就加上条件order_type_code=xxx ,直接过滤掉

因此,谨记,大表查询的时候,where 的条件千万记得加上索引!!!!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • MySQL使用变量实现各种排序

    MySQL使用变量实现各种排序

    这篇文章主要介绍了MySQL使用变量实现各种排序,需要的朋友可以参考下
    2017-05-05
  • MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)

    MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)

    这篇文章主要介绍了MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)的解决方法,需要的朋友可以参考下
    2014-07-07
  • 浅析MySQL的WriteSet并行复制

    浅析MySQL的WriteSet并行复制

    这篇文章主要介绍了浅析MySQL的WriteSet并行复制的相关资料,帮助大家更好的理解和学习MySQL,感兴趣的朋友可以了解下
    2020-11-11
  • MySql逗号分割的字段数据分解为多行代码示例

    MySql逗号分割的字段数据分解为多行代码示例

    逗号分割的字符串可以作为分组数据的标识符,用于对数据进行分组和聚合操作,下面这篇文章主要给大家介绍了关于MySql逗号分割的字段数据分解为多行的相关资料,需要的朋友可以参考下
    2023-12-12
  • 基于mysql数据库的密码问题详解

    基于mysql数据库的密码问题详解

    本篇文章是对mysql数据库的密码问题进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • MySQL 5.7.17压缩版安装笔记

    MySQL 5.7.17压缩版安装笔记

    这篇文章主要介绍了MySQL 5.7.17压缩版安装笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Linux操作系统操作MySQL常用命令小结

    Linux操作系统操作MySQL常用命令小结

    本文给大家分享Linux操作系统操作MySQL常用命令小结,需要的朋友参考下吧
    2017-07-07
  • MySQL多表查询详解下

    MySQL多表查询详解下

    这篇文章主要介绍了MySQL多表查询详解下,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • mysql5.7.17安装配置图文教程

    mysql5.7.17安装配置图文教程

    这篇文章主要为大家详细介绍了mysql5.7.17安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 基于MySQL在磁盘上存储NULL值

    基于MySQL在磁盘上存储NULL值

    这篇文章主要介绍了基于MySQL在磁盘上存储NULL值,NULL值列表,一行数据里可能有的字段值是NULL,比如nickname字段,允许为NULL,存储时,如果没赋值,这字段值就是NULL,下文关于NULL值的相关资料,需要的小伙伴可以参考一下
    2022-02-02

最新评论