-- ============================================
-- 优化 pile_reservation_info 表索引
-- 目标：提升 queryReservationInfo 接口性能
-- 创建时间：2026-06-22
-- ============================================

-- 1. 检查现有索引
SELECT
    TABLE_NAME,
    INDEX_NAME,
    COLUMN_NAME,
    SEQ_IN_INDEX,
    INDEX_TYPE
FROM
    information_schema.STATISTICS
WHERE
    TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'pile_reservation_info'
ORDER BY
    INDEX_NAME, SEQ_IN_INDEX;

-- 2. 添加复合索引（pile_connector_code + del_flag）
-- 此索引用于优化 selectByPileConnectorCode 查询
-- WHERE del_flag = '0' AND pile_connector_code = ?
ALTER TABLE pile_reservation_info
ADD INDEX idx_connector_delflag (pile_connector_code, del_flag);

-- 3. 添加复合索引（member_id + pile_sn + status + del_flag）
-- 此索引用于优化 findByMemberIdAndPileSnAndStatus 查询
-- 已存在的查询：WHERE del_flag = '0' AND member_id = ? AND pile_sn = ? AND status = ?
ALTER TABLE pile_reservation_info
ADD INDEX idx_member_pile_status (member_id, pile_sn, status, del_flag);

-- 4. 添加复合索引（pile_connector_code + reservation_type + status + del_flag）
-- 此索引用于优化 selectActiveReservationByPileConnectorCode 查询
-- WHERE del_flag = '0' AND reservation_type = 'single' AND status = '1' AND pile_connector_code = ?
ALTER TABLE pile_reservation_info
ADD INDEX idx_connector_type_status (pile_connector_code, reservation_type, status, del_flag);

-- 5. 验证索引创建成功
SHOW INDEX FROM pile_reservation_info;

-- 6. 分析表以更新统计信息
ANALYZE TABLE pile_reservation_info;

-- ============================================
-- 回滚脚本（如需删除索引）
-- ============================================
/*
ALTER TABLE pile_reservation_info DROP INDEX idx_connector_delflag;
ALTER TABLE pile_reservation_info DROP INDEX idx_member_pile_status;
ALTER TABLE pile_reservation_info DROP INDEX idx_connector_type_status;
*/
