Files
Oxicloud/migrations/20260625000001_files_user_size_index.sql
Edouard Vanbelle 4531ee9f15 fix(db-migration): fix 2 changes with same ID
This solve issue with 2 migrations made the same day, due to merge on pull request, DB migration is blocking

    the same version prefix:
     - 20260625000000_files_user_size_index.sql    (Dio)
     - 20260625000000_folder_tree_modified_at.sql  (Ed)
    They were renamed to ...0001 and ...0002 (disjoint versions) + protection like "IF NOT EXISTS"

    I have opt for an automated clean up of old entry:
        `DELETE FROM _sqlx_migrations WHERE version = 20260625000000;`
    runned on startup

    affected users: Dio, myself and any dev that wanted to work on this project since eb0ba58158
2026-06-07 16:32:42 +02:00

19 lines
948 B
SQL

-- Covering partial index for per-user storage-usage accounting.
--
-- The usage calculation is:
-- SELECT COALESCE(SUM(size), 0) FROM storage.files
-- WHERE user_id = $1 AND NOT is_trashed;
--
-- Without an index that carries `size`, this is a heap scan over every file
-- the user owns. This index lets PostgreSQL satisfy it with an index-only scan:
-- * keyed by user_id → only the target user's rows are visited
-- * INCLUDE (size) → the sum is read straight from the index
-- * WHERE NOT is_trashed → matches the query predicate exactly and keeps
-- the index small (trashed files are excluded)
--
-- Used by the per-upload usage update and the periodic background
-- reconciliation sweep (GET /api/auth/me no longer recomputes usage inline).
CREATE INDEX IF NOT EXISTS idx_files_user_size_active
ON storage.files (user_id) INCLUDE (size)
WHERE NOT is_trashed;