Why do multiple WHERE conditions slow query rather than speed it up?

前端 未结 8 967
轮回少年
轮回少年 2021-02-04 04:35

The problem is that the query in question runs very slow when compared to the query run with one or two, rather than all three of its conditions.

Now the query.

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 05:27

    I suspect the Date field is not indexed, and without an index to rely on to filter the resultset before applying the where clause on the non-sargable columns, it gives them all equal weight and does not perform the quick filters first before applying the other more expensive clauses.

    When I am unable to tune the database using indexes, etc., I often find that re-writing the query similar to this is enough to direct the compiler to a more efficient query:

    Select Count(*)
    From (
        Select 1
        From SearchTable 
        Where [Zip] In (Select ZipCode from dbo.ZipCodesForRadius('30348', 150))
    ) 
    Where [Date] >= '8/1/2009' 
        AND FreeText([Description], 'keyword list here')  
    

提交回复
热议问题