简体   繁体   English

MySQL 改变表内存增加

[英]MySQL Alter Table Memory Increase

I'm trying to alter 2000 tables by adding a column to each.我试图通过向每个表添加一列来更改 2000 个表。 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.我通过在 C# 中使用 for 循环并对每个表名执行查询以获取我拥有的字符串列表来实现这一点。 It starts out fast but then gets slower and slower.它开始很快,但随后变得越来越慢。 The memory for mysql.exe increases from 50k to 375k in about 30 seconds. mysql.exe 的内存在大约 30 秒内从 50k 增加到 375k。 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.我无法完成这个 for 循环,因为它在第 20-25 个更改表之后变得非常慢。

When you issue a alter table statement to MySQL, it starts rebuilding the table.当你向 MySQL 发出一个alter table语句时,它开始重建表。

It does the following steps:它执行以下步骤:

  1. Copy the old table to a new file.old表复制到新文件中。
  2. Add a column to the new file definition .frm-filenew文件定义 .frm-file 添加一列
  3. Add the column in the datafile in the new .myd-datafile, this requires rewriting the entire datafile.new .myd-datafile 中添加数据文件中的列,这需要重写整个数据文件。
  4. Go through all the rows in the new datafile to fill the new column with the default value遍历new数据文件中的所有行以使用默认值填充新列
  5. If the new column has an index MySQL has to do steps 2,3,4 for the .MYI-indexfile as well.如果新列有索引,MySQL 也必须对 .MYI-indexfile 执行步骤 2、3、4。
  6. Rename the old datafile重命名old数据文件
  7. Rename the new datafile so it has the same name the old file had.重命名new数据文件,使其具有与old文件相同的名称。
  8. Delete the old datafile删除old数据文件

For performance reasons it will try to keep the new data and index file in memory.出于性能原因,它将尝试将new数据和索引文件保存在内存中。
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.ini has been exhausted. MySQL 会将表数据保留在内存中一段时间​​,并且只有在My.ini分配给缓存空间的内存已耗尽时才会清除缓存。

You can clear the cache by issuing:您可以通过发出以下命令清除缓存:

RESET QUERY CACHE;

After every alter table statement.在每个alter table语句之后。

See: http://dev.mysql.com/doc/refman/5.0/en/query-cache-status-and-maintenance.html请参阅: 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/在此处阅读 MySQL 缓存问题: http : //www.docplanet.org/mysql/mysql-query-cache-in-depth/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM