#16049·chatwoot

perf: missing composite index for default last_activity_at sort on conversations table

Author: md-riazCreated Sep 28, 2026Updated Sep 29, 2026
LabelsperformanceBugBackend

Describe the bug

In accounts with a large number of conversations (e.g. 40,000+), loading the conversation list—especially in the "All" view or views without an inbox filter—causes severe database latency (200ms+ in PostgreSQL execution time per query). Under concurrent dashboard usage by multiple agents, this exhausts Puma worker threads and results in cascading 10s+ request timeouts across the entire UI.

Tracing EXPLAIN ANALYZE on PostgreSQL reveals that the database performs a Parallel Sequential Scan across the entire conversations table followed by an in-memory heap sort, because there is no composite index supporting (account_id, last_activity_at DESC NULLS LAST).


Context & Root Cause

  1. Previous Index Removal (PR #9475 / CW-3300): In migration 20240515201632_index_improvements_conversations_contacts.rb, the global single-column index index_conversations_on_last_activity_at was removed to improve filter planner performance:

    ruby
    remove_index :conversations, :last_activity_at
    add_index :contacts, [:account_id, :last_activity_at],
              order: { last_activity_at: 'DESC NULLS LAST' },
              algorithm: :concurrently,
              name: 'index_contacts_on_account_id_and_last_activity_at'

    While contacts received the account-scoped composite index [:account_id, :last_activity_at], conversations only had its index removed without an account-scoped replacement.

  2. Misconception in PR #15360 (CW-7888): When adding the [:account_id, :status, :created_at] index in PR #15360, the description stated:

    "The default last_activity_at sort and inbox-filtered views are unaffected (already indexed)."

    However, checking db/schema.rb reveals that last_activity_at does not appear in any index on the conversations table.

  3. Application Default Sort: In ConversationFinder (app/finders/conversation_finder.rb:207):

    ruby
    sort_by, sort_order = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['last_activity_at_desc']

    last_activity_at DESC is the default sorting behavior for all conversation views across Chatwoot.


To Reproduce

  1. Have an account with a significant number of conversations (e.g., 40k+).
  2. Open the dashboard and navigate to the "All" conversations tab (/api/v1/accounts/:id/conversations?status=all&assignee_type=all&page=1).
  3. Run EXPLAIN ANALYZE on the generated query:
    sql
    SELECT conversations.* FROM conversations 
    WHERE conversations.account_id = 1 
    ORDER BY conversations.last_activity_at DESC NULLS LAST 
    LIMIT 25 OFFSET 0;

Expected behavior

PostgreSQL should perform an Index Scan utilizing a composite index scoped by account_id and ordered by last_activity_at DESC NULLS LAST, returning the top 25 rows in < 1ms without scanning the entire table.


Query Plan Comparison

Without Index (Current Chatwoot Schema):

 Limit  (cost=5087.72..5090.64 rows=25 width=396) (actual time=194.689..203.554 rows=25 loops=1)
   ->  Gather Merge  (cost=5087.72..9327.45 rows=36338 width=396) (actual time=194.685..203.545 rows=25 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Sort  (cost=4087.70..4133.12 rows=18169 width=396) (actual time=131.500..131.507 rows=21 loops=3)
               Sort Key: last_activity_at DESC NULLS LAST
               Sort Method: top-N heapsort  Memory: 37kB
               ->  Parallel Seq Scan on conversations  (cost=0.00..3574.98 rows=18169 width=396) (actual time=0.024..123.920 rows=14563 loops=3)
                     Filter: (account_id = 1)
 Planning Time: 4.248 ms
 Execution Time: 203.831 ms

With Composite Index Added:

sql
CREATE INDEX CONCURRENTLY index_conversations_on_account_id_and_last_activity_at 
ON conversations (account_id, last_activity_at DESC NULLS LAST);
 Limit  (cost=0.29..6.05 rows=25 width=396) (actual time=0.089..0.162 rows=25 loops=1)
   ->  Index Scan using index_conversations_on_account_id_and_last_activity_at on conversations  (cost=0.29..10060.65 rows=43654 width=396) (actual time=0.086..0.156 rows=25 loops=1)
         Index Cond: (account_id = 1)
 Planning Time: 3.485 ms
 Execution Time: 0.272 ms

(Execution time drops from 204 ms to 0.27 ms — a ~750x speedup).


Proposed Solution

Add a concurrent migration matching the pattern used for contacts in PR #9475:

ruby
class AddAccountIdLastActivityAtIndexToConversations < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def change
    add_index :conversations, [:account_id, :last_activity_at],
              order: { last_activity_at: 'DESC NULLS LAST' },
              algorithm: :concurrently,
              name: 'index_conversations_on_account_id_and_last_activity_at',
              if_not_exists: true
  end
end

Environment

  • Deployment: Linux VM (Self-hosted)
  • Chatwoot Version: v4.14+ / master
  • Database: PostgreSQL 14+