Add performance optimizations (Phase 7)
- Add 12 composite database indexes for sync operations - Composite indexes for sync_queue (device/status/priority) - Composite indexes for reading_progress (user/media timestamps) - Composite indexes for devices (user/sync_enabled) - Composite indexes for annotations (user/media) - Comment out ALTER SYSTEM commands for sqlc compatibility - PostgreSQL tuning recommendations included for manual application
This commit is contained in:
@@ -28,6 +28,7 @@ CREATE TABLE users (
|
||||
theme VARCHAR(50) DEFAULT 'tokyo-night',
|
||||
scan_frequency_minutes INTEGER DEFAULT 60,
|
||||
auto_scan_enabled BOOLEAN DEFAULT true,
|
||||
max_devices INTEGER DEFAULT 10,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
@@ -427,10 +428,30 @@ CREATE INDEX idx_sync_conflicts_media_item_id ON sync_conflicts(media_item_id);
|
||||
CREATE INDEX idx_sync_conflicts_user_id ON sync_conflicts(user_id);
|
||||
CREATE INDEX idx_sync_conflicts_status ON sync_conflicts(resolution_status);
|
||||
|
||||
-- Composite indexes for sync queue performance (Phase 7)
|
||||
CREATE INDEX idx_sync_queue_device_status_priority ON sync_queue(device_id, status, priority ASC, created_at ASC);
|
||||
CREATE INDEX idx_sync_queue_status_priority_created ON sync_queue(status, priority ASC, created_at ASC);
|
||||
CREATE INDEX idx_sync_queue_device_created_at ON sync_queue(device_id, created_at DESC);
|
||||
|
||||
-- Composite indexes for reading progress performance (Phase 7)
|
||||
CREATE INDEX idx_reading_progress_user_last_sync ON reading_progress(user_id, last_sync_timestamp DESC);
|
||||
CREATE INDEX idx_reading_progress_media_last_sync ON reading_progress(media_item_id, last_sync_timestamp DESC);
|
||||
CREATE INDEX idx_reading_progress_user_sync_source ON reading_progress(user_id, last_sync_source, last_sync_timestamp DESC);
|
||||
|
||||
-- Composite indexes for devices (Phase 7)
|
||||
CREATE INDEX idx_devices_user_sync_enabled ON devices(user_id, sync_enabled, auto_sync);
|
||||
CREATE INDEX idx_devices_user_type ON devices(user_id, device_type);
|
||||
CREATE INDEX idx_devices_last_seen ON devices(last_seen DESC) WHERE sync_enabled = true;
|
||||
|
||||
-- Composite indexes for annotations (Phase 7)
|
||||
CREATE INDEX idx_media_notes_user_media ON media_notes(user_id, media_item_id);
|
||||
CREATE INDEX idx_media_highlights_user_media ON media_highlights(user_id, media_item_id);
|
||||
|
||||
-- Reading history indexes
|
||||
CREATE INDEX idx_reading_history_user_id ON reading_history(user_id);
|
||||
CREATE INDEX idx_reading_history_media_item_id ON reading_history(media_item_id);
|
||||
CREATE INDEX idx_reading_history_created_at ON reading_history(created_at DESC);
|
||||
CREATE INDEX idx_reading_history_user_device_created ON reading_history(user_id, device_id, created_at DESC);
|
||||
|
||||
-- ============================================
|
||||
-- TRIGGER FUNCTIONS (Phase 1)
|
||||
@@ -473,12 +494,48 @@ COMMENT ON COLUMN media_items.google_books_id IS 'Google Books identifier for in
|
||||
-- - All users default to 'user' role
|
||||
-- - Admin users can: create/manage libraries and folders, scan media, modify media metadata, delete media, control library visibility
|
||||
-- - Regular users can: view visible libraries, rate media, track reading progress, create notes and highlights, manage their profile
|
||||
-- - To create first admin: UPDATE users SET role = 'admin' WHERE email = 'your-admin-email';
|
||||
-- - Library visibility is controlled through library_visibility table - admins can hide/show libraries per user
|
||||
-- - Notes and highlights support position data (page:offset or CFI format) for precise location tracking
|
||||
-- - Highlights can have associated notes for detailed annotations
|
||||
-- - Backward compatibility views (ebooks, ebook_ratings, ebook_reading_progress, ebook_notes, ebook_highlights) maintain existing API contracts
|
||||
|
||||
-- ============================================
|
||||
-- PERFORMANCE OPTIMIZATION (Phase 7)
|
||||
-- ============================================
|
||||
-- NOTE: ALTER SYSTEM commands are commented out for sqlc compatibility.
|
||||
-- These should be run manually by DBA or via init script:
|
||||
--
|
||||
-- Concurrency settings for high-throughput sync operations
|
||||
-- ALTER SYSTEM SET max_connections = 200;
|
||||
-- ALTER SYSTEM SET shared_buffers = '256MB';
|
||||
-- ALTER SYSTEM SET effective_cache_size = '1GB';
|
||||
-- ALTER SYSTEM SET maintenance_work_mem = '64MB';
|
||||
-- ALTER SYSTEM SET checkpoint_completion_target = 0.9;
|
||||
-- ALTER SYSTEM SET wal_buffers = '16MB';
|
||||
-- ALTER SYSTEM SET default_statistics_target = 100;
|
||||
--
|
||||
-- Enable parallel query processing for sync operations
|
||||
-- ALTER SYSTEM SET max_parallel_workers_per_gather = 4;
|
||||
-- ALTER SYSTEM SET max_parallel_workers = 8;
|
||||
-- ALTER SYSTEM SET parallel_setup_cost = 100;
|
||||
-- ALTER SYSTEM SET parallel_tuple_cost = 0.1;
|
||||
--
|
||||
-- Work memory for complex sync queries
|
||||
-- ALTER SYSTEM SET work_mem = '16MB';
|
||||
--
|
||||
-- Background writer optimization for sync queue
|
||||
-- ALTER SYSTEM SET bgwriter_delay = 200ms;
|
||||
-- ALTER SYSTEM SET bgwriter_lru_maxpages = 200;
|
||||
-- ALTER SYSTEM SET bgwriter_lru_multiplier = 2.0;
|
||||
|
||||
-- Vacuum and analyze schedule for sync tables (run via cron)
|
||||
-- VACUUM ANALYZE sync_queue;
|
||||
-- VACUUM ANALYZE reading_progress;
|
||||
-- VACUUM ANALYZE sync_conflicts;
|
||||
-- VACUUM ANALYZE devices;
|
||||
-- REINDEX TABLE CONCURRENTLY sync_queue;
|
||||
-- REINDEX TABLE CONCURRENTLY reading_progress;
|
||||
|
||||
-- ============================================
|
||||
-- SYNC HELPER FUNCTIONS (Phase 1)
|
||||
-- ============================================
|
||||
|
||||
Reference in New Issue
Block a user