-- Performance Optimization SQL Script
-- This script adds necessary indexes to improve query performance

-- Enable slow query logging (PostgreSQL)
ALTER SYSTEM SET log_min_duration_statement = 1000;
SELECT pg_reload_conf();

-- ============================================
-- User Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_users_username ON sys_users(username);
CREATE INDEX IF NOT EXISTS idx_users_email ON sys_users(email);
CREATE INDEX IF NOT EXISTS idx_users_status ON sys_users(status);
CREATE INDEX IF NOT EXISTS idx_users_deleted_at ON sys_users(deleted_at);
CREATE INDEX IF NOT EXISTS idx_users_created_at ON sys_users(created_at);

-- ============================================
-- Role Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_roles_role_key ON sys_roles(role_key);
CREATE INDEX IF NOT EXISTS idx_roles_role_name ON sys_roles(role_name);
CREATE INDEX IF NOT EXISTS idx_roles_status ON sys_roles(status);
CREATE INDEX IF NOT EXISTS idx_roles_deleted_at ON sys_roles(deleted_at);

-- ============================================
-- Menu Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_menus_parent_id ON sys_menus(parent_id);
CREATE INDEX IF NOT EXISTS idx_menus_status ON sys_menus(status);
CREATE INDEX IF NOT EXISTS idx_menus_menu_type ON sys_menus(menu_type);

-- ============================================
-- Config Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_config_config_key ON sys_config(config_key);
CREATE INDEX IF NOT EXISTS idx_sys_config_deleted_at ON sys_config(deleted_at);

-- ============================================
-- Notice Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_notice_status ON sys_notice(status);
CREATE INDEX IF NOT EXISTS idx_sys_notice_deleted_at ON sys_notice(deleted_at);
CREATE INDEX IF NOT EXISTS idx_sys_notice_created_at ON sys_notice(created_at);

-- ============================================
-- File Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_file_file_name ON sys_file(file_name);
CREATE INDEX IF NOT EXISTS idx_sys_file_file_type ON sys_file(file_type);
CREATE INDEX IF NOT EXISTS idx_sys_file_deleted_at ON sys_file(deleted_at);

-- ============================================
-- Dictionary Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_dictionary_type ON dictionary(type);
CREATE INDEX IF NOT EXISTS idx_dictionary_code ON dictionary(code);
CREATE INDEX IF NOT EXISTS idx_dictionary_type_code ON dictionary(type, code);
CREATE INDEX IF NOT EXISTS idx_dictionary_deleted_at ON dictionary(deleted_at);

-- ============================================
-- Dict Type Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_dict_type_dict_type ON sys_dict_type(dict_type);
CREATE INDEX IF NOT EXISTS idx_sys_dict_type_deleted_at ON sys_dict_type(deleted_at);

-- ============================================
-- Dict Data Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_dict_data_dict_type ON sys_dict_data(dict_type);
CREATE INDEX IF NOT EXISTS idx_sys_dict_data_dict_code ON sys_dict_data(dict_code);
CREATE INDEX IF NOT EXISTS idx_sys_dict_data_deleted_at ON sys_dict_data(deleted_at);

-- ============================================
-- User Message Table Indexes
-- ============================================
CREATE INDEX IF NOT EXISTS idx_sys_user_message_user_id ON sys_user_message(user_id);
CREATE INDEX IF NOT EXISTS idx_sys_user_message_is_read ON sys_user_message(is_read);
CREATE INDEX IF NOT EXISTS idx_sys_user_message_deleted_at ON sys_user_message(deleted_at);

-- ============================================
-- Composite Indexes for Common Queries
-- ============================================
CREATE INDEX IF NOT EXISTS idx_users_status_deleted ON sys_users(status, deleted_at);
CREATE INDEX IF NOT EXISTS idx_roles_status_deleted ON sys_roles(status, deleted_at);

-- ============================================
-- Analyze Tables After Index Creation
-- ============================================
ANALYZE sys_users;
ANALYZE sys_roles;
ANALYZE sys_menus;
ANALYZE sys_config;
ANALYZE sys_notice;
ANALYZE sys_file;
ANALYZE dictionary;
ANALYZE sys_dict_type;
ANALYZE sys_dict_data;
ANALYZE sys_user_message;

-- ============================================
-- Query Performance Verification
-- ============================================
-- Use EXPLAIN ANALYZE to verify query performance
-- Example: EXPLAIN ANALYZE SELECT * FROM sys_users WHERE username = 'testuser';
