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

前端 未结 8 964
轮回少年
轮回少年 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

    Data transportation wise, you are correct in your thinking: less data, quicker completion time. However, usually that time is minimal, and most of the time is spent on the actual query processing.

    Look at it this way: If you were in a car lot, would it be easier to pick out all cars that were red, or all cars that were red, model year 2006, black interior, and had rubber floor mats?

    0 讨论(0)
  • 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')  
    
    0 讨论(0)
提交回复
热议问题