27,579
社区成员
发帖
与我相关
我的任务
分享
SQL Select语句完整的执行顺序:
1、from子句组装来自不同数据源的数据;
2、where子句基于指定的条件对记录行进行筛选;
3、group by子句将数据划分为多个分组;
4、使用聚集函数进行计算;
5、使用having子句筛选分组;
6、计算所有的表达式;
7、使用order by对结果集进行排序。
另外:一个查询语句各个部分的执行顺序:
--8) SELECT (9) DISTINCT (11) <TOP_specification> <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>
对where,group by,order by 字段加索引,还有就是合理的语句,尽量避免一些复杂的计算
--求答案。
---专门支持!
;with A as(
select t1.*,t2.xxx,t3.yyyy
from t1
inner join t2
on t1.x=t2.x
inner join t3
on t1.y=t3.y
),B as(
select t4.yyyy,t4.ZZZZZ
from t4
)
select A.*,B.ZZZZZ
from A
inner join B
on A.yyyy=B.yyyy
select t1.*,t2.xxx,t3.yyyy
from t1
into #A
inner join t2
on t1.x=t2.x
inner join t3
on t1.y=t3.y;
select t4.yyyy,t4.ZZZZZ
from t4
into #B;
select #A.*,#B.ZZZZZ
from #A
inner join #B
on #A.yyyy=#B.yyyy
--别忘drop掉临时表
drop table #A
drop table #B