Files
pj_service_cloud/db/20261021_hard_order_statistics_index.sql
2026-10-21 19:39:59 +08:00

26 lines
1.6 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 订单实时统计配套索引;仅在确认执行计划后安排低峰期上线,不由应用自动执行。
-- 已有创建时间索引 idx_hard_order_tenant_del_ct_uid 可直接支持创建时间范围过滤。
-- 支付时间索引只在没有同名前导列的等效索引时创建,不重复建立宽覆盖索引。
SET @statistics_pay_index_exists = (
SELECT COUNT(*)
FROM (
SELECT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ',') AS columns_in_order
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'pjcloudx_hardware' AND TABLE_NAME = 'hard_order'
GROUP BY INDEX_NAME
) existing_indexes
WHERE columns_in_order = 'tenant_id,del_flag,pay_time'
OR columns_in_order LIKE 'tenant_id,del_flag,pay_time,%'
);
SET @statistics_pay_index_ddl = IF(@statistics_pay_index_exists = 0,
'CREATE INDEX idx_hard_order_tenant_del_pay_time ON pjcloudx_hardware.hard_order (tenant_id, del_flag, pay_time)',
'SELECT ''支付时间范围索引已存在,跳过'' AS message');
PREPARE statistics_pay_index_statement FROM @statistics_pay_index_ddl;
EXECUTE statistics_pay_index_statement;
DEALLOCATE PREPARE statistics_pay_index_statement;
-- 上线验收:SHOW INDEX FROM pjcloudx_hardware.hard_order;
-- 对接口实际生成的 SQL(含租户、数据权限、日期和商户/部门/钱包筛选)执行 EXPLAIN。
-- 确认订单表使用日期范围扫描,且未出现用户/购物车展示关联、全表售后聚合或分组 COUNT。
-- 记录近三个月的大租户耗时、扫描行数和并发请求下业务查询延迟;单条统计 SQL 超时为15秒。