Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Efficient way to check if ID of foreign key is still referenced?

Context: I have this table called product and I am writing a script to clean up some old products, but have to follow rules, which can be deleted. One of the rules, is to only delete it if its not referenced by some other specific tables.

So currently I find all the products (using other simler rules) and then try to filter that IDs list on all referenced products like:

SELECT id FROM product
WHERE 
  id NOT IN (SELECT product_id FROM sale_line)
  AND id NOT IN (SELECT product_id FROM purchase_line)

On smaller number of products, it works. But in real scenario, there is over a million rows. And this query just takes forever.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any more efficient way to determine if table row is referenced on some other specific tables, before trying to delete it?

>Solution :

Typically NOT EXISTS is faster than NOT IN:

SELECT p.id 
FROM product p
WHERE NOT EXISTS (select * from sale_line sl where sl.product_id = p.id)
  AND NOT EXISTS (select * from purchase_line pl where pl.product_id = p.id)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading