Why is a UDF so much slower than a subquery?

前端 未结 4 1448
臣服心动
臣服心动 2020-11-28 12:03

I have a case where I need to translate (lookup) several values from the same table. The first way I wrote it, was using subqueries:

SELECT
    (SELECT id FRO         


        
4条回答
  •  离开以前
    2020-11-28 12:58

    The UDF is a black box to the query optimiser so it's executed for every row. You are doing a row-by-row cursor. For each row in an asset, look up an id three times in another table. This happens when you use scalar or multi-statement UDFs (In-line UDFs are simply macros that expand into the outer query)

    One of many articles on the problem is "Scalar functions, inlining, and performance: An entertaining title for a boring post".

    The sub-queries can be optimised to correlate and avoid the row-by-row operations.

    What you really want is this:

    SELECT
       uc.id AS creator,
       uu.id AS updater,
       uo.id AS owner,
       a.[name]
    FROM
        asset a
        JOIN
        user uc ON uc.user_pk = a.created_by
        JOIN
        user uu ON uu.user_pk = a.updated_by
        JOIN
        user uo ON uo.user_pk = a.owned_by
    

    Update Feb 2019

    SQL Server 2019 starts to fix this problem.

提交回复
热议问题