Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rewrite inline table-valued functions in SQL Server

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to rewrite inline table-valued functions in SQL Server". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to rewrite inline table-valued functions in SQL Server".

Question 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.RandomRankFROM dbo.HuntApplication haJOIN dbo.HuntTypeLicenseYear htly ON ha.HuntTypeLicenseYearID = htly.HuntTypeLicenseYearIDJOIN dbo.MasterHuntType mht ON htly.MasterHuntTypeID = mht.MasterHuntTypeIDLEFT JOIN dbo.HuntApplicationLicense hal ON ha.HuntApplicationID = hal.HuntApplicationIDLEFT JOIN dbo.DrawTicket dt ON ha.HuntApplicationID = dt.HuntApplicationIDLEFT JOIN dbo.DrawTicketLicense won ON dt.DrawTicketID = won.DrawTicketIDAND won.WasDrawn = 1LEFT JOIN dbo.DrawSelectionMethod dsm ON dt.DrawSelectionMethodID = dsm.DrawSelectionMethodIDLEFT JOIN dbo.StatusCode lStatus ON dt.StatusCodeID = lStatus.StatusCodeIDJOIN dbo.DrawTicketHuntChoice dthc ON dt.DrawTicketID = dthc.DrawTicketIDCROSS APPLY dbo .TV f _ GetHuntApplicationPartyCount (ha.HuntApplicationID) hapcCROSS APPLY dbo.tvf_GetAvgPreferencePoints (dt.DrawTicketID) appWHERE 1 = 1 AND htly.MasterHuntTypeID = @ iMasterHuntTypeID AND htly.LicenseYear = @ iLicenseYear AND dt.StatusCodeID = @ iDrawTicketStatusCodeID AND dthc.WasDrawn = @ iHuntChoiceWasDrawnGROUP 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.RandomRankORDER BY htly.LicenseYear DESC, mht.Name, lStatus. [Status], dt.DrawID, PreferencePointAverage DESC, PreDrawRandomNumber, ha.PartyNumber

Static function:

CREATE FUNCTION [dbo]. [udf_GetAvgPreferencePoints] (@ DrawTicketID INT) RETURNS NUMERIC (18,3) ASBEGIN RETURN (SELECT TOP 1 CONVERT (DECIMAL, dt.PreDrawPreferencePointTotal) / NULLIF (CONVERT (DECIMAL, dt.PreDrawMemberCount), 0) FROM dbo.DrawTicket dt WHERE dt.DrawTicketID = @ DrawTicketID) END

Execution time 40s

This is a typical sql that can overwrite inline table-valued functions with static functions:

After rewriting:

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.RandomRankFROM dbo.HuntApplication haJOIN dbo.HuntTypeLicenseYear htly ON ha.HuntTypeLicenseYearID = htly.HuntTypeLicenseYearIDJOIN dbo.MasterHuntType mht ON htly.MasterHuntTypeID = mht.MasterHuntTypeIDLEFT JOIN dbo.HuntApplicationLicense hal ON ha.HuntApplicationID = hal.HuntApplicationIDLEFT JOIN dbo.DrawTicket dt ON ha.HuntApplicationID = dt.HuntApplicationIDLEFT JOIN dbo.DrawTicketLicense won ON dt.DrawTicketID = won.DrawTicketIDAND won.WasDrawn = 1LEFT JOIN dbo.DrawSelectionMethod dsm ON dt.DrawSelectionMethodID = dsm.DrawSelectionMethodIDLEFT JOIN dbo.StatusCode lStatus ON dt.StatusCodeID = lStatus.StatusCodeIDJOIN dbo.DrawTicketHuntChoice dthc ON dt.DrawTicketID = dthc.DrawTicketIDCROSS APPLY dbo .TV f _ GetHuntApplicationPartyCount (ha.HuntApplicationID) hapcCROSS APPLY dbo.tvf_GetAvgPreferencePoints (dt.DrawTicketID) appWHERE 1 = 1 AND htly.MasterHuntTypeID = @ iMasterHuntTypeID AND htly.LicenseYear = @ iLicenseYear AND dt.StatusCodeID = @ iDrawTicketStatusCodeID AND dthc.WasDrawn = @ iHuntChoiceWasDrawnGROUP 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.RandomRankORDER BY htly.LicenseYear DESC, mht.Name, lStatus. [Status], dt.DrawID, PreferencePointAverage DESC, PreDrawRandomNumber, ha.PartyNumber

Corresponding table-valued functions:

CREATE FUNCTION [dbo]. [tvf_GetAvgPreferencePoints] (@ DrawTicketID INT) RETURNS TABLE WITH SCHEMABINDINGASRETURN (SELECT TOP 1 CONVERT (DECIMAL, dt.PreDrawPreferencePointTotal) / NULLIF (CONVERT (DECIMAL, dt.PreDrawMemberCount), 0) as PreferencePointAverage FROM dbo.DrawTicket dt WHERE dt.DrawTicketID = @ DrawTicketID) GO

After rewriting, the execution time is reduced from 40s to 16s, and the optimization speed for slanted columns is more obvious.

Thank you for your reading, the above is the content of "how to rewrite inline table-valued functions in SQL Server". After the study of this article, I believe you have a deeper understanding of how to rewrite inline table-valued functions in SQL Server, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report