-- ============================================
-- 数据库索引性能测试脚本
-- 用途: 对比索引创建前后的查询性能
-- 使用方法: mysql -u root -p hfb_sys < test_index_performance.sql
-- ============================================

SET NAMES utf8mb4;

-- 关闭查询缓存，确保测试准确性
SET SESSION query_cache_type = OFF;

-- 开启性能分析
SET profiling = 1;

-- ============================================
-- 测试 1: 订单状态+创建时间查询
-- ============================================

-- 清空缓冲区
RESET QUERY CACHE;

-- 查看执行计划
EXPLAIN SELECT * FROM rental_orders
WHERE status = 'active'
ORDER BY created_at DESC
LIMIT 20\G

-- 执行查询并记录时间
SELECT '测试 1: 订单状态+创建时间查询' AS test_name;
SELECT * FROM rental_orders
WHERE status = 'active'
ORDER BY created_at DESC
LIMIT 20;

-- ============================================
-- 测试 2: 钱包流水按用户+业务类型查询
-- ============================================

RESET QUERY CACHE;

EXPLAIN SELECT * FROM wallet_ledger
WHERE user_id = 1 AND biz_type = 'order_payment'
ORDER BY created_at DESC
LIMIT 20\G

SELECT '测试 2: 钱包流水按用户+业务类型查询' AS test_name;
SELECT * FROM wallet_ledger
WHERE user_id = 1 AND biz_type = 'order_payment'
ORDER BY created_at DESC
LIMIT 20;

-- ============================================
-- 测试 3: 订单结算状态查询
-- ============================================

RESET QUERY CACHE;

EXPLAIN SELECT * FROM rental_orders
WHERE settlement_status = 'unsettled' AND owner_id = 1
ORDER BY created_at
LIMIT 20\G

SELECT '测试 3: 订单结算状态查询' AS test_name;
SELECT * FROM rental_orders
WHERE settlement_status = 'unsettled' AND owner_id = 1
ORDER BY created_at
LIMIT 20;

-- ============================================
-- 测试 4: 商品发布时间查询
-- ============================================

RESET QUERY CACHE;

EXPLAIN SELECT * FROM rental_listings
WHERE status = 'active' AND review_status = 'approved'
ORDER BY published_at DESC
LIMIT 20\G

SELECT '测试 4: 商品发布时间查询' AS test_name;
SELECT * FROM rental_listings
WHERE status = 'active' AND review_status = 'approved'
ORDER BY published_at DESC
LIMIT 20;

-- ============================================
-- 测试 5: 用户实名认证状态查询
-- ============================================

RESET QUERY CACHE;

EXPLAIN SELECT * FROM users
WHERE realname_status = 'verified' AND status = 'active'
LIMIT 100\G

SELECT '测试 5: 用户实名认证状态查询' AS test_name;
SELECT COUNT(*) FROM users
WHERE realname_status = 'verified' AND status = 'active';

-- ============================================
-- 显示性能分析结果
-- ============================================

-- 显示所有查询的性能概览
SELECT '性能测试结果' AS title;
SHOW PROFILES;

-- ============================================
-- 索引使用情况检查
-- ============================================

SELECT '索引使用情况检查' AS title;

-- 检查新增索引是否存在
SELECT
  TABLE_NAME,
  INDEX_NAME,
  GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
  AND INDEX_NAME IN (
    'idx_rental_orders_status_created_at',
    'idx_wallet_ledger_user_biz_created',
    'idx_rental_orders_settlement',
    'idx_rental_listings_published',
    'idx_users_realname_status'
  )
GROUP BY TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME, INDEX_NAME;

-- ============================================
-- 慢查询统计
-- ============================================

SELECT '慢查询统计' AS title;
SHOW GLOBAL STATUS LIKE 'Slow_queries';

-- ============================================
-- 索引基数检查（检查索引选择性）
-- ============================================

SELECT '索引基数检查' AS title;

-- rental_orders 表索引基数
SELECT
  INDEX_NAME,
  SEQ_IN_INDEX,
  COLUMN_NAME,
  CARDINALITY,
  SUB_PART,
  NULLABLE
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'rental_orders'
  AND INDEX_NAME LIKE 'idx_%'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

-- wallet_ledger 表索引基数
SELECT
  INDEX_NAME,
  SEQ_IN_INDEX,
  COLUMN_NAME,
  CARDINALITY,
  SUB_PART,
  NULLABLE
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'wallet_ledger'
  AND INDEX_NAME LIKE 'idx_%'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

-- ============================================
-- 表统计信息
-- ============================================

SELECT '表统计信息' AS title;

SELECT
  TABLE_NAME,
  TABLE_ROWS,
  AVG_ROW_LENGTH,
  DATA_LENGTH,
  INDEX_LENGTH,
  ROUND(DATA_LENGTH / 1024 / 1024, 2) AS data_mb,
  ROUND(INDEX_LENGTH / 1024 / 1024, 2) AS index_mb,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) AS total_mb
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME IN ('rental_orders', 'wallet_ledger', 'rental_listings', 'users')
ORDER BY TABLE_NAME;

-- ============================================
-- 测试完成
-- ============================================

SELECT '索引性能测试完成！' AS message;
SELECT '请查看 SHOW PROFILES 的结果，对比优化前后的查询时间' AS tips;

-- 关闭性能分析
SET profiling = 0;
