How bad is IN operator for SQL query perfomance?

前端 未结 5 569
你的背包
你的背包 2021-01-16 05:05

I had SQL query that was taking 9 hours for execution. See below:

Select Field1, Field2
From A
Where Field3 IN (45 unique values here) 

Whe

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 05:40

    Two possibilities that I can see:

    1) There might be a billion records with Field3 = 10001 so that'll be really slow. And there might be no records with the other values, so that would be very fast.

    2) the issue could be the method that the DB uses to run the query. for example there may be a cut off point where it will probably switch from using the index to a full table scan. the optimizer isn't always right. when it's wrong you have to dance around it.

提交回复
热议问题