简体   繁体   English

如何在不影响生产性能的情况下将索引添加到18 GB innodb mysql表中?

[英]How to add index to 18 GB innodb mysql table without affecting production performance?

How can I add index to an 18 GB innodb mysql table without affecting the production performance? 如何在不影响生产性能的情况下为18 GB innodb mysql表添加索引? The table is frequently accessed, I tried altering the table just now and it turns up to have locked more than 200 queries out, and that's bad for performance. 这个表经常被访问,我刚试过改变表,结果已经锁定了200多个查询,这对性能有害。 Are there any other ways to do it? 有没有其他方法可以做到这一点?

Another option is to use pt-online-schema-change . 另一种选择是使用pt-online-schema-change It will create a copy of the old table with the new index and create trigger that will reflect all changes from the old table to the new one. 它将使用新索引创建旧表的副本,并创建触发器,该触发器将反映从旧表到新表的所有更改。 In end, it will change the name of the old table. 最后,它将更改旧表的名称。

TheOnly92 - there is another option, one that even amazon and ebay use. TheOnly92 - 还有另一个选项,即亚马逊和ebay使用的选项。 it's not considered 'bad' form to have infrequesnt maintenence periods where the site is unaccesible. 如果网站无法接入,则不会被认为是“不良”的形式。 on those occassions, you'll see a 404 maintenence page being displayed with a user friendly messge saying that the site is undergoing essential upgrades between the hours of .... etc 在那些场合,你会看到一个404维护页面正在显示一个用户友好的信息,说该网站正在进行必要的升级....

this might be the most pragmatic solution as creating this page will take you 5 mins, whereas the other option may take many hours to figure out and many more to implement. 这可能是最实用的解决方案,因为创建此页面将花费您5分钟,而另一个选项可能需要花费很多时间来确定,还有更多要实现。 also, as it would be infrequent, then it's unlikely that your users would be put off by such a message or period of downtime. 此外,由于这种情况很少发生,因此您的用户不太可能因此类消息或停机时间而被推迟。

jim 吉姆

It depends, how critical is it that you don't lose new records? 这取决于你不会丢失新记录的重要性如何?

Duplicate the table structure using CREATE TABLE ... LIKE ... , add the new index to the duplicate table, do a INSERT INTO ... SELECT ... FROM ... to grab all the data, then run a pair of ALTER s to rename the old table, then rename the new table to the old table's name. 使用CREATE TABLE ... LIKE ...复制表结构CREATE TABLE ... LIKE ... ,将新索引添加到重复表,执行INSERT INTO ... SELECT ... FROM ...以获取所有数据,然后运行一对ALTER要重命名旧表,然后将新表重命名为旧表的名称。

Unfortunately if any data in the old table changes between the time that the INSERT/SELECT runs and the tables get renamed, it may be lost. 不幸的是,如果旧表中的任何数据在INSERT / SELECT运行和重命名表之间发生变化,则可能会丢失。 It might be possible to get the data back using one of the Maatkit tools for table comparison. 可以使用Maatkit工具之一来获取数据以进行表比较。

Another common pattern relies on duplicate hardware. 另一种常见模式依赖于重复的硬件。 Set up a replication slave, add the index there, then make the slave the master. 设置复制从站,在那里添加索引,然后使从站成为主站。

Neither of these is likely to be fast, but they'll probably be faster than adding the index directly. 这些都不是很快,但它们可能比直接添加索引更快。 If you can afford the downtime, however, you really should just take the system down while you're altering/copying/switching slaves. 但是,如果您能够承受停机时间,那么在改变/复制/切换从站时,您真的应该关闭系统。 Not having to worry about getting the data back in sync will make your life easier. 不必担心让数据恢复同步会让您的生活更轻松。

(You may wish to consider switching to a database that lets you add indexes without locking the table .) (您可能希望考虑切换到允许您在不锁定表的情况下添加索引的数据库。)

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

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