fix: Add account_id to the query to force using a better index (#12420)

I've added the account_id filter to the
`get_agent_ids_over_assignment_limit` method. This optimization will
help the query leverage the existing composite index
`conv_acid_inbid_stat_asgnid_idx (account_id, inbox_id, status,
assignee_id)` for better performance.

**Before:**
```sql
HashAggregate (cost=224238.12..224256.27 rows=484 width=4)
Group Key: assignee_id
Filter: (count(*) >= 10)
-> Index Scan using index_conversations_on_inbox_id on conversations (cost=0.44..223963.67 rows=54891 width=4)
Index Cond: (inbox_id = ???)
Filter: (status = 0)
```

**After:**
```sql
GroupAggregate (cost=0.44..5688.30 rows=476 width=4)
Group Key: assignee_id
Filter: (count(*) >= 10)
-> Index Only Scan using conv_acid_inbid_stat_asgnid_idx on conversations (cost=0.44..5640.81 rows=5928 width=4)
Index Cond: ((account_id = ??) AND (inbox_id = ??) AND (status = 0))
```
This commit is contained in:
Pranav
2025-09-11 22:27:38 -07:00
committed by GitHub
parent de5fb7a405
commit 16b98b6017
2 changed files with 10 additions and 4 deletions

View File

@@ -22,7 +22,13 @@ module Enterprise::Inbox
end
def get_agent_ids_over_assignment_limit(limit)
conversations.open.select(:assignee_id).group(:assignee_id).having("count(*) >= #{limit.to_i}").filter_map(&:assignee_id)
conversations
.open
.where(account_id: account_id)
.select(:assignee_id)
.group(:assignee_id)
.having("count(*) >= #{limit.to_i}")
.filter_map(&:assignee_id)
end
def ensure_valid_max_assignment_limit