Implementing search functionality with multiple optional parameters against database table

前端 未结 3 700
离开以前
离开以前 2021-01-02 12:54

I would like to check if there is a preferred design pattern for implementing search functionality with multiple optional parameters against database table where the access

3条回答
  •  余生分开走
    2021-01-02 13:26

    I posted this as a comment, but I realized it should probably be an answer.

    It's not good to write predicates as WHERE @Param IS NULL OR Column = @Param, because the optimizer usually considers it to be non-sargable.

    Try this experiment: Take your most populated table, and try to query just for the Primary Key field, which should be your clustered index:

    DECLARE @PrimaryKey int
    SET @PrimaryKey = 1
    
    SELECT CoveredColumn
    FROM Table
    WHERE @PrimaryKey IS NULL
    OR PrimaryKeyColumn = @PrimaryKey
    
    SELECT CoveredColumn
    FROM Table
    WHERE PrimaryKeyColumn >= ISNULL(@PrimaryKey, 0)
    AND PrimaryKeyColumn <= ISNULL(@PrimaryKey, 2147483647)
    

    Both of these SELECT statements will produce identical results, assuming that the PK column is a non-negative int. But pull up the execution plan for this and you'll see a huge difference in cost. The first SELECT does a full index scan and typically takes up about 90% of the query cost.

    When you want to have optional search conditions in SQL, and you can't use dynamic SQL, it's best for performance if you can turn it into a range query instead using ISNULL. Even if the range is huge (literally half the range of an int here), the optimizer will still figure it out when the optional parameter is used.

提交回复
热议问题