From bf69893f8750563e3997af9a32341537a3c50b8c Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 2 Apr 2024 13:57:34 -0500 Subject: [PATCH] add index by user_id on classifications (#56) --- ...6_add_index_by_user_id_for_classifications.rb | 16 ++++++++++++++++ db/schema.rb | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20240328183306_add_index_by_user_id_for_classifications.rb diff --git a/db/migrate/20240328183306_add_index_by_user_id_for_classifications.rb b/db/migrate/20240328183306_add_index_by_user_id_for_classifications.rb new file mode 100644 index 0000000..666bc7b --- /dev/null +++ b/db/migrate/20240328183306_add_index_by_user_id_for_classifications.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Timescale currently does not support creating indexes concurrently. See: https://github.com/timescale/timescaledb/issues/504 +# This means that we cannot avoid write locks. +# Timescale does offer adding indexes on a transaction per chunk basis. +# See: https://docs.timescale.com/api/latest/hypertable/create_index/ for more details. + +class AddIndexByUserIdForClassifications < ActiveRecord::Migration[7.0] + disable_ddl_transaction! + + def change + execute <<~SQL + CREATE INDEX index_classification_events_on_user_id ON classification_events(user_id) WITH (timescaledb.transaction_per_chunk); + SQL + end +end diff --git a/db/schema.rb b/db/schema.rb index 6e6433d..612a347 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_11_06_170457) do +ActiveRecord::Schema[7.0].define(version: 2024_03_28_183306) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "timescaledb" @@ -29,6 +29,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["event_time"], name: "classification_events_event_time_idx", order: :desc + t.index ["user_id"], name: "index_classification_events_on_user_id" end create_table "classification_user_groups", primary_key: ["classification_id", "event_time", "user_group_id", "user_id"], force: :cascade do |t|