20万数据的子查询外加入where条件,执行速度减慢?

下面一段SQL,如果不加where str1 = '1' 执行速度很快,仅需5秒,但是加入where查询之后,无法产生结果,总体数据量仅有20万,各位大拿给看看是什么原因。SQL如下:selectcount(distinct str1)from(select a1.str0 ,a1.str4 ,case when a2.str3=1 then '1' else '0' end a...显示全部

下面一段SQL,如果不加where str1 = '1' 执行速度很快,仅需5秒,但是加入where查询之后,无法产生结果,总体数据量仅有20万,各位大拿给看看是什么原因。
SQL如下:
select

count(distinct str1)

from
(

select
    a1.str0
    ,a1.str4
    ,case when a2.str3=1 then '1' else '0' end as str1
from table1 a1
left join table2 a2
on a1.str0=a2.str0

)
where str1 = '1'

收起
参与14

查看其它 2 个回答冰玉的回答

冰玉冰玉数据库开发工程师上海海典

对str1进行where再做count(distinct)肯定是1啊,如果这样做慢可能是distinct的时候有问题,你试一下把didtinct换成group by。
一般sql不正常执行慢:
先对两个表做reorg,runstatus,如果没用,再用db2expln -d bidbdw -q "select * from test" -g -t看一下执行计划。

IT其它 · 2018-02-27
浏览2062

回答者

冰玉
数据库开发工程师上海海典

冰玉 最近回答过的问题

回答状态

  • 发布时间:2018-02-27
  • 关注会员:4 人
  • 回答浏览:2062
  • X社区推广