112
scripts/add-database-indexes.sql
Normal file
112
scripts/add-database-indexes.sql
Normal file
@@ -0,0 +1,112 @@
|
||||
-- Database Performance Indexes
|
||||
-- Adds indexes to frequently queried columns for improved performance
|
||||
--
|
||||
-- Run with: sqlite3 /opt/media-downloader/database/downloads.db < scripts/add-database-indexes.sql
|
||||
|
||||
-- ============================================================================
|
||||
-- Downloads Table Indexes
|
||||
-- ============================================================================
|
||||
|
||||
-- Index on platform for filtering downloads by platform
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_platform
|
||||
ON downloads(platform);
|
||||
|
||||
-- Index on source for filtering downloads by source/username
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_source
|
||||
ON downloads(source);
|
||||
|
||||
-- Index on download_date for time-based queries (DESC for most recent first)
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_download_date
|
||||
ON downloads(download_date DESC);
|
||||
|
||||
-- Index on status for filtering by download status
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_status
|
||||
ON downloads(status);
|
||||
|
||||
-- Compound index for platform + source queries (common filter combination)
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_platform_source
|
||||
ON downloads(platform, source);
|
||||
|
||||
-- Compound index for platform + download_date (common for analytics)
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_platform_date
|
||||
ON downloads(platform, download_date DESC);
|
||||
|
||||
-- Index on filename for search queries
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_filename
|
||||
ON downloads(filename);
|
||||
|
||||
-- Index on media_id for duplicate detection
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_media_id
|
||||
ON downloads(media_id)
|
||||
WHERE media_id IS NOT NULL;
|
||||
|
||||
-- Index on file_hash for duplicate detection
|
||||
CREATE INDEX IF NOT EXISTS idx_downloads_file_hash
|
||||
ON downloads(file_hash)
|
||||
WHERE file_hash IS NOT NULL;
|
||||
|
||||
-- ============================================================================
|
||||
-- Notifications Table Indexes
|
||||
-- ============================================================================
|
||||
|
||||
-- Index on sent_at for time-based queries
|
||||
CREATE INDEX IF NOT EXISTS idx_notifications_sent_at
|
||||
ON notifications(sent_at DESC);
|
||||
|
||||
-- Index on platform for filtering notifications
|
||||
CREATE INDEX IF NOT EXISTS idx_notifications_platform
|
||||
ON notifications(platform);
|
||||
|
||||
-- Index on status for filtering by notification status
|
||||
CREATE INDEX IF NOT EXISTS idx_notifications_status
|
||||
ON notifications(status);
|
||||
|
||||
-- Compound index for platform + sent_at (common query)
|
||||
CREATE INDEX IF NOT EXISTS idx_notifications_platform_sent_at
|
||||
ON notifications(platform, sent_at DESC);
|
||||
|
||||
-- ============================================================================
|
||||
-- Scheduler State Table Indexes
|
||||
-- ============================================================================
|
||||
|
||||
-- Index on status for active task queries
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduler_state_status
|
||||
ON scheduler_state(status);
|
||||
|
||||
-- Index on next_run for finding next scheduled tasks
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduler_state_next_run
|
||||
ON scheduler_state(next_run ASC);
|
||||
|
||||
-- Index on platform for platform-specific queries
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduler_state_platform
|
||||
ON scheduler_state(platform);
|
||||
|
||||
-- ============================================================================
|
||||
-- Users Table Indexes
|
||||
-- ============================================================================
|
||||
|
||||
-- Index on username for login queries
|
||||
CREATE INDEX IF NOT EXISTS idx_users_username
|
||||
ON users(username);
|
||||
|
||||
-- Index on email for lookup queries (if email is used)
|
||||
CREATE INDEX IF NOT EXISTS idx_users_email
|
||||
ON users(email)
|
||||
WHERE email IS NOT NULL;
|
||||
|
||||
-- ============================================================================
|
||||
-- Performance Analysis
|
||||
-- ============================================================================
|
||||
|
||||
-- Run ANALYZE to update query planner statistics
|
||||
ANALYZE;
|
||||
|
||||
-- Display index information
|
||||
SELECT
|
||||
'Index Information' as info,
|
||||
name as index_name,
|
||||
tbl_name as table_name
|
||||
FROM sqlite_master
|
||||
WHERE type = 'index'
|
||||
AND name LIKE 'idx_%'
|
||||
ORDER BY tbl_name, name;
|
||||
Reference in New Issue
Block a user