Skip to content

Instantly share code, notes, and snippets.

@somebody32
Last active April 25, 2017 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somebody32/11cc80527fd9bf35c059917afb7d4856 to your computer and use it in GitHub Desktop.
Save somebody32/11cc80527fd9bf35c059917afb7d4856 to your computer and use it in GitHub Desktop.
ActiveRecord::Base.transaction do
- # doing delete ... where task_id in (...) and subtask_id is null (inequality test on secondary key),
- # locks secondary indexes and primary key index
- AssignedUser.where(task_id: task.id, subtask_id: nil).delete_all
+ assigned_user_ids_to_clean = AssignedUser.where(task_id: task.id, subtask_id: nil).pluck(:id)
+ # doing delete ... where id in (...) (equality test on primary key), locks only primary key index
+ AssignedUser.where(id: assigned_user_ids_to_clean).delete_all
task.assigned_user_ids = assigned_user_ids # inserts multiple records
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment