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

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

    Try to add some indexes to your table. Specifically ones that cover the conditions in your where clause. Most likely it is now doing a table scan to pull the data back which can be very very slow.

    Also you might want to use the Include Actual Execution Plan button in management studio to show how it's going about determining which records you get.

    UPDATE

    From one of your comments it sounds like this query is pulling from a temp table. In that case after creating the table apply indexes to it. Adding the indexes then running the queries will be faster than running a table scan on a 500k row temp table.

提交回复
热议问题