In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.