I’m trying to alter 2000 tables by adding a column to each. I achieve this by using a for loop in C# and executing a query on each table name for a list of strings I have. It starts out fast but then gets slower and slower. The memory for mysql.exe increases from 50k to 375k in about 30 seconds. Why is it doing this? Once I do one alter table the memory should drop down again. Is some resource not being disposed of after each alter? A cache, or buffer? Garbage collection? I can’t finish this for loop because it gets incredible slow after the 20th-25th alter table.
Share
When you issue a
alter tablestatement to MySQL, it starts rebuilding the table.It does the following steps:
oldtable to a new file.newfile definition .frm-filenew.myd-datafile, this requires rewriting the entire datafile.newdatafile to fill the new column with the default valueolddatafilenewdatafile so it has the same name theoldfile had.olddatafileFor performance reasons it will try to keep the
newdata and index file in memory.This is the memory increase you are seeing.
MySQL will keep the table data in memory for some time and will only clear out the cache if the memory assigned to cache space in
My.inihas been exhausted.You can clear the cache by issuing:
After every
alter tablestatement.See: http://dev.mysql.com/doc/refman/5.0/en/query-cache-status-and-maintenance.html
Read up on MySQL cache issues here: http://www.docplanet.org/mysql/mysql-query-cache-in-depth/