views:

46

answers:

1

I have a query.

DELETE FROM A  
WHERE i NOT IN 
( SELECT i FROM B WHERE j = 1  
  UNION select i from C 
  UNION select i from D 
);

basically delete all rows in A where field i does not occur in tables B, C or D. If it was just

DELETE FROM A  
WHERE i NOT IN 
( SELECT i FROM B 
);

then that could be done easility with a left join

DELETE A FROM A 
LEFT JOIN B 
ON A.i = B.i
WHERE B.id is NULL;

( Assume that every table has a id field in the schema )

I guess my question is then does the above extend to the three table scenario with the following solution?

DELETE A FROM A 
LEFT JOIN B 
ON A.i = B.i AND B.j = 1
LEFT JOIN C
ON A.i = C.i 
LEFT JOIN D
ON A.i = D.i
WHERE B.id is NULL
AND   C.id is NULL
AND   D.id is NULL
A: 

Something like this:

DELETE 
  FROM A
 WHERE NOT EXISTS (
                   SELECT * 
                     FROM B
                    WHERE B.i = A.i
                  )
       AND NOT EXISTS (
                       SELECT * 
                         FROM C
                        WHERE C.i = A.i
                      )
       AND NOT EXISTS (
                       SELECT * 
                         FROM D
                        WHERE D.i = A.i
                      );
onedaywhen
That is generally always a bad idea in my experience. Inner selects where one of the conditions is dependant on the outer table are very slow.
bradgonesurfing
@bradgonesurfing: you haven't posted schema nor data so I cannot test it. Have you...?
onedaywhen
Is it not a self explanatory schema?Each table has two fieldsid - primary keyi - integer
bradgonesurfing
I assume by 'very slow' we are talking about a potential performance issue. To test this properly I'd need to know the other constraints (because optimizers may use them as 'hints'), data types, storage engine, indexes and, most crucially, the actual data involved. In other words, I have no hope of challenging you on your "generally always a bad idea in my experience" other than to say, "But have you actually tested it?" (and to point out that "generally always" would seem a contradiction :)
onedaywhen