PERF: Add index `id DESC, baked_version` ON `posts`.

A scheduled job runs `Post.rebake_old` with a limit of 80 which does a
look up for the latest posts that have not been baked to the latest
version. Before this commit, the query would run using the primary key's
index and does a reverse scan. However the query performance quicky
becomes bad as more and more of the latest posts have been baked to the
latest version.
This commit is contained in:
Guo Xiang Tan 2019-04-08 15:29:02 +08:00
parent ca57e18f42
commit 4791d992dc
1 changed files with 7 additions and 0 deletions

View File

@ -0,0 +1,7 @@
class AddIndexIdBakedVersionOnPosts < ActiveRecord::Migration[5.2]
def change
add_index :posts, [:id, :baked_version],
order: { id: :desc },
where: "(deleted_at IS NULL)"
end
end