group member count optimization

This commit is contained in:
2458773093
2019-07-22 03:46:00 +03:00
parent bd3f453c0d
commit bf2d54201b
7 changed files with 45 additions and 5 deletions

View File

@@ -0,0 +1,10 @@
class AddMemberCountToGroups < ActiveRecord::Migration[5.2]
def up
add_column :groups, :member_count, :integer
change_column_default :groups, :member_count, 0
end
def down
remove_column :groups, :member_count
end
end

View File

@@ -0,0 +1,10 @@
class BackfillAddMemberCountToGroups < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
Group.in_batches do |relation|
relation.update_all member_count: 0
sleep(0.1)
end
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_07_21_234917) do
ActiveRecord::Schema.define(version: 2019_07_22_003649) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -358,6 +358,7 @@ ActiveRecord::Schema.define(version: 2019_07_21_234917) do
t.boolean "is_archived", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "member_count", default: 0
t.index ["account_id"], name: "index_groups_on_account_id"
end