From 67d7123e47862eccd54c9acb57253125f498d8ae Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 26 Dec 2025 18:21:06 +0100 Subject: [PATCH] Add composite index to points on user_id and timestamp --- app/controllers/api/v1/points_controller.rb | 1 + ...dd_composite_index_to_points_user_id_timestamp.rb | 12 ++++++++++++ db/schema.rb | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20251226170919_add_composite_index_to_points_user_id_timestamp.rb diff --git a/app/controllers/api/v1/points_controller.rb b/app/controllers/api/v1/points_controller.rb index 1595d326..8c5424be 100644 --- a/app/controllers/api/v1/points_controller.rb +++ b/app/controllers/api/v1/points_controller.rb @@ -13,6 +13,7 @@ class Api::V1::PointsController < ApiController points = current_api_user .points + .without_raw_data .where(timestamp: start_at..end_at) # Filter by geographic bounds if provided diff --git a/db/migrate/20251226170919_add_composite_index_to_points_user_id_timestamp.rb b/db/migrate/20251226170919_add_composite_index_to_points_user_id_timestamp.rb new file mode 100644 index 00000000..49fb0224 --- /dev/null +++ b/db/migrate/20251226170919_add_composite_index_to_points_user_id_timestamp.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class AddCompositeIndexToPointsUserIdTimestamp < ActiveRecord::Migration[8.0] + disable_ddl_transaction! + + def change + add_index :points, %i[user_id timestamp], + order: { timestamp: :desc }, + algorithm: :concurrently, + if_not_exists: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 0968224f..089b01c7 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[8.0].define(version: 2025_12_10_193532) do +ActiveRecord::Schema[8.0].define(version: 2025_12_26_170919) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "postgis" @@ -249,6 +249,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_12_10_193532) do t.index ["user_id", "country_name"], name: "idx_points_user_country_name" t.index ["user_id", "reverse_geocoded_at"], name: "index_points_on_user_id_and_reverse_geocoded_at", where: "(reverse_geocoded_at IS NOT NULL)" t.index ["user_id", "timestamp", "track_id"], name: "idx_points_track_generation" + t.index ["user_id", "timestamp"], name: "index_points_on_user_id_and_timestamp", order: { timestamp: :desc } t.index ["user_id"], name: "index_points_on_user_id" t.index ["visit_id"], name: "index_points_on_visit_id" end