Skip to content

[_] fix/reduce stats limit#1044

Merged
apsantiso merged 2 commits into
masterfrom
chore/increase-limit-folder-stats
Apr 21, 2026
Merged

[_] fix/reduce stats limit#1044
apsantiso merged 2 commits into
masterfrom
chore/increase-limit-folder-stats

Conversation

@apsantiso
Copy link
Copy Markdown
Collaborator

@apsantiso apsantiso commented Apr 20, 2026

What

This query is not the most performant, but we still have an ORDER BY creation_time which is not an indexed field.

Also, we we're looking for 10K files while just returning 1K, so there's no point on the extra load on the DB.

Changes

  1. Removed order by creation_time as it adds no value here.
  2. Reduced files limit to 1K as discussed. (looks for 1001)
  3. Reduce depth to 50.

Benchmark

New:

Aggregate  (cost=1131.27..1131.28 rows=1 width=44) (actual time=13.068..13.072 rows=1 loops=1)
  Buffers: shared hit=727
  CTE folder_recursive
    ->  Recursive Union  (cost=0.57..82.90 rows=11 width=40) (actual time=0.019..0.103 rows=21 loops=1)
          Buffers: shared hit=52
          ->  Index Scan using uuid_index on folders fl1  (cost=0.57..2.59 rows=1 width=40) (actual time=0.017..0.018 rows=1 loops=1)
                Index Cond: (uuid = '451a7bfe-ed44-43d6-a25b-41670a1d6ecd'::uuid)
                Filter: ((NOT removed) AND (NOT deleted))
                Buffers: shared hit=5
          ->  Nested Loop  (cost=0.57..8.02 rows=1 width=40) (actual time=0.022..0.037 rows=10 loops=2)
                Buffers: shared hit=47
                ->  WorkTable Scan on folder_recursive fr_1  (cost=0.00..0.22 rows=3 width=24) (actual time=0.001..0.001 rows=4 loops=2)
                      Filter: (depth < 50)
                ->  Index Scan using idx_folders_user_parentuuid_plainname_not_deleted_removed on folders fl2  (cost=0.57..2.59 rows=1 width=36) (actual time=0.005..0.009 rows=3 loops=7)
                      Index Cond: ((user_id = fr_1.owner_id) AND (parent_uuid = fr_1.uuid))
                      Buffers: shared hit=47
  ->  Limit  (cost=0.57..1030.86 rows=1001 width=28) (actual time=0.046..12.967 rows=1001 loops=1)
        Buffers: shared hit=727
        ->  Nested Loop  (cost=0.57..12832.27 rows=12467 width=28) (actual time=0.045..12.853 rows=1001 loops=1)
              Buffers: shared hit=727
              ->  CTE Scan on folder_recursive fr  (cost=0.00..0.22 rows=11 width=20) (actual time=0.021..0.114 rows=21 loops=1)
                    Buffers: shared hit=52
              ->  Index Scan using idx_files_folder_user_exists on files f  (cost=0.57..1155.22 rows=1133 width=24) (actual time=0.010..0.601 rows=48 loops=21)
                    Index Cond: (folder_uuid = fr.uuid)
                    Buffers: shared hit=675
Planning:
  Buffers: shared hit=10
Planning Time: 0.963 ms
Execution Time: 17.968 ms

Old

Aggregate  (cost=14199.63..14199.64 rows=1 width=48) (actual time=14.332..14.335 rows=1 loops=1)
  Buffers: shared hit=1535
  CTE folder_recursive
    ->  Recursive Union  (cost=0.57..82.90 rows=11 width=40) (actual time=0.015..0.273 rows=53 loops=1)
          Buffers: shared hit=265
          ->  Index Scan using uuid_index on folders fl1  (cost=0.57..2.59 rows=1 width=40) (actual time=0.014..0.015 rows=1 loops=1)
                Index Cond: (uuid = '451a7bfe-ed44-43d6-a25b-41670a1d6ecd'::uuid)
                Filter: ((NOT removed) AND (NOT deleted))
                Buffers: shared hit=5
          ->  Nested Loop  (cost=0.57..8.02 rows=1 width=40) (actual time=0.017..0.048 rows=10 loops=5)
                Buffers: shared hit=260
                ->  WorkTable Scan on folder_recursive fr_1  (cost=0.00..0.22 rows=3 width=24) (actual time=0.000..0.002 rows=11 loops=5)
                      Filter: (depth < 20)
                ->  Index Scan using idx_folders_user_parentuuid_plainname_not_deleted_removed on folders fl2  (cost=0.57..2.59 rows=1 width=36) (actual time=0.003..0.004 rows=1 loops=53)
                      Index Cond: ((user_id = fr_1.owner_id) AND (parent_uuid = fr_1.uuid))
                      Buffers: shared hit=260
  ->  WindowAgg  (cost=13680.41..13898.56 rows=12467 width=40) (actual time=10.886..14.188 rows=1662 loops=1)
        Run Condition: (row_number() OVER (?) <= 10000)
        Buffers: shared hit=1535
        ->  Sort  (cost=13680.39..13711.56 rows=12467 width=32) (actual time=10.880..10.970 rows=1662 loops=1)
              Sort Key: f.creation_time
              Sort Method: quicksort  Memory: 126kB
              Buffers: shared hit=1535
              ->  Nested Loop  (cost=0.57..12832.27 rows=12467 width=32) (actual time=0.030..2.365 rows=1662 loops=1)
                    Buffers: shared hit=1535
                    ->  CTE Scan on folder_recursive fr  (cost=0.00..0.22 rows=11 width=16) (actual time=0.017..0.299 rows=53 loops=1)
                          Buffers: shared hit=265
                    ->  Index Scan using idx_files_folder_user_exists on files f  (cost=0.57..1155.22 rows=1133 width=48) (actual time=0.007..0.035 rows=31 loops=53)
                          Index Cond: (folder_uuid = fr.uuid)
                          Buffers: shared hit=1270
Planning:
  Buffers: shared hit=14
Planning Time: 0.711 ms
Execution Time: 14.414 ms

@apsantiso apsantiso requested a review from jzunigax2 as a code owner April 20, 2026 09:02
@apsantiso apsantiso self-assigned this Apr 20, 2026
@apsantiso apsantiso requested review from sg-gs and xabg2 April 20, 2026 09:03
sg-gs
sg-gs previously approved these changes Apr 20, 2026
@sg-gs
Copy link
Copy Markdown
Member

sg-gs commented Apr 20, 2026

Add an EXPLAIN ANALYZE to the description if you can @apsantiso. Also, tests are failing

@sonarqubecloud
Copy link
Copy Markdown

@apsantiso apsantiso requested a review from sg-gs April 20, 2026 14:50
@apsantiso apsantiso merged commit 4c4b09b into master Apr 21, 2026
9 checks passed
@apsantiso apsantiso deleted the chore/increase-limit-folder-stats branch April 21, 2026 10:11
apsantiso added a commit that referenced this pull request Apr 28, 2026
* chore: increase limit folder stats

* reduce limit to 1000
apsantiso added a commit that referenced this pull request Apr 28, 2026
* chore: increase limit folder stats

* reduce limit to 1000
apsantiso added a commit that referenced this pull request Apr 28, 2026
* chore: increase limit folder stats

* reduce limit to 1000
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.

2 participants