Loading changelogs/unreleased/ab-44446-add-indexes-for-user-activity-queries.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line --- title: Add indexes for user activity queries. merge_request: 17890 author: type: performance db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb 0 → 100644 +40 −0 Original line number Diff line number Diff line class AddIndexesForUserActivityQueries < ActiveRecord::Migration include Gitlab::Database::MigrationHelpers DOWNTIME = false disable_ddl_transaction! def up add_concurrent_index :events, [:author_id, :project_id] unless index_exists?(:events, [:author_id, :project_id]) add_concurrent_index :user_interacted_projects, :user_id unless index_exists?(:user_interacted_projects, :user_id) end def down remove_concurrent_index :events, [:author_id, :project_id] if index_exists?(:events, [:author_id, :project_id]) patch_foreign_keys do remove_concurrent_index :user_interacted_projects, :user_id if index_exists?(:user_interacted_projects, :user_id) end end private def patch_foreign_keys return yield if Gitlab::Database.postgresql? # MySQL doesn't like to remove the index with a foreign key using it. remove_foreign_key :user_interacted_projects, :users if fk_exists?(:user_interacted_projects, :user_id) yield # Let's re-add the foreign key using the existing index on (user_id, project_id) add_concurrent_foreign_key :user_interacted_projects, :users, column: :user_id unless fk_exists?(:user_interacted_projects, :user_id) end def fk_exists?(table, column) foreign_keys(table).any? do |key| key.options[:column] == column.to_s end end end db/schema.rb +3 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20180309160427) do ActiveRecord::Schema.define(version: 20180320182229) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" Loading Loading @@ -728,6 +728,7 @@ end add_index "events", ["action"], name: "index_events_on_action", using: :btree add_index "events", ["author_id", "project_id"], name: "index_events_on_author_id_and_project_id", using: :btree add_index "events", ["author_id"], name: "index_events_on_author_id", using: :btree add_index "events", ["project_id", "id"], name: "index_events_on_project_id_and_id", using: :btree add_index "events", ["target_type", "target_id"], name: "index_events_on_target_type_and_target_id", using: :btree Loading Loading @@ -1856,6 +1857,7 @@ end add_index "user_interacted_projects", ["project_id", "user_id"], name: "index_user_interacted_projects_on_project_id_and_user_id", unique: true, using: :btree add_index "user_interacted_projects", ["user_id"], name: "index_user_interacted_projects_on_user_id", using: :btree create_table "user_synced_attributes_metadata", force: :cascade do |t| t.boolean "name_synced", default: false Loading Loading
changelogs/unreleased/ab-44446-add-indexes-for-user-activity-queries.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line --- title: Add indexes for user activity queries. merge_request: 17890 author: type: performance
db/migrate/20180320182229_add_indexes_for_user_activity_queries.rb 0 → 100644 +40 −0 Original line number Diff line number Diff line class AddIndexesForUserActivityQueries < ActiveRecord::Migration include Gitlab::Database::MigrationHelpers DOWNTIME = false disable_ddl_transaction! def up add_concurrent_index :events, [:author_id, :project_id] unless index_exists?(:events, [:author_id, :project_id]) add_concurrent_index :user_interacted_projects, :user_id unless index_exists?(:user_interacted_projects, :user_id) end def down remove_concurrent_index :events, [:author_id, :project_id] if index_exists?(:events, [:author_id, :project_id]) patch_foreign_keys do remove_concurrent_index :user_interacted_projects, :user_id if index_exists?(:user_interacted_projects, :user_id) end end private def patch_foreign_keys return yield if Gitlab::Database.postgresql? # MySQL doesn't like to remove the index with a foreign key using it. remove_foreign_key :user_interacted_projects, :users if fk_exists?(:user_interacted_projects, :user_id) yield # Let's re-add the foreign key using the existing index on (user_id, project_id) add_concurrent_foreign_key :user_interacted_projects, :users, column: :user_id unless fk_exists?(:user_interacted_projects, :user_id) end def fk_exists?(table, column) foreign_keys(table).any? do |key| key.options[:column] == column.to_s end end end
db/schema.rb +3 −1 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 20180309160427) do ActiveRecord::Schema.define(version: 20180320182229) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" Loading Loading @@ -728,6 +728,7 @@ end add_index "events", ["action"], name: "index_events_on_action", using: :btree add_index "events", ["author_id", "project_id"], name: "index_events_on_author_id_and_project_id", using: :btree add_index "events", ["author_id"], name: "index_events_on_author_id", using: :btree add_index "events", ["project_id", "id"], name: "index_events_on_project_id_and_id", using: :btree add_index "events", ["target_type", "target_id"], name: "index_events_on_target_type_and_target_id", using: :btree Loading Loading @@ -1856,6 +1857,7 @@ end add_index "user_interacted_projects", ["project_id", "user_id"], name: "index_user_interacted_projects_on_project_id_and_user_id", unique: true, using: :btree add_index "user_interacted_projects", ["user_id"], name: "index_user_interacted_projects_on_user_id", using: :btree create_table "user_synced_attributes_metadata", force: :cascade do |t| t.boolean "name_synced", default: false Loading