How bad is IN operator for SQL query perfomance?

前端 未结 5 624
你的背包
你的背包 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:18

    First, make sure you have an index on your "Field3" in your table "A", and if possible include "Field1", "Field2" (covering index). You don't say which database you are running so it is hard to be precise in giving any better advice.

    If that still doesn't speed it up, try creating a temp table, then inserting all the "IN" values and then "INNER JOIN" or "WHERE EXISTS" that temp table to your table "A".

提交回复
热议问题