优化工作-1

This commit is contained in:
yml2213
2026-06-05 07:41:52 +08:00
parent b7ec5e2755
commit 0b9b0b5ea7
17 changed files with 1807 additions and 127 deletions
+66
View File
@@ -0,0 +1,66 @@
-- ============================================
-- 数据库索引优化迁移
-- 创建时间: 2026-06-05
-- 目的: 优化高频查询的性能
-- ============================================
SET NAMES utf8mb4;
-- -------------------------------------------
-- 1. 订单状态+创建时间复合索引
-- 用途: 后台订单管理、用户订单列表按时间排序
-- 场景: SELECT * FROM rental_orders WHERE status = ? ORDER BY created_at DESC
-- -------------------------------------------
ALTER TABLE rental_orders
ADD INDEX idx_rental_orders_status_created_at (status, created_at DESC);
-- -------------------------------------------
-- 2. 钱包流水按用户+业务类型+时间查询
-- 用途: 用户查看特定类型的流水记录
-- 场景: SELECT * FROM wallet_ledger WHERE user_id = ? AND biz_type = ? ORDER BY created_at DESC
-- -------------------------------------------
ALTER TABLE wallet_ledger
ADD INDEX idx_wallet_ledger_user_biz_created (user_id, biz_type, created_at DESC);
-- -------------------------------------------
-- 3. 订单结算状态查询优化
-- 用途: 查询待结算订单、按号主查询结算记录
-- 场景: SELECT * FROM rental_orders WHERE settlement_status = ? AND owner_id = ? ORDER BY created_at
-- -------------------------------------------
ALTER TABLE rental_orders
ADD INDEX idx_rental_orders_settlement (settlement_status, owner_id, created_at);
-- -------------------------------------------
-- 4. 商品发布时间查询优化
-- 用途: 首页商品列表按发布时间排序
-- 场景: SELECT * FROM rental_listings WHERE status = 'active' AND review_status = 'approved' ORDER BY published_at DESC
-- 注意: 使用部分索引(MySQL 8.0+)只索引可用商品
-- -------------------------------------------
ALTER TABLE rental_listings
ADD INDEX idx_rental_listings_published (status, review_status, published_at DESC);
-- 如果需要进一步优化,可以考虑添加 in_transaction 列
-- 但当前索引已经足够覆盖大部分查询
-- -------------------------------------------
-- 5. 用户实名认证状态查询
-- 用途: 查询未实名用户、查询活跃且已实名用户
-- 场景: SELECT * FROM users WHERE realname_status = 'verified' AND status = 'active'
-- -------------------------------------------
ALTER TABLE users
ADD INDEX idx_users_realname_status (realname_status, status);
-- ============================================
-- 索引创建完成
-- ============================================
-- 验证索引创建情况(可选,用于开发环境验证)
-- SHOW INDEX FROM rental_orders WHERE Key_name LIKE 'idx_rental_orders_%';
-- SHOW INDEX FROM wallet_ledger WHERE Key_name LIKE 'idx_wallet_ledger_%';
-- SHOW INDEX FROM rental_listings WHERE Key_name LIKE 'idx_rental_listings_%';
-- SHOW INDEX FROM users WHERE Key_name LIKE 'idx_users_%';
@@ -0,0 +1,200 @@
-- ============================================
-- 数据库索引性能测试脚本
-- 用途: 对比索引创建前后的查询性能
-- 使用方法: 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;