performance - MySQL updates getting very slow towards end of the table -
i have table "data" holds around 100,000,000 records. have added new column "batch_id" (integer).
on application layer, i'm updating batch_id in batches of 10,000 records each of 100,000,000 records (the batch_id same 10k).
i'm doing (application layer pseudo code):
loop { $batch_id = $batch_id + 1; mysql.query("update data set batch_id='$batch_id' batch_id null limit 10000"); }
i have index on batch_id column.
in beginning, update statement took ~30 seconds. i'm halfway through table , it's getting slower , slower. @ moment same statement takes around 10 minutes(!). reached point no longer feasible take on month update whole table @ current speed.
what speed up, , why mysql getting slower towards end of table? index on primary key help?
is primary key automatically indexed in mysql? answer yes
so instead 1 index batch_id
help.
the problem without index engine full table scan. @ first easy find 10k null values, when more , more records updated engine have scan more find nulls.
but should easier create batch_id
autonumeric column
other option: create new table , add index , replace old table.
create newtable select if(@newid := @newid + 1, @newid div 10000, @newid div 10000) batch_id, <other fields> yourtable cross join (select @newid :=0 ) v
Comments
Post a Comment