1

I need to delete records from one table based on inner join condition on other two tables, however query runs for ages.

DELETE FROM public.blacklist WHERE subject_id NOT IN(
SELECT DISTINCT a.id FROM public.subject a
INNER JOIN stg.blacklist_init b ON a.subject_id=b.customer_code);

Any ideas how to achieve this?

Thank you.

1 Answer 1

1

You can use NOT EXISTS instead of NOT IN, and I think you don't need a DISTINCT

DELETE FROM public.blacklist bl
 WHERE NOT EXISTS  (
                     SELECT 0 
                       FROM public.subject a
                      INNER JOIN stg.blacklist_init b 
                         ON a.subject_id=b.customer_code
                      WHERE a.id = bl.subject_id 
                     );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.