-- 性能优化索引脚本
-- 创建时间: 2025
-- 描述: 为常用查询字段添加索引，提升查询性能

SET NAMES utf8mb4;

-- =============================================
-- 1. 部门表 (yz_tenant_departments) 索引优化
-- =============================================

-- 检查并添加 tenant_id 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_departments' 
               AND index_name = 'idx_tenant_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_tenant_id ON yz_tenant_departments(tenant_id)',
                   'SELECT "索引 idx_tenant_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 delete_time 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_departments' 
               AND index_name = 'idx_delete_time');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_delete_time ON yz_tenant_departments(delete_time)',
                   'SELECT "索引 idx_delete_time 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加复合索引 (tenant_id, delete_time) 用于常用查询
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_departments' 
               AND index_name = 'idx_tenant_delete');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_tenant_delete ON yz_tenant_departments(tenant_id, delete_time)',
                   'SELECT "索引 idx_tenant_delete 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 parent_id 索引（用于树形结构查询）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_departments' 
               AND index_name = 'idx_parent_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_parent_id ON yz_tenant_departments(parent_id)',
                   'SELECT "索引 idx_parent_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =============================================
-- 2. 职位表 (yz_tenant_positions) 索引优化
-- =============================================

-- 检查并添加 tenant_id 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_positions' 
               AND index_name = 'idx_tenant_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_tenant_id ON yz_tenant_positions(tenant_id)',
                   'SELECT "索引 idx_tenant_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 delete_time 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_positions' 
               AND index_name = 'idx_delete_time');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_delete_time ON yz_tenant_positions(delete_time)',
                   'SELECT "索引 idx_delete_time 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 department_id 索引（用于按部门查询职位）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_positions' 
               AND index_name = 'idx_department_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_department_id ON yz_tenant_positions(department_id)',
                   'SELECT "索引 idx_department_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加复合索引 (department_id, delete_time, status) 用于常用查询
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_tenant_positions' 
               AND index_name = 'idx_dept_delete_status');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_dept_delete_status ON yz_tenant_positions(department_id, delete_time, status)',
                   'SELECT "索引 idx_dept_delete_status 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =============================================
-- 3. 角色表 (yz_roles) 索引优化
-- =============================================

-- 检查并添加 tenant_id 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_roles' 
               AND index_name = 'idx_tenant_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_tenant_id ON yz_roles(tenant_id)',
                   'SELECT "索引 idx_tenant_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 delete_time 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_roles' 
               AND index_name = 'idx_delete_time');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_delete_time ON yz_roles(delete_time)',
                   'SELECT "索引 idx_delete_time 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =============================================
-- 4. 员工表 (yz_employees) 索引优化
-- =============================================

-- 检查并添加 tenant_id 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_employees' 
               AND index_name = 'idx_tenant_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_tenant_id ON yz_employees(tenant_id)',
                   'SELECT "索引 idx_tenant_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 delete_time 索引（如果不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_employees' 
               AND index_name = 'idx_delete_time');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_delete_time ON yz_employees(delete_time)',
                   'SELECT "索引 idx_delete_time 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 department_id 索引
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_employees' 
               AND index_name = 'idx_department_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_department_id ON yz_employees(department_id)',
                   'SELECT "索引 idx_department_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 检查并添加 position_id 索引
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics 
               WHERE table_schema = DATABASE() 
               AND table_name = 'yz_employees' 
               AND index_name = 'idx_position_id');
SET @sqlstmt := IF(@exist = 0, 
                   'CREATE INDEX idx_position_id ON yz_employees(position_id)',
                   'SELECT "索引 idx_position_id 已存在" AS message');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT '性能优化索引创建完成！' AS message;

