Mar 6, 2014

In clause in SQL

If we execute the following with expected thousand rows in subquery result, it make takes a while (some minute):
DELETE FROM table 
WHERE row_id IN (
    SELECT ref_id IN ref_table 
    WHERE a_col = '..'
)
I discovered the reason for slowness: IN clause run ineffective with non indexed items So here is a solution:
CREATE TABLE tmp_ref AS
    SELECT ref_id IN ref_table 
    WHERE a_col = '..';

DELETE FROM table 
WHERE row_id IN (SELECT tmp_ref IN tmp_ref);

DROP TABLE tmp_ref;

No comments:

Post a Comment

New comment