问题背景:
with as(
……
)t1
SELECT
(
SELECT count(distinct )
FROM t1
where money>=500000
) A1,
(
SELECT count(distinct case_id)
FROM t1
where money>=400000
and money <500000
)B1
(
SELECT count(distinct )
FROM t1
)
C1
FROM
dual;
1、 客户反馈,在with as内容中,Oracle和达梦两边SQL执行效率一致。但是在主查询中达梦的执行效率很慢。
2、 判断问题sql在达梦数据库中没有把with as 作为临时temp表使用,从执行计划看,主查询中做了表连接展开,with as内容存在多次查询的情况。失去了with as作为缓存查询的意义。导致重复查询。
3、 优化建议:
(方法1)、主查询中,标量主查询可以改写为case when的形式
(方法2)、with as (…… rownum > 0) 结尾添加 --客户采用建议,问题解决
分析优化和测试过程记录:
1、正常达梦数据库with as 的执行计划,HEAP TABLE SCAN,作为缓存表使用,每次cost不变。
问题sql的执行计划中未体现出该访问路径。尝试在with as ()中加入rownum>0,执行计划出现heap table缓存表。
测试: