Files
junhong_cmp_fiber/migrations/archive/000057_optimize_iot_card_indexes.up.sql

15 lines
736 B
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.
-- 优化 tb_iot_card 索引:修复 PG 优化器在 shop_id IN 场景下选错执行计划的问题
-- 问题idx_iot_card_global_created_at 导致优化器选择全表扫描+Filter而不是更高效的 partial 索引
-- 30M 行场景下 status+carrier_id 过滤查询从 4.5s 降至 <200ms
-- 1. 删除全局 created_at 索引(诱导优化器走错计划的元凶)
DROP INDEX IF EXISTS idx_iot_card_global_created_at;
-- 2. 新建 carrier_id 复合 partial 索引(覆盖运营商+排序场景)
CREATE INDEX IF NOT EXISTS idx_iot_card_standalone_shop_carrier_created
ON tb_iot_card (shop_id, carrier_id, created_at DESC)
WHERE deleted_at IS NULL AND is_standalone = true;
-- 3. 更新统计信息
ANALYZE tb_iot_card;