久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

內聯表值 UDF 能否勝過 SELECT 列列表中的等效標量

Can an inline table-valued UDF outperform the equivalent scalar UDF in a SELECT column list?(內聯表值 UDF 能否勝過 SELECT 列列表中的等效標量 UDF?)
本文介紹了內聯表值 UDF 能否勝過 SELECT 列列表中的等效標量 UDF?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

這個問題源于SQLServer:為什么避免使用表- 有價值的用戶定義函數?.我開始在一些評論中提出問題,對我評論的回復偏離了主題.

This question grew out of SQLServer: Why avoid Table-Valued User Defined Functions?. I began asking questions in some of the comments, and the replies to my comments moved off topic.

這樣您就不必閱讀整個討論:我從未聽說過用戶定義函數 (UDF) 很慢,或者應該避免使用.上面引用的問題中發布了一些鏈接,以說明它們很慢.我還是沒明白,求個例子.貼了個例子,性能差別很大.

So that you don't have to read the entire discussion: I had never heard it said that user defined functions (UDF) were slow, or to be avoided. Some links were posted in the question referenced above to illustrate that they were slow. I still didn't get it, and asked for an example. An example was posted, and the performance difference was huge.

我不可能是唯一沒有意識到可能存在如此大的性能差異的人.我覺得這個事實應該分成一個新的問題和答案,以提高被發現的機會.這就是問題".請不要關閉,因為我想給回答者時間來發布答案.

I can't be the only person who did not realize there could be such a large performance difference. I felt this fact should be separated into a new question and answer, to improve its chances of being found. This here is the "question". Please don't close yet, as I'd like to give the answerer time to post the answer.

當然,其他人也應該發布答案或示例.我特別感謝任何能幫助我理解為什么性能差異如此巨大的東西.

Of course, others should also post answers or examples, as well. I'd especially appreciate anything that would help me understand why the performance difference is so huge.

另請注意,我不是在談論在 WHERE 子句中使用 UDF.我知道這會如何阻止優化器完成其工作.當原始 UDF 是 SELECT 列列表的一部分時,我對性能差異特別感興趣.

Note also that I'm not talking about the use of a UDF in a WHERE clause. I'm aware of how this can prevent the optimizer from doing its job. I'm specifically interested in differences in performance when the original UDF was part of the SELECT column list.

推薦答案

為了進行基準測試,讓我們創建一個 100 萬行的表:

For benchmarking let's create a table with 1M rows:

CREATE TABLE dbo.Numbers(n INT NOT NULL PRIMARY KEY)
GO
DECLARE @i INT;
SET @i = 1;
INSERT INTO dbo.Numbers(n) SELECT 1;
WHILE @i<1024000 BEGIN
  INSERT INTO dbo.Numbers(n)
    SELECT n + @i FROM dbo.Numbers;
  SET @i = @i * 2;
END;
GO

運行簡單的內聯添加:

SELECT COUNT(*) FROM(
SELECT n,n+1 AS ValuePlusOne
FROM  dbo.Numbers
) AS t WHERE ValuePlusOne>0

   CPU time = 15 ms, elapsed time = 122 ms.

(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 3, read-ahead reads 3498, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
   CPU time = 406 ms,  elapsed time = 951 ms.

創建一個標量 UDF,將一個整數加一,并運行 100 萬次:

Create a scalar UDF that just adds one to an integer, and run it 1M times:

CREATE FUNCTION dbo.[AddOne] 
(
        @value int
)
RETURNS int
AS
BEGIN
        DECLARE @Result int
        SELECT @Result = @value + 1
        RETURN @Result
END
GO

SELECT COUNT(*) FROM(
SELECT n,dbo.AddOne(n) AS ValuePlusOne
FROM  dbo.Numbers
) AS t WHERE ValuePlusOne>0

   CPU time = 15 ms, elapsed time = 122 ms.

(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
   CPU time = 108313 ms,  elapsed time = 295072 ms.

創建一個內聯 UDF,它和添加一樣快,并運行 100 萬次:

Create an inline UDF, which is just as fast as just adding, and run that 1M times:

CREATE FUNCTION dbo.[AddOneInline] 
(
        @value int
)
RETURNS TABLE
AS
RETURN(SELECT @value + 1 AS ValuePlusOne)
GO

SELECT COUNT(*) FROM(
SELECT ValuePlusOne
FROM  dbo.Numbers
CROSS APPLY dbo.[AddOneInline](n)
) AS t WHERE ValuePlusOne>0

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 35 ms.

(1 row(s) affected)
Table 'Numbers'. Scan count 1, logical reads 3521, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
   CPU time = 391 ms,  elapsed time = 403 ms.

標量 UDF 與內聯 UDF 的性能差異很明顯.

The difference in performance of a scalar UDF vs. an inline one is obvious.

這篇關于內聯表值 UDF 能否勝過 SELECT 列列表中的等效標量 UDF?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 久久国产精品99久久久久久丝袜 | 91精品国产91久久久久久最新 | av天天看 | 亚洲精品一区二区 | 欧美不卡在线 | 国产精品毛片一区二区三区 | 奇米av| 国产999精品久久久 日本视频一区二区三区 | 国产一区欧美 | 中文在线一区二区 | 日本精品视频在线 | 干干干操操操 | 国产在线一区二区三区 | 91在线免费视频 | 涩色视频在线观看 | 精品视频在线一区 | 成人在线看片 | 91在线免费视频 | 国产免费播放视频 | 免费国产一区二区 | 伊人看片 | 黄色av免费| 天天操综合网站 | 久久久久久99 | 女人一区 | 手机在线不卡av | 国产精品一区在线观看 | 国际精品久久 | 国产精品区一区二区三 | 国产一区二区三区四区 | 中文字幕国产精品 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 一区精品视频在线观看 | 美女在线国产 | 精品久久久久久久 | 国产黄色网址在线观看 | 中国91av| 日本久草 | 欧美精品1区 | 亚洲日韩视频 | 精久久久|