简体   繁体   English

Rails Migrations MySQL 算法::并发替代

[英]Rails Migrations MySQL algorithm: :concurrently alternative

I have this rails migration, I'm adding an index to a very large table and am aware of the fact that introducing a migration that would lock the table and potentially block build processing on Semaphore is quite risky.我有这个 Rails 迁移,我正在向一个非常大的表添加一个索引,并且我知道引入一个会锁定表并可能阻止信号量上的构建处理的迁移是非常危险的。 So I used the safe route, and triggered a concurrent index build instead所以我使用了安全路线,而是触发了并发索引构建

class AddIndexToEventsTable < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

def change
  add_index :events, [:status, :created_at], algorithm: :concurrently
end
end

but after migrating, it turns out to be unsuccessful here's the error:但是迁移后发现不成功,错误如下:

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Algorithm must be one of the following: :default, :copy, :inplace

Im using rails 5.2.5我正在使用rails 5.2.5

How can I replicate the functionality algorithm: :concurrently has with PostGres with MYSQL.我如何复制功能algorithm: :concurrently

To make sure you don't have any locks the option you want is为了确保您没有任何锁,您想要的选项是

LOCK=NONE

Sadly I do not believe rails migrations have support for this option.遗憾的是,我不相信 rails 迁移支持此选项。 One possible solution is to manually build the SQL and run it with execute.一种可能的解决方案是手动构建 SQL 并使用执行运行它。

An example can be seen below:一个例子可以在下面看到:

class AddIndexToEventsTable < ActiveRecord::Migration[5.2]
disable_ddl_transaction!

 def change
    execute <<~SQL
      ALTER TABLE events
      ADD INDEX index_events_on_status_created_at(status, created_at),
      ALGORITHM=DEFAULT,
      LOCK=NONE;
    SQL
 end
end

Create a migration like this one:创建这样的迁移:

add_index(:events, [:status, :created_at], {algorithm: :inplace, lock: :none})

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

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