PERF: Add `index_for_rebake_old` to `posts`.

The index becomes smaller over time and is much faster.

Follow up to 4791d992dc.
This commit is contained in:
Guo Xiang Tan 2019-04-09 13:54:14 +08:00
parent 28d117898f
commit c82a929025
2 changed files with 23 additions and 0 deletions

View File

@ -20,6 +20,7 @@ class Post < ActiveRecord::Base
self.plugin_permitted_create_params = {}
# increase this number to force a system wide post rebake
# Recreate `index_for_rebake_old` when the number is increased
# Version 1, was the initial version
# Version 2 15-12-2017, introduces CommonMark and a huge number of onebox fixes
BAKED_VERSION = 2

View File

@ -0,0 +1,22 @@
class AddIndexForRebakeOldOnPosts < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def up
remove_index :posts, name: :index_posts_on_id_and_baked_version
add_index :posts, :id,
order: { id: :desc },
where: "(baked_version IS NULL OR baked_version < 2) AND deleted_at IS NULL",
name: :index_for_rebake_old,
algorithm: :concurrently
end
def down
remove_index :posts, name: :index_for_rebake_old
add_index :posts, [:id, :baked_version],
order: { id: :desc },
where: "(deleted_at IS NULL)",
algorithm: :concurrently
end
end