Skip to content

Conversation

@scmmishra
Copy link
Member

@scmmishra scmmishra commented May 31, 2023

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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

After Fix

CleanShot 2023-06-01 at 12 11 57@2x

Before Fix

CleanShot 2023-05-31 at 17 56 34@2x

@netlify
Copy link

netlify bot commented May 31, 2023

Deploy Preview for chatwoot-storybook canceled.

Name Link
🔨 Latest commit 080dc53
🔍 Latest deploy log https://app.netlify.com/sites/chatwoot-storybook/deploys/64784136c7a1490008d25a25

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)
Copy link
Member Author

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

Copy link
Contributor

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?

Copy link
Member Author

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

@conversations = @conversations.includes(
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox
)
@conversations = @conversations.includes(:taggings, { inbox: [:channel] },
Copy link
Member Author

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?

@scmmishra scmmishra changed the title feat: update query to reduce N+1 impact feat(perf): update query to reduce N+1 impact May 31, 2023
@scmmishra scmmishra changed the title feat(perf): update query to reduce N+1 impact feat(perf): update query to reduce N+1 impact [CW-1926] May 31, 2023
@scmmishra scmmishra changed the title feat(perf): update query to reduce N+1 impact [CW-1926] feat(perf): update query to reduce N+1 impact [CW-1926, CW-1605] May 31, 2023
@scmmishra scmmishra changed the title feat(perf): update query to reduce N+1 impact [CW-1926, CW-1605] feat(perf): update query to reduce N+1 impact [CW-1926] May 31, 2023
@scmmishra scmmishra requested a review from sojan-official May 31, 2023 12:31
fixes the error: ActiveRecord::EagerLoadPolymorphicError Exception: Cannot eagerly load the polymorphic association :channel
end

def conversations
@conversations = @conversations.includes(:taggings, { inbox: [:channel] },
Copy link
Member Author

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

def conversations
@conversations = @conversations.includes(:taggings, :inbox,
{ assignee: [{ account_users: [:account] }, { avatar_attachment: [:blob] }] },
{ contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox, { messages: [:sender] })
Copy link
Member Author

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

@scmmishra
Copy link
Member Author

Had to remove a bunch of pre-fetches, but still shows some performance improvement

@scmmishra scmmishra requested a review from tejaswinichile June 1, 2023 06:48
@tejaswinichile
Copy link
Contributor

@scmmishra LGTM, one small suggestions on specs.

@scmmishra
Copy link
Member Author

@scmmishra LGTM, one small suggestions on specs.

Addressed in comments

@scmmishra scmmishra merged commit f28533b into develop Jun 2, 2023
@scmmishra scmmishra deleted the feature/cw-1926 branch June 2, 2023 05:41
scmmishra added a commit that referenced this pull request Jun 8, 2023
scmmishra added a commit that referenced this pull request Jun 15, 2023
scmmishra added a commit that referenced this pull request Jun 16, 2023
@github-actions
Copy link

github-actions bot commented Jul 2, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants