重写为:
name = ‘abc’
条件化简
:
主要针对
where
,
having
和
on
条件化简。
sql
语句没有
group by
时候将
having
和
where
条件合并
去除冗余的括号
如:((
a and b
)
and
(
c and d
)化简为
a and b and c and d
常量传递
如:
col1 =col2 and col2=3
化简为
col1=3 and col2=3
消除死码
如:
where
(
0 >1 and s1 =5
)这个条件化简为恒为假
外连消除
:
由于外连过程中左右子树不能互换这种性质性质了优化器选择连接顺序时交换表连接
顺序的优化方式。
左连接转内连接
全外连接转左外连接
右连接转左连接
嵌套消除
:
只有内连的嵌套查询可以去掉括号,交换表的连接顺序。
如:
select * from A join
(
B join C on B.b1 = C.c1
)
on A.a1 = B.b1 where A.a1 > 1;
转换为
select * from A join B join C on B.b1 = C.c1 on A.a1 = B.b1 where A.a1 > 1;
如果有左右连接,表的连接顺序不可以改变不能进行嵌套消除优化。
子查询优化
:
子查询可以出现的位置:
目标列(标量子查询)
FROM
子句(非相关子查询)
WHERE
子句
JOIN/ON
子句
GROUP BY
子句
ORDER BY
子句
子查询分类:
非相关子查询
相关子查询
子查询优化思路
:
子查询合并
select * from t1 where a < 10 and exists (select a2 from t2 where t2.a2 < 5 and t2.b2 =1) or
exists (select a2 from t2 where t2.a2 < 5 and t2.b2 = 2);
经过优化后,我们可以将两个子查询合并如下:
select * from t1 where a < 10 and exists (select a2 from t2 where t2.a2 < 5 and
(
t2.b2 =1
or t2.b2 = 2
)
;
子查询展开(子查询上拉)
:
评论