在MS SQL Server中SQL的执行顺序是固定的. join 是在where前面执行的
(8)SELECT (9)DISTINCT (11)<Top Num> <select list>
(1)FROM [left_table]
(3)<join_type> JOIN <right_table>
(2) ON <join_condition>
(4)WHERE <where_condition>
(5)GROUP BY <group_by_list>
(6)WITH <CUBE | RollUP>
(7)HAVING <having_condition>
(10)ORDER BY <order_by_list>
但在HANA的目前版本中(1.0.33)where和join的执行顺序不是固定的,有下列三种情况
当where的后面条件跟等号的时候,会先做where后面的过滤,然后再做join
select * from employee e left join course c on e.empid=c.studentid
where mgrid =7
当where的条件是判断是否为空的时候,会先做join,再where过滤
select * from employee e left join course c on e.empid=c.studentid where score is null
当where的条件有等号也有判断是否为空的时候,会先做where的等号过滤,再join。再做判断是否为空的过滤
select * from employee e left join course c on e.empid=c.studentid where mgrid=7 and score is null
这种机制让HANA在表join的时候会提高性能,但如果不清楚这些执行顺序,会让你的sql结果不是你所期望的结果。
由于HANA目前一直在开发中,新release的版本可能顺序就不是这样的,所以需要使用这样的SQL语句之前最好用执行计划查看下
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
8 | |
8 | |
7 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 |