-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
feat(perf): update query to reduce N+1 impact [CW-1926] #7228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ Deploy Preview for chatwoot-storybook canceled.
|
| elsif conversation.unread_incoming_messages.count.zero? | ||
| json.messages [conversation.messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)] | ||
| else | ||
| json.messages conversation.unread_messages.includes([:user, { attachments: [{ file_attachment: [:blob] }] }]).last(10).map(&:push_event_data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not useful, so had to remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check all the pages, may be add backend/frontend specs if there are any for JSON expectations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked those, and the response will be the same, the changes only affect under-the-hood ORM operations. I tested a bunch of cases, and the existing unit tests cover most of the scenarios
app/finders/conversation_finder.rb
Outdated
| @conversations = @conversations.includes( | ||
| :taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox | ||
| ) | ||
| @conversations = @conversations.includes(:taggings, { inbox: [:channel] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's still a N+1 query
GET /api/v1/accounts/1/conversations?status=open&assignee_type=all&page=1&sort_by=latest
USE eager loading detected
Message => [:sender]
Add to your query: .includes([:sender])
Call stack
/Users/shivammishra/Chatwoot/chatwoot/app/models/message.rb:160:in `merge_sender_attributes'
/Users/shivammishra/Chatwoot/chatwoot/app/models/message.rb:147:in `push_event_data'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/conversations/partials/_conversation.json.jbuilder:25:in `map'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/conversations/partials/_conversation.json.jbuilder:25:in `_app_views_api_v__conversations_partials__conversation_json_jbuilder__1828115892073730114_1685640'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/accounts/conversations/index.json.jbuilder:10:in `block (3 levels) in _app_views_api_v__accounts_conversations_index_json_jbuilder__1868484514649990367_1681860'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/accounts/conversations/index.json.jbuilder:9:in `block (2 levels) in _app_views_api_v__accounts_conversations_index_json_jbuilder__1868484514649990367_1681860'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/accounts/conversations/index.json.jbuilder:8:in `block in _app_views_api_v__accounts_conversations_index_json_jbuilder__1868484514649990367_1681860'
/Users/shivammishra/Chatwoot/chatwoot/app/views/api/v1/accounts/conversations/index.json.jbuilder:1:in `_app_views_api_v__accounts_conversations_index_json_jbuilder__1868484514649990367_1681860'
/Users/shivammishra/Chatwoot/chatwoot/app/controllers/concerns/switch_locale.rb:24:in `set_locale'
/Users/shivammishra/Chatwoot/chatwoot/app/controllers/concerns/switch_locale.rb:16:in `switch_locale_using_account_locale'
/Users/shivammishra/Chatwoot/chatwoot/app/controllers/concerns/request_exception_handler.rb:11:in `handle_with_exception'
/Users/shivammishra/Chatwoot/chatwoot/app/controllers/concerns/switch_locale.rb:24:in `set_locale'
/Users/shivammishra/Chatwoot/chatwoot/app/controllers/concerns/switch_locale.rb:11:in `switch_locale'
Wasn't able to solve this event after adding the :sender to messages either here or in the jbuilder. @sojan-official can you give it a shot?
fixes the error: ActiveRecord::EagerLoadPolymorphicError Exception: Cannot eagerly load the polymorphic association :channel
app/finders/conversation_finder.rb
Outdated
| end | ||
|
|
||
| def conversations | ||
| @conversations = @conversations.includes(:taggings, { inbox: [:channel] }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to remove this, was getting an error in the tests (See previous commit). Will try to fix this.
ActiveRecord::EagerLoadPolymorphicError Exception: Cannot eagerly load the polymorphic association :channel
CC: @sojan-official
app/finders/conversation_finder.rb
Outdated
| def conversations | ||
| @conversations = @conversations.includes(:taggings, :inbox, | ||
| { assignee: [{ account_users: [:account] }, { avatar_attachment: [:blob] }] }, | ||
| { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox, { messages: [:sender] }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to remove another one for the tests
|
Had to remove a bunch of pre-fetches, but still shows some performance improvement |
|
@scmmishra LGTM, one small suggestions on specs. |
Addressed in comments |
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR reduces the impact on N+1 queries in the conversation list view. Reduces the number of queries while rendering the template by about 15%
Type of change
How Has This Been Tested?
After Fix
Before Fix