创建索引而不锁定数据库
我有一个包含 10 多万行的表。我需要在单个列上创建索引,但是,创建索引需要很长时间,以至于我获得了针对表的锁定。
可能需要注意的是,索引是作为“rake db:migrate”步骤的一部分创建的......如果可行的话,我并不反对手动创建索引。
更新:我想我应该提到这是一个经常写入的表。
I have a table with 10+ million rows. I need to create an index on a single column, however, the index takes so long to create that I get locks against the table.
It may be important to note that the index is being created as part of a 'rake db:migrate' step... I'm not adverse to creating the index manually if that will work.
UPDATE: I suppose I should have mentioned that this a write often table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL NDBCLUSTER引擎可以在线创建索引,而无需锁定对表的写入。然而,使用最广泛的InnoDB引擎并不支持此功能。另一个免费开源数据库 Postgres 支持“同时创建索引”。
MySQL NDBCLUSTER engine can create index online without locking the writes to the table. However, the most widely used InnoDB engine does not support this feature. Another free and open source DB Postgres supports 'create index concurrently'.
您可以使用类似以下内容(伪代码)来防止阻塞:
其中记录器可以是在常规使用中向表中添加行/更新的任何内容(例如:php 脚本)。这将设置一个临时表,以便在另一个表更新时使用。
you can prevent the blockage with something like this (pseudo-code):
Where logger would be whatever adds rows/updates to your table in regular use(ex.: php script). This will set up a temporary table to use while the other one updates.
尝试确保索引是在插入记录之前创建的。这样,索引也将在表填充期间被填充。虽然这需要更长的时间,但至少在 rake 任务完成后就可以开始了。
Try to make sure that the index is created before the records are inserted. That way, the index will also be filled during the population of the table. Although that will take longer, at least it will be ready to go when the rake task is done.