简体   繁体   中英

How to delete heavy records from a table in the most performant way?

This is the case: I have a table with 16,000 rows, with a child table with 4,000,000 rows. The parent table has a column with a lot of data (it's a wkt, used for geometry). I need to cleanup the data periodically, and at this moment I need to delete 5685 parent rows along with 1,400,000 child rows. I'm struggling to write the most performant query to achieve this. My current method is this:

1) Get all the ids from the parent table from the rows that needs to be deleted.

SELECT Id, ValidTo From ParentTable Where ValidTo < someDate;

2) For each id I find I am executing following commands with:

DELETE FROM ChildTable WHERE ParentId = IdFromStepOne;

DELETE FROM ParentTable WHERE Id = IdFromStepOne

This is taking 15 minutes for 95-100 records, so it will be done in 14 hours.. Can this be written more performant? I'm coding in .Net Core and using Entitiy Framework for you information.

Thanks in advance!

You query shows that you are looping through each id and deleting the child & parent rows.

Use IN clause to perform it for multiple values.

    DELETE FROM ChildTable WHERE ParentId in (SELECT Id From ParentTable Where ValidTo < someDate)

    DELETE FROM ParentTable WHERE Id in (SELECT Id From ParentTable Where ValidTo < someDate)

As you need to delete rows in two tables, you will need 2 queries and the SELECT query doesn't need to select the ValidTo column but only the Id.

I would write these queries:

DELETE FROM ChildTable ct
WHERE EXISTS (SELECT pt.Id FROM ParentTable pt WHERE ct.Id_parent = pt.Id AND pt.ValidTo < someDate);

DELETE FROM ParentTable
WHERE ValidTo < someDate;

Using pl/sql you should be able to select the ParentTable's Id s to delete only one time.

Query1 => SELECT Id FROM ParentTable WHERE ValidTo < someDate
Query2 => DElETE FROM ChildTable WHERE id_parent IN [results of Query 1]
Query3 => DELETE FROM ParentTable WHERE Id IN [results of Query 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM