Skip to content

feat(perf): contact page loading speed - #7629

Merged
scmmishra merged 3 commits into
developfrom
feature/cw-2184
Jul 27, 2023
Merged

scmmishra merged 3 commits into
developfrom
feature/cw-2184

Conversation

@scmmishra

Copy link
Copy Markdown
Member

This PR adds a partial index to the contacts very similar to the one added here: #7175

The main difference is the fields on which this index operates, in this case we only index the account_id field, this drastically helps in building the contact list query when loading the contact page

EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
SELECT
  contacts.*,
  (
    SELECT
      COUNT(*)
    FROM
      "conversations"
    WHERE
      "conversations"."contact_id" = "contacts"."id"
  ) as conversations_count
FROM
  "contacts"
WHERE
  "contacts"."account_id" = 1
  AND (
    contacts.email <> ''
    OR contacts.phone_number <> ''
    OR contacts.identifier <> ''
  )
GROUP BY
  "contacts"."id"
ORDER BY
  "contacts"."last_activity_at" desc NULLS LAST
LIMIT
  20;

Type of change

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

How Has This Been Tested?

This is tested on ChatwootQA DB. Overall the performance is higher for larger accounts, for smaller accounts the performance improvement is relatively smaller, but in all cases the query takes less than half of the original

The following are the links to the query plans

Account with 6K contacts

Unoptimized: https://explain.dalibo.com/plan/ed5fg23729d02c91
Optimized: https://explain.dalibo.com/plan/fdf16065ccg3ad85

Cuts query time by 85%

Account with 23K contacts

Unoptimized: https://explain.dalibo.com/plan/a1g0e8c9d699c7d6
Optimized: https://explain.dalibo.com/plan/12b68b5h8h5399f1

Cuts query time by 96%

Account with 495K contacts

Unoptimized: https://explain.dalibo.com/plan/f45g344g7d7696g3
Optimized: https://explain.dalibo.com/plan/242bd6d1g83ac8f7

Cuts query time by 99%

Account with 2.9 M contacts

Unoptimized: https://explain.dalibo.com/plan/f2ef8467fa57b5e5
Optimized: https://explain.dalibo.com/plan/a58aa96e954b73g2

Cuts query time by 98%

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@netlify

netlify Bot commented Jul 27, 2023 •

Copy link
Copy Markdown

✅ Deploy Preview for chatwoot-storybook ready!

Name Link
🔨 Latest commit fda8544
🔍 Latest deploy log https://app.netlify.com/sites/chatwoot-storybook/deploys/64c21ce023dcd30007216dad
😎 Deploy Preview https://deploy-preview-7629--chatwoot-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@scmmishra
scmmishra requested a review from sojan-official July 27, 2023 07:22
@scmmishra
scmmishra marked this pull request as ready for review July 27, 2023 07:23
@scmmishra
scmmishra temporarily deployed to qa-chatwoot July 27, 2023 07:44 Inactive
@scmmishra

scmmishra commented Jul 27, 2023 •

Copy link
Copy Markdown
Member Author

@sojan-official thanks for the review, deploying it on QA and testing this out first Done

@scmmishra
scmmishra merged commit ff97536 into develop Jul 27, 2023
@scmmishra
scmmishra deleted the feature/cw-2184 branch July 27, 2023 08:02
@github-actions

Copy link
Copy Markdown

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 Aug 26, 2023

This branch was previously deployed

1 inactive deployment
qa-chatwoot — fda8544d Deployed Jul 27, 2023 by scmmishra
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants