Compare commits

...
Author SHA1 Message Date
Pranav ac02a60707 Merge commit '5ba87a3d27a2284236d598d871eb46bec310f18d' into change-the-order-of-after_create_commit 2025-06-27 16:42:09 -07:00
Pranav 5ba87a3d27 disable ddl transaction on concurrent add_index 2025-06-27 16:31:22 -07:00
Pranav f73e328709 make it work 2025-06-27 16:24:36 -07:00
Pranav 5eb94fc545 Change the order of after create_commit 2025-06-27 15:51:30 -07:00
PranavandGitHub eea1ab3002 fix: Add composite index on messages for csat_metrics API performance (#11831)
This PR adds a composite index (:account_id, :content_type, :created_at)
on the table messages.

This index is added as a temporary fix for performance issues in the
CSAT responses controller where we query messages with account_id,
content_type and created_at. The current implementation
(account.message.input_csat.count) times out with millions of messages.

TODO: Create a dedicated csat_survey table and add entries when surveys
are sent, then query this table instead of the entire messages table for
better performance.
2025-06-27 15:48:04 -07:00
4 changed files with 30 additions and 4 deletions
+3 -1
View File
@@ -34,9 +34,11 @@ class Attachment < ApplicationRecord
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
].freeze
has_one_attached :file
belongs_to :account
belongs_to :message
has_one_attached :file
validate :acceptable_file
validates :external_url, length: { maximum: Limits::URL_LENGTH_LIMIT }
enum file_type: { :image => 0, :audio => 1, :video => 2, :file => 3, :location => 4, :fallback => 5, :share => 6, :story_mention => 7,
+3 -2
View File
@@ -24,6 +24,7 @@
#
# Indexes
#
# idx_messages_account_content_created (account_id,content_type,created_at)
# index_messages_on_account_created_type (account_id,created_at,message_type)
# index_messages_on_account_id (account_id)
# index_messages_on_account_id_and_inbox_id (account_id,inbox_id)
@@ -122,12 +123,12 @@ class Message < ApplicationRecord
belongs_to :conversation, touch: true
belongs_to :sender, polymorphic: true, optional: true
after_create_commit :execute_after_create_commit_callbacks
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
has_one :csat_survey_response, dependent: :destroy_async
has_many :notifications, as: :primary_actor, dependent: :destroy_async
after_create_commit :execute_after_create_commit_callbacks
after_update_commit :dispatch_update_event
def channel_token
@@ -0,0 +1,22 @@
class AddIndexToMessages < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def change
# This index is added as a temporary fix for performance issues in the CSAT
# responses controller where we query messages with account_id, content_type
# and created_at. The current implementation (account.message.input_csat.count)
# times out with millions of messages.
#
# TODO: Create a dedicated csat_survey table and add entries when surveys are
# sent, then query this table instead of the entire messages table for better
# performance.
return if index_exists?(
:messages,
[:account_id, :content_type, :created_at],
name: 'idx_messages_account_content_created'
)
add_index :messages, [:account_id, :content_type, :created_at],
name: 'idx_messages_account_content_created', algorithm: :concurrently
end
end
+2 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2025_06_20_120000) do
ActiveRecord::Schema[7.1].define(version: 2025_06_27_195529) do
# These extensions should be enabled to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -825,6 +825,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_06_20_120000) do
t.text "processed_message_content"
t.jsonb "sentiment", default: {}
t.index "((additional_attributes -> 'campaign_id'::text))", name: "index_messages_on_additional_attributes_campaign_id", using: :gin
t.index ["account_id", "content_type", "created_at"], name: "idx_messages_account_content_created"
t.index ["account_id", "created_at", "message_type"], name: "index_messages_on_account_created_type"
t.index ["account_id", "inbox_id"], name: "index_messages_on_account_id_and_inbox_id"
t.index ["account_id"], name: "index_messages_on_account_id"