This commit is contained in:
Tomáš Dvořák
2025-10-16 13:32:05 +02:00
commit 12cba639b9
663 changed files with 168914 additions and 0 deletions
@@ -0,0 +1,82 @@
-- Performance indexes for optimal query performance
-- Run this migration to achieve 10/10 database performance
-- Articles table indexes
CREATE INDEX IF NOT EXISTS idx_articles_published_date ON articles(published, published_at DESC) WHERE published = true;
CREATE INDEX IF NOT EXISTS idx_articles_slug ON articles(slug) WHERE slug IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_articles_category ON articles(category_id) WHERE category_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_articles_author ON articles(author_id);
CREATE INDEX IF NOT EXISTS idx_articles_featured ON articles(featured, published_at DESC) WHERE featured = true AND published = true;
CREATE INDEX IF NOT EXISTS idx_articles_search ON articles USING gin(to_tsvector('english', title || ' ' || COALESCE(content, '')));
-- Players table indexes
CREATE INDEX IF NOT EXISTS idx_players_team ON players(team_id) WHERE team_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_players_position ON players(position) WHERE position IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_players_active ON players(is_active) WHERE is_active = true;
CREATE INDEX IF NOT EXISTS idx_players_name ON players(name);
-- Users table indexes
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_users_role ON users(role);
CREATE INDEX IF NOT EXISTS idx_users_created ON users(created_at DESC);
-- Categories table indexes
CREATE INDEX IF NOT EXISTS idx_categories_name ON categories(name);
-- Newsletter subscriptions indexes
CREATE INDEX IF NOT EXISTS idx_newsletter_email ON newsletter_subscriptions(email);
CREATE INDEX IF NOT EXISTS idx_newsletter_active ON newsletter_subscriptions(is_active) WHERE is_active = true;
-- Contact messages indexes
CREATE INDEX IF NOT EXISTS idx_contact_messages_created ON contact_messages(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_contact_messages_read ON contact_messages(is_read) WHERE is_read = false;
-- Polls indexes
CREATE INDEX IF NOT EXISTS idx_polls_active ON polls(is_active, end_date) WHERE is_active = true;
CREATE INDEX IF NOT EXISTS idx_poll_votes_poll ON poll_votes(poll_id);
CREATE INDEX IF NOT EXISTS idx_poll_votes_user ON poll_votes(user_identifier);
-- Match overrides indexes
CREATE INDEX IF NOT EXISTS idx_match_overrides_external ON match_overrides(external_match_id);
-- Team logo overrides indexes
CREATE INDEX IF NOT EXISTS idx_team_logo_external ON team_logo_overrides(external_team_id);
-- Uploaded files indexes
CREATE INDEX IF NOT EXISTS idx_uploaded_files_user ON uploaded_files(uploaded_by_id) WHERE uploaded_by_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_uploaded_files_created ON uploaded_files(created_at DESC);
-- Settings table (only one row, no index needed)
-- Competition aliases indexes
CREATE INDEX IF NOT EXISTS idx_competition_aliases_code ON competition_aliases(code);
CREATE INDEX IF NOT EXISTS idx_competition_aliases_order ON competition_aliases(display_order) WHERE display_order > 0;
-- Clothing items indexes
CREATE INDEX IF NOT EXISTS idx_clothing_category ON clothing(category) WHERE category IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_clothing_available ON clothing(is_available) WHERE is_available = true;
-- Composite indexes for common queries
CREATE INDEX IF NOT EXISTS idx_articles_category_published ON articles(category_id, published, published_at DESC) WHERE published = true;
CREATE INDEX IF NOT EXISTS idx_articles_author_published ON articles(author_id, published, published_at DESC) WHERE published = true;
-- Partial indexes for better performance on filtered queries
CREATE INDEX IF NOT EXISTS idx_articles_published_only ON articles(published_at DESC) WHERE published = true;
CREATE INDEX IF NOT EXISTS idx_articles_unpublished_only ON articles(created_at DESC) WHERE published = false;
-- VACUUM ANALYZE to update statistics
VACUUM ANALYZE articles;
VACUUM ANALYZE players;
VACUUM ANALYZE users;
VACUUM ANALYZE categories;
VACUUM ANALYZE newsletter_subscriptions;
VACUUM ANALYZE contact_messages;
VACUUM ANALYZE polls;
VACUUM ANALYZE poll_options;
VACUUM ANALYZE poll_votes;
-- Add comments for documentation
COMMENT ON INDEX idx_articles_published_date IS 'Optimizes published articles listing';
COMMENT ON INDEX idx_articles_search IS 'Full-text search on articles';
COMMENT ON INDEX idx_players_team IS 'Player queries by team';
COMMENT ON INDEX idx_newsletter_active IS 'Active subscriber queries';