Skip to content
Advertisement

Rewrite SQL query to remove duplicate SELECTs in [WHERE xxx IN] condition

I need to execute following query:

What is bothering me – is that it has to select same values 3 times. I am sure that there is a better, proper way of doing it. Could you please suggest how this query might be rewritten?

Advertisement

Answer

You can use an EXISTS condition with an IN:

Which is more or less the same as:


However, the majority of the time will be spent by the actual DELETE part, rather than by finding the rows. So unless missing is really huge or a really complicated subquery, I doubt you will see a big performance difference.


After a few simple tests (250000 rows in notifications, 10000 rows in missing) it seems that the original version is way faster than the EXISTS or USING alternative.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement