@nikita Not at all. I was just curious if MemSQL had any different recommendations.
Would you just delete ~900 million rows in a single statement?
My first thought was to use multiple DELETE statements with a LIMIT of 10,000 rows each with autocommit enabled (default).
The smaller transactions has always been advised when I started working with SQL databases. Probably because of smaller “chunks” in the transaction logs?
1 million rows at a time was apparently too much. The two 4 vCPU leafs increased to 95-100% CPU usage and caused all queries to run slow.
It took 4+ hours after stopping the DELETE statements before the CPU returned to the normal. Why is this? Some background mergers?
Actually, after watching the CPU usage from the yesterday’s 10,000 rows limit, the situation seemed to be the same. Increased CPU usage around 80-100%. No users was using the system at the time, so not sure how it affected queries.
Technique I have used in the past when the amount of data to delete is greater than the data I want to keep is to insert the data i want to keep into a new table and swap
Sometimes I’ll use a status flag to remove rows from consideration in transactions, then just schedule a job to periodically delete 50-100k at a time throughout the day. Minimizes impact.
For more time sensitive deletes, I wrote a simple looping delete procedure where you pass in the table and the LIMIT and it just loops until the tables is empty. I suppose you could add an additional parameter to drive a WHERE statement. It is dynamic sql. I would really like the ability to add a “SLEEP” call between the loops to let the background processes catch up.
I don’t write the prettiest code, so be kind.
call LoopingDelete (‘schema.table_name’,30000)
DELIMITER //
CREATE PROCEDURE LoopingDelete(p_tablename varchar(100) , pn_delete_limit bigint(21) ) RETURNS void AS
Declare
v_delete_limit text = cast(pn_delete_limit as char);
q_delete_rows text;
q_record_counts text;
v_cnt bigint(21);
BEGIN
#####Define Dynamic Query#######
q_delete_rows = 'Delete from '|| p_tablename || ’ limit ’ || pn_delete_limit;
q_record_counts = ’ select count(1) cnt from '||p_tablename;