SQL Server最佳化標量函式改寫內聯表值函式
問題SQL:
SELECT TOP 1001 ha.HuntApplicationID , ha.PartyNumber , mht.Name AS MasterHuntTypeName , htly.LicenseYear , lStatus.[Status] AS DrawTicketStatus , isnull(dbo.udf_GetHuntApplicationPartyCount(ha.HuntApplicationID), 0) AS MemberCount , count( won.DrawTicketLicenseID) AS DrawnMemberCount , won.drawticketid , dt.PreDrawNonResidentMemberCount AS NRMemberCount , dbo.udf_GetAvgPreferencePoints(dt.DrawTicketID) AS PreferencePointAverage , CASE WHEN ha.Quantity > 1 THEN NULL ELSE dt.PreDrawRandomNumber END AS PreDrawRandomNumber , dsm.Name AS DrawSelectionMethodName , dt.DrawnSequence , dt.PreferencePointRank , dt.DrawID , dt.RandomRank FROM dbo.HuntApplication ha JOIN dbo.HuntTypeLicenseYear htly ON ha.HuntTypeLicenseYearID = htly.HuntTypeLicenseYearID JOIN dbo.MasterHuntType mht ON htly.MasterHuntTypeID = mht.MasterHuntTypeID LEFT JOIN dbo.HuntApplicationLicense hal ON ha.HuntApplicationID = hal.HuntApplicationID LEFT JOIN dbo.DrawTicket dt ON ha.HuntApplicationID = dt.HuntApplicationID LEFT JOIN dbo.DrawTicketLicense won ON dt.DrawTicketID = won.DrawTicketID AND won.WasDrawn = 1 LEFT JOIN dbo.DrawSelectionMethod dsm ON dt.DrawSelectionMethodID = dsm.DrawSelectionMethodID LEFT JOIN dbo.StatusCode lStatus ON dt.StatusCodeID = lStatus.StatusCodeID JOIN dbo.DrawTicketHuntChoice dthc ON dt.DrawTicketID = dthc.DrawTicketID CROSS APPLY dbo.tvf_GetHuntApplicationPartyCount(ha.HuntApplicationID) hapc CROSS APPLY dbo.tvf_GetAvgPreferencePoints(dt.DrawTicketID) app WHERE 1 = 1 AND htly.MasterHuntTypeID = @iMasterHuntTypeID AND htly.LicenseYear = @iLicenseYear AND dt.StatusCodeID = @iDrawTicketStatusCodeID AND dthc.WasDrawn = @iHuntChoiceWasDrawn GROUP BY ha.HuntApplicationID, ha.PartyNumber, mht.[Name], htly.LicenseYear, lStatus.[Status], isnull(dbo.udf_GetHuntApplicationPartyCount(ha.HuntApplicationID), 0), won.DrawTicketID, dt.PreDrawNonResidentMemberCount, dbo.udf_GetAvgPreferencePoints(dt.DrawTicketID), CASE WHEN ha.Quantity > 1 THEN NULL ELSE dt.PreDrawRandomNumber END, dsm.[Name], dt.DrawnSequence, dt.PreferencePointRank, dt.DrawID, dt.RandomRank ORDER BY htly.LicenseYear DESC, mht.Name, lStatus.[Status], dt.DrawID, PreferencePointAverage DESC, PreDrawRandomNumber, ha.PartyNumber
靜態函式:
CREATE FUNCTION [dbo].[udf_GetAvgPreferencePoints] (@DrawTicketID INT) RETURNS NUMERIC (18, 3) AS BEGIN RETURN ( SELECT TOP 1 CONVERT(DECIMAL, dt.PreDrawPreferencePointTotal) / NULLIF(CONVERT(DECIMAL, dt.PreDrawMemberCount),0) FROM dbo.DrawTicket dt WHERE dt.DrawTicketID = @DrawTicketID ) END
執行時間40s
這是典型可以進行靜態函式改寫內聯表值函式的sql:
改寫後:
SELECT TOP 1001 ha.HuntApplicationID , ha.PartyNumber , mht.Name AS MasterHuntTypeName , htly.LicenseYear , lStatus.[Status] AS DrawTicketStatus , --isnull(dbo.udf_GetHuntApplicationPartyCount(ha.HuntApplicationID), 0) AS MemberCount , isnull(hapc.MemberCount, 0) AS MemberCount, count( won.DrawTicketLicenseID) AS DrawnMemberCount , won.drawticketid , dt.PreDrawNonResidentMemberCount AS NRMemberCount , --dbo.udf_GetAvgPreferencePoints(dt.DrawTicketID) AS PreferencePointAverage , app.PreferencePointAverage PreferencePointAverage, CASE WHEN ha.Quantity > 1 THEN NULL ELSE dt.PreDrawRandomNumber END AS PreDrawRandomNumber , dsm.Name AS DrawSelectionMethodName , dt.DrawnSequence , dt.PreferencePointRank , dt.DrawID , dt.RandomRank FROM dbo.HuntApplication ha JOIN dbo.HuntTypeLicenseYear htly ON ha.HuntTypeLicenseYearID = htly.HuntTypeLicenseYearID JOIN dbo.MasterHuntType mht ON htly.MasterHuntTypeID = mht.MasterHuntTypeID LEFT JOIN dbo.HuntApplicationLicense hal ON ha.HuntApplicationID = hal.HuntApplicationID LEFT JOIN dbo.DrawTicket dt ON ha.HuntApplicationID = dt.HuntApplicationID LEFT JOIN dbo.DrawTicketLicense won ON dt.DrawTicketID = won.DrawTicketID AND won.WasDrawn = 1 LEFT JOIN dbo.DrawSelectionMethod dsm ON dt.DrawSelectionMethodID = dsm.DrawSelectionMethodID LEFT JOIN dbo.StatusCode lStatus ON dt.StatusCodeID = lStatus.StatusCodeID JOIN dbo.DrawTicketHuntChoice dthc ON dt.DrawTicketID = dthc.DrawTicketID CROSS APPLY dbo.tvf_GetHuntApplicationPartyCount(ha.HuntApplicationID) hapc CROSS APPLY dbo.tvf_GetAvgPreferencePoints(dt.DrawTicketID) app WHERE 1 = 1 AND htly.MasterHuntTypeID = @iMasterHuntTypeID AND htly.LicenseYear = @iLicenseYear AND dt.StatusCodeID = @iDrawTicketStatusCodeID AND dthc.WasDrawn = @iHuntChoiceWasDrawn GROUP BY ha.HuntApplicationID, ha.PartyNumber, mht.[Name], htly.LicenseYear, lStatus.[Status], --isnull(dbo.udf_GetHuntApplicationPartyCount(ha.HuntApplicationID), 0), isnull(hapc.MemberCount, 0), won.DrawTicketID, dt.PreDrawNonResidentMemberCount, --dbo.udf_GetAvgPreferencePoints(dt.DrawTicketID), app.PreferencePointAverage, CASE WHEN ha.Quantity > 1 THEN NULL ELSE dt.PreDrawRandomNumber END, dsm.[Name], dt.DrawnSequence, dt.PreferencePointRank, dt.DrawID, dt.RandomRank ORDER BY htly.LicenseYear DESC, mht.Name, lStatus.[Status], dt.DrawID, PreferencePointAverage DESC, PreDrawRandomNumber, ha.PartyNumber
對應的表值函式:
CREATE FUNCTION [dbo].[tvf_GetAvgPreferencePoints] (@DrawTicketID INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN ( SELECT TOP 1 CONVERT(DECIMAL, dt.PreDrawPreferencePointTotal) / NULLIF(CONVERT(DECIMAL, dt.PreDrawMemberCount),0) as PreferencePointAverage FROM dbo.DrawTicket dt WHERE dt.DrawTicketID = @DrawTicketID ) GO
改寫後執行時間從40s降低到16s,對於傾斜列的最佳化速度更為明顯
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69950462/viewspace-2666110/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 分析函式改寫自關聯函式
- SQL Server實戰七:自定義資料型別、標量值、內嵌表值、多語句表值函式的操作SQLServer資料型別函式
- SQL Server 2016 函式:CASTSQLServer函式AST
- SQL Server常用函式整理SQLServer函式
- Hive函式(內建函式+自定義標準函式UDF)Hive函式
- SQL KEEP 視窗函式等價改寫案例SQL函式
- SQL server儲存過程函式SQLServer儲存過程函式
- 【SQL Server】常見系統函式SQLServer函式
- SQL-函式 - 聚合函式SQL函式
- 指標函式 和 函式指標指標函式
- sql函式SQL函式
- 內聯(inline)函式與虛擬函式(virtual)的討論inline函式
- 物聯網學習教程—函式引數和函式的值函式
- Sql 中的 left 函式、right 函式SQL函式
- T-SQL——函式——字串操作函式SQL函式字串
- webgl內建函式--通用函式Web函式
- 支援向量機(非線性模型)——改寫最佳化目標函式和限制條件模型函式
- sql server 資料型別轉換函式SQLServer資料型別函式
- 【SQL】19 SQL函式SQL函式
- 【不在混淆的C】指標函式、函式指標、回撥函式指標函式
- 函式指標&回撥函式Callback函式指標
- 【SQL】17 SQL 檢視(Views)、SQL Date 函式、SQL NULL 值、SQLView函式Null
- T-SQL——函式——時間操作函式SQL函式
- 箭頭函式、簡寫函式、普通函式的區別函式
- webgl內建函式--指數函式Web函式
- 高階函式與標籤函式,解構賦值與物件字面量的簡化學習函式賦值物件
- webgl內建函式--幾何函式與矩陣函式Web函式矩陣
- webgl內建函式--向量函式與紋理查詢函式Web函式
- 虛擬函式,虛擬函式表函式
- 傳遞指標引數(函式內部給指標賦值)示例指標函式賦值
- SQL Server建立使用者函式與應用SQLServer函式
- 函式指標函式指標
- SQL最佳化案例-自定義函式索引(五)SQL函式索引
- python內建函式-eval()函式與exec()函式的區別Python函式
- 如何在函式內部定義函式?函式
- 函式外與函式內的變數函式變數
- 函式的祕密之 函式返回值函式
- [C++] 成員函式指標和函式指標C++函式指標