Hello Amazingly Smart People,
I did a search and found the answer to my question. However, the answer is excessively slow. I'm wondering if you know a better way.
Need: To delete records from TblA when the key value of the records does not appear in several related tables.
Solution:
DELETE * FROM tblA
WHERE value Not In (SELECT value FROM tblBSub)
AND value Not In (SELECT value FROM tblCSub)
AND value Not In (SELECT value FROM tblDSub)
AND value Not In (SELECT value FROM tblESub)
AND value Not In (SELECT value FROM tblFSub);
I'm dealing with only thousands and tens of thousands of records, but this takes many minutes to run. I haven't had the patience to wait beyond 5 or 6 minutes for it to complete. But I know it works by testing it on small test data sets first.
Another solution is to use a union query to create a single list of "value" from all the sub-tables. The union query runs very fast on its own. But when I place it into the Not In statement above the Delete query runs just as slow.
Another solution is to update a True/False field in tblA first, then delete records from tblA based on that field.
This runs MUCH faster - in a matter of 20 seconds or so - but it is a two step process ... first the update then the delete. I'm guessing the first two solutions are slower because using the IN approach may eliminate the benefits of indexing??
The update, then delete solution is not a problem, per se, but I'm wondering if anyone knows how to speed up the delete query itself. Is there better syntax that executes faster?
Thanks,
Joe
I did a search and found the answer to my question. However, the answer is excessively slow. I'm wondering if you know a better way.
Need: To delete records from TblA when the key value of the records does not appear in several related tables.
Solution:
DELETE * FROM tblA
WHERE value Not In (SELECT value FROM tblBSub)
AND value Not In (SELECT value FROM tblCSub)
AND value Not In (SELECT value FROM tblDSub)
AND value Not In (SELECT value FROM tblESub)
AND value Not In (SELECT value FROM tblFSub);
I'm dealing with only thousands and tens of thousands of records, but this takes many minutes to run. I haven't had the patience to wait beyond 5 or 6 minutes for it to complete. But I know it works by testing it on small test data sets first.
Another solution is to use a union query to create a single list of "value" from all the sub-tables. The union query runs very fast on its own. But when I place it into the Not In statement above the Delete query runs just as slow.
Another solution is to update a True/False field in tblA first, then delete records from tblA based on that field.
This runs MUCH faster - in a matter of 20 seconds or so - but it is a two step process ... first the update then the delete. I'm guessing the first two solutions are slower because using the IN approach may eliminate the benefits of indexing??
The update, then delete solution is not a problem, per se, but I'm wondering if anyone knows how to speed up the delete query itself. Is there better syntax that executes faster?
Thanks,
Joe