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

MYSQL delete from large table increase the delete time for each transaction

I am trying to delete data from table which contains almost 6,000,000,000 records , with where clause.

here is the stored procedure I am using and running from command prompt MySQL in windows.

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `clean_table`( )
BEGIN   
    REPEAT
        DO SLEEP(1);
        DELETE FROM tablename
        WHERE exportInfoId<=8479  limit 100000;   
        SELECT ROW_COUNT();
    UNTIL ROW_COUNT() = 0 END REPEAT;
END$$
DELIMITER ;

Its deleting the data but it continue incasing the time for each delete transaction. Why its keep increasing , even when with delete the earlier transaction reduce the data in table ? Is there any way to make it same time for each transaction ? I am already using sleep as suggested in some other answers .

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

enter image description here

>Solution :

You need to add an index to the column with which you are using to find the record(s) to be deleted.

With an index, MySQL knows exactly where the records are to be found so it can go straight to the record(s).

Without an index, then the table must be searched row by row.

The difference is, without an index the deletes are performed in the order of the primary key however, with an index the records will be searched in the order of that particular column.

Adding an index to the column is the correct answer here, there really is no other answer.

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