这个函数的最优程度如何

发布于 2024-09-27 13:13:12 字数 424 浏览 7 评论 0原文

我即将实现这个函数来计算一些数字。

CREATE FUNCTION [dbo].[funLookupFTE] (@PFID int) RETURNS
    VARCHAR(20) AS BEGIN 

DECLARE @NumberOfFTE AS VARCHAR(20)
SET @NumberOfFTE = (SELECT  SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1     END) AS FTECount 
                    FROM tblPractitioners 
                    WHERE PFID =
@PFID)
RETURN @NumberOfFTE
END

只是想看看它是否是最佳的,并且不会自行解决问题。

I'm about to implement this function to calculate some numbers.

CREATE FUNCTION [dbo].[funLookupFTE] (@PFID int) RETURNS
    VARCHAR(20) AS BEGIN 

DECLARE @NumberOfFTE AS VARCHAR(20)
SET @NumberOfFTE = (SELECT  SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1     END) AS FTECount 
                    FROM tblPractitioners 
                    WHERE PFID =
@PFID)
RETURN @NumberOfFTE
END

Just trying to see if it's optimal and it won't take matters into its own hand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-10-04 13:13:12

如果要在任何类型的查询中调用它,您最好使用子查询而不是用户定义的函数。

例如:

SELECT * FROM tblPractioners P1
INNER JOIN
(
    SELECT  PFID, SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1 END) 
        AS FTECount 
    FROM tblPractitioners 
    GROUP BY PFID
) P2 ON P1.PFID = P2.PFID

用户定义函数的性能可能非常差,但您确实需要使用子查询或 UDF 来比较执行计划,看看哪个更好。

If this is to be called in any sort of query, you're probably better off using a subquery instead of a user-defined function.

For example:

SELECT * FROM tblPractioners P1
INNER JOIN
(
    SELECT  PFID, SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1 END) 
        AS FTECount 
    FROM tblPractitioners 
    GROUP BY PFID
) P2 ON P1.PFID = P2.PFID

Performance of user-defined functions can be very poor, but you'll really need to compare the execution plans using a subquery or UDF to see which is better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文