华为云云数据库GaussDB案例:改写SQL消除子查询(案例1)_云淘科技

现象描述

1
2
3
4
select 
    1,
    (select count(*) from customer_address_001 a4 where a4.ca_address_sk = a.ca_address_sk) as GZCS 
from customer_address_001 a;

此SQL性能较差,查看发现执行计划中存在SubPlan,具体如下:

优化说明

此优化的核心就是消除子查询。分析业务场景发现a.ca_address_sk不为null,那么从SQL语义出发,可以等价改写SQL为:

1
2
3
4
5
select 
count(*) 
from customer_address_001 a4, customer_address_001 a
where a4.ca_address_sk = a.ca_address_sk
group by  a.ca_address_sk;

为了保证改写的等效性,在customer_address_001. ca_address_sk加了not null约束。

父主题: 实际调优案例

同意关联代理商云淘科技,购买华为云产品更优惠(QQ 78315851)

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家