diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index c5815c4..5e3b2f2 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -743,3 +743,4 @@ npm --prefix frontend run build | 2026-08-15 | P2 订单详情分片事务写入切片 | 已完成 | 订单详情远端请求仍在锁外执行;每个详情分片先收集成功结果,再通过订单应用 `UnitOfWork` 在单事务内提交,避免逐订单独立事务和分片内部分提交;事务失败时整片结果统一失败并保留远端会话收束逻辑;订单刷新定向测试、`make check`、架构和中文注释门禁通过。尚未实现真正单 SQL 多行 UPSERT、游标分页和后台 Job 化,P2 批量同步仍保持“进行中” | | 2026-08-15 | P2 订单刷新复合游标扫描切片 | 已完成 | 订单刷新应用 Port 移除 OFFSET 分页入口,数据库新增 `created_at + order_id` 复合游标查询并统一扫描模型;刷新流程按游标读取每个账号订单,加入游标不推进保护;1001 条订单无重复扫描、订单/Server 定向测试和中文注释门禁通过。用户订单列表 HTTP 分页仍是页码语义;真正单 SQL 多行 UPSERT、后台 Job 化、SQLite/MySQL/Postgres 查询计划证据仍待完成,P2 批量同步保持“进行中” | | 2026-08-15 | P2 商品多规格探测批量与并发边界切片 | 已完成 | 商品同步先用 `MultiSpecFlags` 一次读取本地多规格标记,消除逐商品数据库查询;剩余远端详情探测使用 4 路有界并发,Session 过期可取消同批次探测,普通单项失败保留告警并继续;新增批量标记和并发上限回归测试,定向测试与中文注释门禁通过。跨请求缓存、跨批次去重、平台限流指标和 MySQL/Postgres 查询计划仍待完成,P2 批量同步保持“进行中” | +| 2026-08-15 | P2 订单复合游标索引与查询计划切片 | 已完成 | SQLite/MySQL/Postgres 新增 `idx_orders_cursor(cookie_id, deleted_at, created_at, order_id)` 复合索引;新增 SQLite `EXPLAIN QUERY PLAN` 回归,确认订单游标查询使用该索引;迁移、订单扫描和中文注释门禁通过。MySQL/Postgres 实际执行计划仍需外部数据库环境验证,P2 批量同步保持“进行中” | diff --git a/internal/db/migrations/mysql/00030_order_cursor_index.sql b/internal/db/migrations/mysql/00030_order_cursor_index.sql new file mode 100644 index 0000000..84e3266 --- /dev/null +++ b/internal/db/migrations/mysql/00030_order_cursor_index.sql @@ -0,0 +1,5 @@ +-- +goose Up +CREATE INDEX idx_orders_cursor ON orders(cookie_id, deleted_at, created_at DESC, order_id DESC); + +-- +goose Down +DROP INDEX idx_orders_cursor ON orders; diff --git a/internal/db/migrations/postgres/00030_order_cursor_index.sql b/internal/db/migrations/postgres/00030_order_cursor_index.sql new file mode 100644 index 0000000..abf1579 --- /dev/null +++ b/internal/db/migrations/postgres/00030_order_cursor_index.sql @@ -0,0 +1,5 @@ +-- +goose Up +CREATE INDEX IF NOT EXISTS idx_orders_cursor ON orders(cookie_id, deleted_at, created_at DESC, order_id DESC); + +-- +goose Down +DROP INDEX IF EXISTS idx_orders_cursor; diff --git a/internal/db/migrations/sqlite/00030_order_cursor_index.sql b/internal/db/migrations/sqlite/00030_order_cursor_index.sql new file mode 100644 index 0000000..abf1579 --- /dev/null +++ b/internal/db/migrations/sqlite/00030_order_cursor_index.sql @@ -0,0 +1,5 @@ +-- +goose Up +CREATE INDEX IF NOT EXISTS idx_orders_cursor ON orders(cookie_id, deleted_at, created_at DESC, order_id DESC); + +-- +goose Down +DROP INDEX IF EXISTS idx_orders_cursor; diff --git a/internal/db/orders_delivery_test.go b/internal/db/orders_delivery_test.go index 6ef2f80..c3f81ab 100644 --- a/internal/db/orders_delivery_test.go +++ b/internal/db/orders_delivery_test.go @@ -64,6 +64,35 @@ func TestOrdersByCookiePageScansBeyondLegacyLimit(t *testing.T) { if len(cursorIDs) != 1001 { t.Fatalf("cursor scan count=%d want 1001", len(cursorIDs)) } + // planRows 保存 SQLite 对复合游标查询生成的执行计划。 + planRows, err := s.DB.QueryContext(ctx, + `EXPLAIN QUERY PLAN SELECT order_id FROM orders WHERE cookie_id=? AND deleted_at IS NULL ORDER BY created_at DESC,order_id DESC LIMIT ?`, cookieID, 500) + if err != nil { + t.Fatalf("cursor explain plan: %v", err) + } + defer planRows.Close() + // planUsesCursorIndex 表示执行计划是否使用订单复合游标索引。 + planUsesCursorIndex := false + for planRows.Next() { + // detail 保存 SQLite 执行计划文本。 + var id, parent, notUsed int + // detail 保存 SQLite 执行计划步骤说明。 + var detail string + // err 表示读取执行计划步骤的错误。 + if err := planRows.Scan(&id, &parent, ¬Used, &detail); err != nil { + t.Fatalf("scan cursor explain plan: %v", err) + } + if strings.Contains(detail, "idx_orders_cursor") { + planUsesCursorIndex = true + } + } + // err 表示遍历执行计划结果的错误。 + if err := planRows.Err(); err != nil { + t.Fatalf("cursor explain rows: %v", err) + } + if !planUsesCursorIndex { + t.Fatal("cursor query did not use idx_orders_cursor") + } } // TestOrdersSoftDeleteMissingForCookie 负责Test订单列表SoftDeleteMissingFor登录凭证相关处理。