Skip to content
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

JAMES-2586 Optimize some postgres query #1984

Merged
merged 2 commits into from
Feb 7, 2024

Conversation

vttranlina
Copy link
Contributor

@vttranlina vttranlina commented Feb 7, 2024

  1. JAMES-2586 Optimize query increase/decrease for Quota Current Value

The query to increase the current value of the quota is frequently executed. The current SQL query is as follows:

insert into quota_current_value (
  identifier,
  component,
  type,
  "current_value"
)
values (
  '$1',
  '$2',
  '$3',
  '$4'
)
on conflict on constraint "quota_current_value_primary_key"
do update
set
  "current_value" = ("quota_current_value"."current_value" + $5);

This query is verbose and incurs overhead due to constraint checking.

=> The reality is the INSERT statement should be executed only once during initialization, and subsequent operations should use the UPDATE statement.
=> James can handle the INSERT and UPDATE cases appropriately.


  1. JAMES-2586 Add Index for Postgres Mailbox table

The query searches for mailbox.mailbox_name using LIKE: :

public Flux<PostgresMailbox> findMailboxWithPathLike(MailboxQuery.UserBound query) {

select *
from mailbox
where (
  mailbox_name like '$1'
  and user_name = '$2'
  and mailbox_namespace = '$3'
);

Although the mailbox table already has an index for the trio (MAILBOX_NAME, USER_NAME, MAILBOX_NAMESPACE), the LIKE "prefix%" query slows it down.

=> Create one more index for the composite (USER_NAME, MAILBOX_NAMESPACE)

result:
before
image

after
image

@vttranlina
Copy link
Contributor Author

Just tested the (2) on staging
Before
image

After
image

26ms -> 0.1 ms 🚀

@Arsnael Arsnael merged commit 7fe6638 into apache:postgresql Feb 7, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants