mysql intersect 使用方法_MySQL查询优化一例——也说说 Using intersect

生成上面有一条sql查询很慢,需要7到8秒左右,简化之后的sql如下所示:

SELECT

mingxi.*

FROM

(

SELECT

btjc01.id,

department.`name` AS btjc01011,

btjc01011 AS obj,

btjc01.inibeginmonth AS beginYearMonth,

lsbtsj.btje AS btjc01030,

CASE

WHEN btjc01.inibeginmonth ='2019-10' THEN

'是'

ELSE

'否'

END AS isadd

FROM

btjc01

INNER JOIN department ON department.Id = btjc01.btjc01038

INNER JOIN lsbtsj ON lsbtsj.btrid = btjc01.Id

WHERE

btjc01.btjc01037 = '3'

AND btjc01046 ='江西省'

AND btjc01047 ='九江市'

AND btjc01048 ='修水县'

AND lsbtsj.btqsyf <='2019-10'

AND lsbtsj.btjzyf >='2019-10'

AND beginYearMonth != inibeginmonth

UNION ALL

SELECT

btjc01.id,

department.`name` AS btjc01011,

btjc01011 AS obj,

btjc01.beginYearMonth,

btjc01.btjc01030,

CASE

WHEN btjc01.inibeginmonth ='2019-10' THEN

'是'

ELSE

'否'

END AS isadd

FROM

btjc01

INNER JOIN department ON department.Id = btjc01.btjc01038

INNER JOIN btgg03 ON btgg03.Id = btjc01.btjc01040

WHERE

btjc01.btjc01037 = '3'

AND btjc01046 ='江西省'

AND btjc01047 ='九江市'

AND btjc01048 ='修水县'

AND (beginYearMonth <='2019-10')

) AS mingxi

LIMIT 100,200;

执行计划如下所示:

可以看到 执行计划里面出现了 Using intersect,btjc01046 btjc01048 btjc01037 btjc01047 多个索引来进行数据的过滤。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值