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

optimize get_queue query #446

Merged
merged 1 commit into from
Aug 7, 2023
Merged

optimize get_queue query #446

merged 1 commit into from
Aug 7, 2023

Conversation

marsha97
Copy link
Contributor

@marsha97 marsha97 commented Aug 7, 2023

OR query somehow not using index condition in our server, resulting in a very slow query(even for .exists())
but changing it to IN improve the speed pretty much
this is for comparison:

Using status OR

                                                                                                                              QUERY PLAN                                                                                                                                
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..6.41 rows=1 width=4) (actual time=20346.830..20346.831 rows=0 loops=1)
   ->  Seq Scan on post_office_email  (cost=0.00..1666614.04 rows=259811 width=4) (actual time=20346.829..20346.829 rows=0 loops=1)
         Filter: (((scheduled_time <= '2023-08-07 03:49:47.800747+00'::timestamp with time zone) OR (scheduled_time IS NULL)) AND ((expires_at > '2023-08-07 03:49:47.800747+00'::timestamp with time zone) OR (expires_at IS NULL)) AND ((status = 2) OR (status = 3)))
         Rows Removed by Filter: 5747818
 Planning Time: 0.110 ms
 Execution Time: 20346.850 ms
(6 rows)

using status IN

                                                                                                              QUERY PLAN                                                                                                              
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.43..1.03 rows=1 width=4) (actual time=1.231..1.231 rows=1 loops=1)
   ->  Index Scan using post_office_email_status_013a896c on post_office_email  (cost=0.43..154141.61 rows=259811 width=4) (actual time=1.230..1.230 rows=1 loops=1)
         Index Cond: (status = ANY ('{2,3}'::integer[]))
         Filter: (((scheduled_time <= '2023-08-07 03:49:47.800747+00'::timestamp with time zone) OR (scheduled_time IS NULL)) AND ((expires_at > '2023-08-07 03:49:47.800747+00'::timestamp with time zone) OR (expires_at IS NULL)))
 Planning Time: 0.130 ms
 Execution Time: 1.250 ms
(6 rows)

@selwin selwin merged commit 71c9022 into ui:master Aug 7, 2023
8 checks 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.

4 participants