网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SQLServer中怎么改写内联表值函数

这篇文章主要讲解了“SQL Server中怎么改写内联表值函数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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,对于倾斜列的优化速度更为明显

感谢各位的阅读,以上就是“SQL Server中怎么改写内联表值函数”的内容了,经过本文的学习后,相信大家对SQL Server中怎么改写内联表值函数这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


新闻名称:SQLServer中怎么改写内联表值函数
网站URL:http://bjjierui.cn/article/gdepjh.html

其他资讯