Add composite index to points on user_id and timestamp

This commit is contained in:
Eugene Burmakin 2025-12-26 18:21:06 +01:00
parent 573d527455
commit 67d7123e47
3 changed files with 15 additions and 1 deletions

View file

@ -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

View file

@ -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

3
db/schema.rb generated
View file

@ -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