一.WITH AS的含義 WITH AS短語,也叫作子查詢部分(subquery factoring),可讓你作不少事情,定義一個SQL片段,該SQL片段會被整個SQL語句所用到。有的時候,是爲了讓SQL語句的可讀性更高些,也有多是在UNION ALL的不一樣部分,做爲提供數據的部分。 特別對於UNION ALL比較有用。由於UNION ALL的每一個部分可能相同,可是若是每一個部分都去執行一遍的話,則成本過高,因此可使用WITH AS短語,則只要執行一遍便可。若是WITH AS短語所定義的表名被調用兩次以上,則優化器會自動將WITH AS短語所獲取的數據放入一個TEMP表裏,若是隻是被調用一次,則不會。而提示materialize則是強制將WITH AS短語裏的數據放入一個全局臨時表裏。不少查詢經過這種方法均可以提升速度。express
二.使用方法 先看下面一個嵌套的查詢語句:性能
1優化
2code
select * from person.StateProvince where CountryRegionCode in遞歸
(select CountryRegionCode from person.CountryRegion where Name like 'C%') 上面的查詢語句使用了一個子查詢。雖然這條SQL語句並不複雜,但若是嵌套的層次過多,會使SQL語句很是難以閱讀和維護。所以,也可使用表變量的方式來解決這個問題,SQL語句以下:
1ip
2get
3it
4io
declare @t table(CountryRegionCode nvarchar(3))table
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')
select * from person.StateProvince where CountryRegionCode in (select * from @t)
雖然上面的SQL語句要比第一種方式更復雜,但卻將子查詢放在了表變量@t中,這樣作將使SQL語句更容易維護,但又會帶來另外一個問題,就是性能的損失。因爲表變量實際上使用了臨時表,從而增長了額外的I/O開銷,所以,表變量的方式並不太適合數據量大且頻繁查詢的狀況。爲此,在SQL Server 2005中提供了另一種解決方案,這就是公用表表達式(CTE),使用CTE,可使SQL語句的可維護性,同時,CTE要比表變量的效率高得多。 下面是CTE的語法:
1
2
3
4
5
[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,n ] ) ] AS ( CTE_query_definition ) 如今使用CTE來解決上面的問題,SQL語句以下:
1
2
3
4
5
6
7
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.StateProvince where CountryRegionCode in (select * from cr)
其中cr是一個公用表表達式,該表達式在使用上與表變量相似,只是SQL Server 2005在處理公用表表達式的方式上有所不一樣。
在使用CTE時應注意以下幾點:
1
2
3
4
5
6
7
8
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.CountryRegion -- 應將這條SQL語句去掉
-- 使用CTE的SQL語句應緊跟在相關的CTE後面 --
select * from person.StateProvince where CountryRegionCode in (select * from cr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
with
cte1 as
(
select * from table1 where name like 'abc%'
),
cte2 as
(
select * from table2 where id > 20
),
cte3 as
(
select * from table3 where price < 100
)
select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id
-- table1是一個實際存在的表
1
2
3
4
5
6
7
with
table1 as
(
select * from persons where age < 30
)
select * from table1 -- 使用了名爲table1的公共表表達式
select * from table1 -- 使用了名爲table1的數據表
CTE 能夠引用自身,也能夠引用在同一 WITH 子句中預先定義的 CTE。不容許前向引用。
不能在 CTE_query_definition 中使用如下子句:
(1)COMPUTE 或 COMPUTE BY
(2)ORDER BY(除非指定了 TOP 子句)
(3)INTO
(4)帶有查詢提示的 OPTION 子句
(5)FOR XML
(6)FOR BROWSE
1
2
3
4
5
6
7
8
9
declare @s nvarchar(3)
set @s = 'C%'
; -- 必須加分號
with
t_tree as
(
select CountryRegionCode from person.CountryRegion where Name like @s
)
select * from person.StateProvince where CountryRegionCode in (select * from t_tree)
CTE除了能夠簡化嵌套SQL語句外,還能夠進行遞歸調用。
實例展現
用戶表
1
2
3
SELECT CONVERT(varchar(100), [RegistDate], 23) dates,COUNT(*) addsuers
FROM [FenxCloudZj].[dbo].[tourol_B2CUser] where [RegistDate]>DATEADD(M,-1,getdate())
group by CONVERT(varchar(100), [RegistDate], 23)
image
接下來使用CTE拼接起來
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/****** Script for SelectTopNRows command from SSMS ******/
WITH CET AS(
SELECT CONVERT(varchar(100), [RegistDate], 23) dates,COUNT(*) addusers FROM [FenxCloudZj].[dbo].[tourol_B2CUser] where [RegistDate]>DATEADD(M,-1,getdate()) group by CONVERT(varchar(100), [RegistDate], 23)
)
SELECT CONVERT(varchar(22),TABLE4.DATES, 102 ) dates,ISNULL(TABLE5.adduers,0) adduers FROM
(
select DATEADD(D,table2.number,table1.mindates) dates from (select MIN(dates) mindates,MAX(dates) maxdates from CET) table1, master..spt_values table2 where table2.type='p' and DATEADD(D,table2.number,table1.mindates)<=table1.maxdates
)TABLE4
LEFT JOIN CET TABLE5 ON TABLE4.DATES=TABLE5.DATES
image