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

Addons: improve DB query for projects_feature table #10871

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

humitos
Copy link
Member

@humitos humitos commented Oct 25, 2023

This small improvements reduces the db query from ~20ms to ~1ms.

Original query:

Limit  (cost=6753.56..6753.77 rows=21 width=24) (actual time=26.139..26.144 rows=13 loops=1)
   ->  HashAggregate  (cost=6753.56..6753.87 rows=31 width=24) (actual time=26.138..26.142 rows=13 loops=1)
         Group Key: projects_feature.feature_id
         ->  Hash Right Join  (cost=1.70..6556.54 rows=78808 width=24) (actual time=0.055..26.118 rows=54 loops=1)
               Hash Cond: (projects_feature_projects.feature_id = projects_feature.id)
               Filter: ((projects_feature_projects.project_id = 256207) OR ((projects_feature.add_date > '2018-12-05 10:47:06.794648+00'::timestamp with time zone) AND projects_feature.default_true) OR ((projects_feature.add_date <= '2018-12-05 10:47:06.794648+00'::timestamp with time zone) AND projects_feature.future_default_true))
               Rows Removed by Filter: 73672
               ->  Seq Scan on projects_feature_projects  (cost=0.00..5669.03 rows=274603 width=8) (actual time=0.006..7.502 rows=73715 loops=1)
               ->  Hash  (cost=1.31..1.31 rows=31 width=38) (actual time=0.041..0.042 rows=31 loops=1)
                     Buckets: 1024  Batches: 1  Memory Usage: 11kB
                     ->  Seq Scan on projects_feature  (cost=0.00..1.31 rows=31 width=38) (actual time=0.006..0.010 rows=31 loops=1)
 Planning Time: 0.396 ms
 Execution Time: 26.173 ms
(13 rows)

New query:

 Limit  (cost=0.56..6405.06 rows=7 width=24) (actual time=0.015..0.039 rows=2 loops=1)
   ->  Unique  (cost=0.56..6405.06 rows=7 width=24) (actual time=0.015..0.038 rows=2 loops=1)
         ->  Nested Loop Left Join  (cost=0.56..6360.57 rows=17795 width=24) (actual time=0.014..0.035 rows=7 loops=1)
               Filter: ((projects_feature_projects.project_id = 256207) OR ((projects_feature.add_date > '2018-12-05 10:47:06.794648+00'::timestamp with time zone) AND projects_feature.default_true) OR ((projects_feature.add_date <= '2018-12-05 10:47:06.794648+00'::timestamp with time zone) AND projects_feature.future_default_true))
               Rows Removed by Filter: 9
               ->  Index Scan using projects_feature_feature_id_key on projects_feature  (cost=0.14..12.68 rows=7 width=38) (actual time=0.003..0.011 rows=8 loops=1)
                     Filter: ((feature_id)::text ~~ 'addons\_%'::text)
                     Rows Removed by Filter: 23
               ->  Index Only Scan using projects_feature_projects_feature_id_a684910e_uniq on projects_feature_projects  (cost=0.42..606.49 rows=17163 width=8) (actual time=0.002..0.002 rows=2 loops=8)
                     Index Cond: (feature_id = projects_feature.id)
                     Heap Fetches: 11
 Planning Time: 0.275 ms
 Execution Time: 0.058 ms
(13 rows)

This small improvements reduces the db query from ~20ms to ~1ms.
@humitos humitos requested a review from a team as a code owner October 25, 2023 10:54
@humitos humitos requested a review from stsewd October 25, 2023 10:54
@humitos humitos merged commit 90f1791 into main Oct 26, 2023
2 checks passed
@humitos humitos deleted the humitos/addons-projects-feature-query branch October 26, 2023 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants