
高性能 SQL 调优精要与案例解析 10.4 SQL 语句改写
电子工业出版社 闫书清
10.4 SQL 语句改写
10.4.1 消除视图
改写前:
SQL> create view v_test as
select * from tab1 where col1=...;
SQL> select * from tab2 t2,tab3 t3,v_test v
where t2.col1=t3.col1
and t3.col1=v.col1
and v.col2=...;
改写后:
SQL> select * from tab2 t2,tab3 t3,tab1 t1
where t2.col1=t3.col1
and t3.col1=t1.col1
and t1.col2=...
and t1.col1=...;
10.4.2 标量子查询改为外连接
改写前:
SQL> select t1.col1,
(
select col2
from
(
select t2.col1,sum(t2.col2) col2
from tab2 t2
group by t2.col1
) t3
where t3.col1=t1.col2
) col2_sum
from tab1 t1
where t1.col3=...;
改写后:
SQL> select t1.col1,t3.col2 col2_sum
from tab1 t1,
(
select t2.col1,sum(t2.col2) col2
from tab2 t2
group by t2.col1
) t3
where t1.col2=t3.col1(+)
and t1.col3=...;
评论