温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

left join没有走索引的原因是什么及怎么解决

发布时间:2023-05-11 16:21:49 来源:亿速云 阅读:257 作者:iii 栏目:开发技术

本篇内容主要讲解“left join没有走索引的原因是什么及怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“left join没有走索引的原因是什么及怎么解决”吧!

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

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

left join没有走索引的原因是什么及怎么解决

通过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命令分析如下

left join没有走索引的原因是什么及怎么解决

发现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字段是否加索引.

left join没有走索引的原因是什么及怎么解决

left join没有走索引的原因是什么及怎么解决

两张表的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的条件后面

left join没有走索引的原因是什么及怎么解决

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

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

left join没有走索引的原因是什么及怎么解决

这是为什么呢?

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

当有这个条件时候

left join没有走索引的原因是什么及怎么解决

全文扫描

没有这个条件的时候

left join没有走索引的原因是什么及怎么解决

从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 的条件千万记得加上索引!!!!

到此,相信大家对“left join没有走索引的原因是什么及怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI