Skip to content
Advertisement

How to speed up sql query execution?

The task is to execute the sql query:

The subquery contains about 1000 id so it takes a long time. Maybe this question was already there, but how can I speed it up? (if it is possible to speed up please write for PL SQL and T-SQL or at least one of them).

Advertisement

Answer

I would start by rewriting the in condition to exists:

Then, consider an index on x(user, id) – or x(id, user) (you can try both and see if one offers better improvement that the other).

Another possibility is to use window functions:

This might, or might not, perform better than the not exists solution, depending on various factors.

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