Files
backend_v2/db/migrations/2026-10-13-01-image-list-indexes.sql
toom1996 1de1ec46a6 update
2026-10-18 17:42:10 +08:00

22 lines
1016 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.

-- 为「按所属图集列出图片」补索引。
--
-- 背景:`brand_runway_images` / `street_snap_images` 原本只有主键 (id),
-- 而列表与编辑页的取图查询是:
-- SELECT * FROM <图片表> WHERE <fk> = ? AND is_deleted = 0 ORDER BY sort_order
-- EXPLAIN 显示为 Seq Scan + Sort(含 Index = false):图片表已有上万行,
-- 每打开一次图集页都要全表扫一遍再排序。
--
-- 索引列顺序 (外键, is_deleted, sort_order) 同时满足等值过滤与排序,
-- 使该查询可以走 Index Scan 并直接按序返回,省掉 Sort。
--
-- 幂等:IF NOT EXISTS。可重复执行。
-- DROP 无回滚需求:索引可随时重建。
CREATE INDEX IF NOT EXISTS idx_br_images_runway
ON public.brand_runway_images (runway_id, is_deleted, sort_order);
CREATE INDEX IF NOT EXISTS idx_ss_images_snap
ON public.street_snap_images (snap_id, is_deleted, sort_order);
-- 应用后请重新导出结构:./bin/dbtool dump -clean -out db/backups/db_dump.sql