Skip to content

Conversation

@sg-gs
Copy link
Member

@sg-gs sg-gs commented Jan 8, 2026

What

Another step to speedup the Drive's database. We need an index to remove files metadata records of still existing users that are stored for >1 year (or any other given period) and these changes aim to offer an efficient solution to that.

Why

Because the current index is not efficient and it is also another thing to maintain and another expense to count on INSERT time. Check this, just to get 200 records:

EXPLAIN ANALYZE SELECT id, status, updated_at
from files where updated_at < NOW() - interval '1 year' and status = 'DELETED';
---
Seq Scan on files  (cost=0.00..62317363.52 rows=112393842 width=350) (actual time=127.480..1321875.634 rows=116543089 loops=1)
  Filter: ((status = 'DELETED'::enum_files_status) AND (updated_at < (now() - '1 year'::interval)))
  Rows Removed by Filter: 733607286
Planning Time: 0.413 ms
JIT:
  Functions: 4
  Options: Inlining true, Optimization true, Expressions true, Deforming true
  Timing: Generation 2.099 ms (Deform 1.282 ms), Inlining 8.262 ms, Optimization 71.281 ms, Emission 47.872 ms, Total 129.514 ms
Execution Time: 1327475.205 ms

We aim to make this table as efficient and light as possible. The current execution time is just not acceptable and the index is also weights 3.5GB.

How

Leveraging BRIN indexes to do that on an efficient manner, as the records removed one year ago are highly correlated with their physical location on the disk due to the fact that the updated_at is no longer updated after the files' statuses are set as DELETED.

Results

After adding this index, things change quite a lot (I have not added any EXPLAIN ANALYZE as they do not seem to work good with BRIN indexes):

  • Before:
    • Index size: 3.5GB
    • Execution time: 1327475.205ms (~1327s)
  • After:
    • Index size: 12MB
    • Execution time: 100ms

@sg-gs sg-gs self-assigned this Jan 8, 2026
@sg-gs sg-gs requested a review from jzunigax2 as a code owner January 8, 2026 15:56
@sg-gs sg-gs added the enhancement New feature or request label Jan 8, 2026
@sg-gs
Copy link
Member Author

sg-gs commented Jan 8, 2026

Migration run @jzunigax2

Copy link
Contributor

@jzunigax2 jzunigax2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these changes must be necessary to fix the e2e failling test

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 8, 2026

@sg-gs sg-gs requested a review from jzunigax2 January 8, 2026 16:36
@sg-gs
Copy link
Member Author

sg-gs commented Jan 8, 2026

Great, thanks! @jzunigax2

@sg-gs sg-gs merged commit fd5aa69 into master Jan 9, 2026
13 checks passed
@sg-gs sg-gs deleted the fix/optimise-background-removal-index branch January 9, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ready-for-preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants