Question

I need an MySQL Skript which does the following:

delete chunks of the database until it has deleted all link_id's greater then 10000

exmaple:

x = 10000
DELETE FROM pligg_links WHERE link_id > x and link_id < x+10000
x = x + 10000
...

So it would delete

DELETE FROM pligg_links WHERE link_id > 10000 and link_id < 20000

then

DELETE FROM pligg_links WHERE link_id > 20000 and link_id < 30000

until all id's less then 10000 have been removed

I need this because the database is very very big (more then a gig)

thank in advance

Was it helpful?

Solution

what is the problem in ::

DELETE FROM pligg_links WHERE link_id > 10000

There is another way of doing it:(Just execute these 3 queries in order)

Step1 : Insert into pligg_links_temp select * from pligg_links where link_id < 10000;
Step2 : Drop pligg_links;
Step3 : Insert into pligg_links select * from pligg_links_temp

OTHER TIPS

You could use the LIMIT statement to regulate how many items to delete in one step:

DELETE FROM pligg_links
WHERE link_id > 10000
LIMIT 1000;

http://dev.mysql.com/doc/refman/5.1/en/delete.html says:

The MySQL-specific LIMIT row_count option to DELETE tells the server the maximum number of rows to be deleted before control is returned to the client. This can be used to ensure that a given DELETE statement does not take too much time. You can simply repeat the DELETE statement until the number of affected rows is less than the LIMIT value.

You could determine the number of deleted rows by using SELECT ROW_COUNT(); if you would like automatize deletion:

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top