I have 50964218 records in a table. I am going to fetch the data from this table and insert into the same table. Which takes more time to manipulate. How optimize this query.
The Query is
INSERT INTO contacts_lists (contact_id, list_id, is_excluded, added_by_search) SELECT contact_id, 68114 , TRUE, added_by_search FROM contacts_lists cl1 WHERE list_id = 67579 AND is_excluded = TRUE AND NOT EXISTS (SELECT 1 FROM contacts_lists cl2 WHERE cl1.contact_id = cl2.contact_id AND cl2.list_id = 68114 ) index: list_id,contact_id