Optimizing Execution Plans for Parameterized T-SQL Queries Containing Window Functions

前端 未结 3 821
逝去的感伤
逝去的感伤 2020-12-05 18:33

EDIT: I\'ve updated the example code and provided complete table and view implementations for reference, but the essential question remains unchanged.

3条回答
  •  长情又很酷
    2020-12-05 19:13

    You could always go the CROSS APPLY way.

    ALTER VIEW [dbo].[ViewOnBaseTable]
    AS
    SELECT
        PrimaryKeyCol,
        ForeignKeyCol,
        ForeignKeyRank,
        DataCol
    FROM (
        SELECT DISTINCT
            ForeignKeyCol
        FROM dbo.BaseTable
    ) AS Src
    CROSS APPLY (
        SELECT
            PrimaryKeyCol,
            DENSE_RANK() OVER (ORDER BY PrimaryKeyCol) AS ForeignKeyRank,
            DataCol
        FROM dbo.BaseTable AS B
        WHERE B.ForeignKeyCol = Src.ForeignKeyCol
    ) AS X
    

提交回复
热议问题