mysql 查询不出结果_mysql 执行查询SQL 一直执行不出结果

今天执行一个mysql 语句, 一直在 执行,执行了5分钟了,还是没有出来结果。

# 每个组织下包括 同级或者下级的 注册店铺数

# 查询 历史每一天的每个组织下的当天存在的门店数

select

dt.`time` startDate,

o.brand_id as brandId,

o.id AS orgId,

count(ogstore.id) as totalStore

from om_organization o

inner join view_om_org_tree ot on ot.parent_id=o.id

left join da_dim_time dt on dt.dim_type='DAY' and dt.`time`>='2018-10-30' and dt.`time` < CURDATE()

INNER JOIN (

select orge.id,orge.create_time as cdate from om_organization orge where

orge.org_type='Store' and orge.removed=0

) as ogstore on ogstore.id=ot.id and ogstore.cdate<=dt.`time`

where 1=1

and o.removed=0

group by o.id, dt.`time`

以上SQL ,看起来逻辑也挺清晰的,而且也不算长SQL了。但是 执行起来 很久没有结果。

无奈只能中断查询了, 试过加了索引发现还是没有用。 查询执行性能如下

1SIMPLEdtrangeidx_time,idx_time2,idx_typeidx_time39391100Using where; Using index; Using temporary; Using filesort

1SIMPLEorgeALLPRIMARY,idx_orgtype110539.09Using where; Using join buffer (Block Nested Loop)

1SIMPLEotrefidx_key,idx_pididx_key8newpearl_db_dev.orge.id6100Using index

1SIMPLEoeq_refPRIMARYPRIMARY8newpearl_db_dev.ot.parent_id150Using where

也不算 很多数据啊, 行数 最多的要 10000多行了。

解决办法

改写了 半天, 我觉得是 以上SQL写法 不够清晰,易懂。一定要写出来 让mysql 很容易明白,解析的SQL才行

SELECT

dt.`time` startDate,

o.brand_id as brandId,

o.id AS orgId,

SUM(ff.totalStore) as totalStore

from

da_dim_time dt ,

(

select

DATE(orge.create_time) ctime,

o.id AS orgId,

count(ot.id) as totalStore

from om_organization o

inner join view_om_org_tree ot on ot.parent_id=o.id

INNER JOIN om_organization orge

on orge.id=ot.id

and orge.org_type='Store' and orge.removed=0

where 1=1

and o.removed=0

group by o.id, ctime

) ff

,

om_organization o

where o.removed=0 and ff.orgId=o.id

and dt.dim_type='DAY' and dt.`time`>='2018-10-30' and dt.`time` < CURDATE()

and ff.ctime <=dt.`time`

and o.id=1

GROUP BY dt.`time`, o.id

同时耗时才 0.15s 难以相信

最终写出来了, 根据 创建时间进行 分组,之后 再进行关联 查询,就出来了 结果。

执行性能

46c1f1545c07ef0df83b194a7534e221.png

那为什么 之前的SQL一直在执行呢?

可以我有另一个 与其相似的SQL是可以出来结果的, 只是 也执行了 20s ,非常耗时。

数据量 比较小,估计mysql 穷尽了所有数据吧,不然怎么这么慢?

以下SQL执行了可以出来结果,虽然耗时20S

# 每个组织下包括 同级或者下级的 注册经销商数

select

dt.`time` startDate,

o.brand_id as brandId,

o.id AS orgId,

count(orgDealer.id) as totalDealer

from om_organization o

inner join view_om_org_tree ot on ot.parent_id=o.id

left join da_dim_time dt on dt.dim_type='DAY' and dt.`time`>='2018-10-30' and dt.`time` < CURDATE()

INNER JOIN

(

select orge.id,DATE(orge.create_time) as cdate from om_organization orge where 1=1

and orge.org_type='Dealer' and orge.removed=0

) as orgDealer on orgDealer.id= ot.id and orgDealer.cdate<=dt.`time`

where 1=1

and o.removed=0

group by o.id , dt.`time`

b9bf21e41bd4c7afec620f6951be0601.png

总结

1.  SQL 一定要 更明白,也就是 从语义上说 让 mysql 解析 更容易, 不要搞那些复杂的。容易出现理解偏差的

2.   将要查询的表 和数据 通过部分进行 分组或者过滤  之后成为中间表, 再关联其他表 , 使其 更 简单化。

3. 根据可能是因为 Using join buffer (Block Nested Loop) , 也就是 join的 循环 太多

来源:oschina

链接:https://my.oschina.net/ouminzy/blog/3133598

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值