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

為什么 SQL 函數比 UDF 快

Why SQL functions are faster than UDF(為什么 SQL 函數比 UDF 快)
本文介紹了為什么 SQL 函數比 UDF 快的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

雖然這是一個相當主觀的問題,但我覺得有必要在這個論壇上分享.

我個人經歷過,當我創建一個 UDF(即使它并不復雜)并將其用于我的 SQL 時,它會大大降低性能.但是當我使用

將邏輯移動到內聯表值函數中

CREATE FUNCTION dbo.F2 (@N BIGINT)退貨表返回(選擇@N - @N AS X)

并將查詢重寫為

SELECT MAX(X)發件人號碼交叉應用 dbo.F2(N)

與不使用任何函數的原始查詢一樣快地執行.

Though it's a quite subjective question but I feel it necessary to share on this forum.

I have personally experienced that when I create a UDF (even if that is not complex) and use it into my SQL it drastically decrease the performance. But when I use SQL inbuild function they happen to work pretty faster. Conversion , logical & string functions are clear example of that.

So, my question is "Why SQL in build functions are faster than UDF"? and it would be an advantage if someone can guide me how can I judge/manipulate function cost either mathematically or logically.

解決方案

This is a well known issue with scalar UDFs in SQL Server.

They are not inlined into the plan and calling them adds overhead compared with having the same logic inline.

The following takes just under 2 seconds on my machine

WITH T10(N) AS 
(
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
) --10 rows                                    
, T(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
           FROM T10 a, T10 b, T10 c, T10 d, T10 e, T10 f, T10 g)  -- 10 million rows
SELECT MAX(N - N)
FROM T
OPTION (MAXDOP 1)

Creating the simple scalar UDF

CREATE FUNCTION dbo.F1 (@N BIGINT)
RETURNS BIGINT 
WITH SCHEMABINDING
AS
BEGIN
RETURN (@N - @N)
END

And changing the query to MAX(dbo.F1(N)) instead of MAX(N - N) it takes around 26 seconds with STATISTICS TIME OFF and 37 with it on.

An average increase of 2.6μs / 3.7μs for each of the 10 million function calls.

Running the Visual Studio profiler shows that the vast majority of time is taken under UDFInvoke. The names of the methods in the call stack gives some idea of what the additional overhead is doing (copying parameters, executing statements, setting up security context).

Moving the logic into an inline table valued function

CREATE FUNCTION dbo.F2 (@N BIGINT)
RETURNS TABLE
RETURN(SELECT @N - @N AS X)

And rewriting the query as

SELECT MAX(X)
FROM Nums
CROSS APPLY dbo.F2(N)

executes in as fast as a time as the original query that does not use any functions.

這篇關于為什么 SQL 函數比 UDF 快的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品久久久久aaaa樱花 | 依人成人 | 精品一区二区三区在线观看国产 | 日韩在线观看一区 | 亚洲人a | 色片在线观看 | 亚洲性视频 | 亚洲男女视频在线观看 | 97超碰在线免费 | 久久伊人精品一区二区三区 | 亚洲a网| 午夜激情视频在线 | 美国十次成人欧美色导视频 | 久久伊人操 | 欧美激情综合五月色丁香小说 | 国产福利在线视频 | 欧美成人h版在线观看 | 日本天堂一区二区 | 国产亚洲精品综合一区 | 一级黄色片在线免费观看 | 精品久久久久一区二区国产 | 国产日韩一区二区三区 | 97超碰在线播放 | 国产精品成人一区二区三区 | 日韩色在线 | 亚洲精品在线免费播放 | 国产伦精品一区二区三区照片91 | 免费永久av| 久草电影网 | 欧美黄色大片在线观看 | 韩国欧洲一级毛片 | 影音先锋欧美资源 | 亚洲 中文 欧美 日韩 在线观看 | 中文字幕第一页在线 | 在线国产视频 | 亚洲在线 | 色嗨嗨| 国产一区二区视频在线观看 | 亚洲精品黑人 | 精品乱人伦一区二区三区 | 精品国产女人 |