iT邦幫忙

DAY 9
4

MS SQL Server新特性探險:探險代碼 Denali系列 第 9

[Denali 新特性探險9]With Result Sets

這篇介紹With Result Sets。
With Result Sets: 返回 Exec 陳述句結果集。
注意:With Result Sets不支援 Insert …. Exec 陳述句

以前我們想要取得 SP(store procedure)中的資料結果集,
通常會將結果塞入 TempTable 或 TVP..等,
然後再查詢這些中繼表取得相關結果集,
但 Denali 不需要你這麼麻煩(不需要使用 TempTable或TVP..等),
我們可以直接利用 With Result Sets 就能達到目的,
且也能減少整體時間,同時更能在執行過程中更改欄位名稱或資料型別,
下面我們就來測試比較兩者效能上的差異。

假設今天SP需要返回50000筆資料,我們可以來看看兩者效能上的差異。

Denali 作法

create proc dbo.mysp
as
select top(50000) TransactionID,TransactionDate,TransactionType  from Production.TransactionHistoryArchive 

Using With Result Sets
declare @starttime time(7),@endtime time(7),@elaspedtime numeric(20,7);
set @starttime=GETDATE() ;
--執Xo行adbo.nysp
exec dbo.mysp
with result sets
(
(
TransactionID int,
MyTransactionDate datetime, --變更欄位名稱
TransactionType varchar(10) --變更類型
)
)
set @endtime=GETDATE(); 
set @elaspedtime = convert(integer, datediff(ms, @starttime, @endtime));
select @elaspedtime as '花費時間(ms)' 


所花費時間。


執行計畫總成本。

SQL2005/2008作法

create proc dbo.mysp2
as
select * into ##myresult 
from (select top(50000) TransactionID,TransactionDate,TransactionType  from Production.TransactionHistoryArchive  ) a





--Using TempTable
declare @starttime time(7),@endtime time(7),@elaspedtime numeric(20,7);
set @starttime=GETDATE() ;
--執行dbo.nysp2
exec dbo.mysp2
select * from ##myresult
set @endtime=GETDATE(); 
set @elaspedtime = convert(integer, datediff(ms, @starttime, @endtime));
select @elaspedtime as '花費時間(ms)' 


所花費時間。


結果比較表

透過結果比較表,很明顯我們可以看到With Result Sets所帶來效能上的改善,
但個人覺得如果欄位過多那在開發撰寫上倒是有點麻煩(因為需要明確寫出相關欄位和資料型別),
但為了獲得更好的效能,這點我個人倒是還可以接受,
不過我還是希望 with result可以有個預設欄位設定,
好比with result set(default(0) all):
返回SP中第一個結果集中的所有欄位,
如果發行RTM版本with result sets能有類似這樣的改善,
那真的有如全X電子說的:讓人就感心。

MS SQL Server新特性探險:探險代碼 Denali 全系列文章


上一篇
[Denali 新特性探險8]Rows and Range
下一篇
[Denali 新特性探險10]Sequence
系列文
MS SQL Server新特性探險:探險代碼 Denali30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言