71 lines
5.0 KiB
SQL
71 lines
5.0 KiB
SQL
-- =====================================================================
|
||
-- MCP 只读数据工具 —— 索引脚本(对应方案第 6 章 / 16.4)
|
||
-- 适用:MySQL 8.0+
|
||
--
|
||
-- 【2026-09-23 实测更新】在 oms_test 上核对 information_schema 后发现:
|
||
-- • P0-1 oms_inventory_outer.idx_outer_code(outer_code) —— 已存在,无需执行
|
||
-- • P0-2 oms_inventory_outer_detail.idx_outer_code(outer_code) —— 已存在,无需执行
|
||
-- • P1-1 oms_inventory_inner.idx_order_code(order_code) —— 已存在,无需执行
|
||
-- • P1-2 oms_purchase_order.idx_vendor_id(vendor_id) —— 已存在,无需执行
|
||
-- • P1-3 oms_inventory_info 覆盖索引 —— 缺失,已在 oms_test 执行并实测收益
|
||
-- (建索引耗时 1.3s;按产品聚合 141.8ms → 66.6ms;EXPLAIN 显示 Using index,免回表)
|
||
--
|
||
-- 因此本脚本默认全部注释,仅作为「生产执行前的核对与补建」清单使用。
|
||
-- 执行前请先跑文末的核对语句,确认目标索引是否缺失。
|
||
-- =====================================================================
|
||
|
||
-- ---------------------------------------------------------------------
|
||
-- P0 必加(inventory_flow 出库链路依赖;若缺失将导致按出库单号全表扫描)
|
||
-- 实测:oms_test 已存在
|
||
-- ---------------------------------------------------------------------
|
||
-- ALTER TABLE oms_inventory_outer ADD INDEX idx_outer_code (outer_code), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_inventory_outer_detail ADD INDEX idx_outer_code (outer_code), ALGORITHM=INPLACE, LOCK=NONE;
|
||
|
||
-- ---------------------------------------------------------------------
|
||
-- P1 建议
|
||
-- 实测:P1-1 / P1-2 在 oms_test 已存在
|
||
-- ---------------------------------------------------------------------
|
||
-- ALTER TABLE oms_inventory_inner ADD INDEX idx_order_code (order_code), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_purchase_order ADD INDEX idx_vendor_id (vendor_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
|
||
-- ---------------------------------------------------------------------
|
||
-- P1-3 覆盖索引(库存聚合免回表)
|
||
-- 实测收益显著;但存在写入放大(每次 insert/update 需维护 4 列索引),
|
||
-- 生产库请由 DBA 结合写入量评估后再执行;回滚语句见文末。
|
||
-- oms_test 已于 2026-09-23 执行。
|
||
-- ---------------------------------------------------------------------
|
||
-- ALTER TABLE oms_inventory_info
|
||
-- ADD INDEX idx_pc_status_amt (product_code, inventory_status, inner_price, outer_price),
|
||
-- ALGORITHM=INPLACE, LOCK=NONE;
|
||
|
||
-- ---------------------------------------------------------------------
|
||
-- P2 按表规模触发(当前数据量下无需执行;表增长到 10 万+ 或成为热点时再补)
|
||
-- 完整编号清单见方案 6.3 节(P2-1 ~ P2-26,其中 P2-3/P2-4/P2-19 已作废)
|
||
-- ---------------------------------------------------------------------
|
||
-- ALTER TABLE oms_purchase_order_map ADD INDEX idx_order_id (order_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_purchase_order_map ADD INDEX idx_purchase_id (purchase_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_inventory_info ADD INDEX idx_warehouse_id (warehouse_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_stock_info ADD INDEX idx_order_code (order_code), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_receivable_receipt_detail ADD INDEX idx_write_off_id (write_off_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_payable_payment_detail ADD INDEX idx_write_off_id (write_off_id), ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE bu_todo_completed ADD INDEX idx_approve_user_time (approve_user, approve_time), ALGORITHM=INPLACE, LOCK=NONE;
|
||
|
||
-- =====================================================================
|
||
-- 执行前核对:列出目标表当前索引(若目标索引已出现则无需再建)
|
||
-- =====================================================================
|
||
-- SELECT table_name, index_name, GROUP_CONCAT(column_name ORDER BY seq_in_index) AS cols
|
||
-- FROM information_schema.statistics
|
||
-- WHERE table_schema = DATABASE()
|
||
-- AND table_name IN ('oms_inventory_outer','oms_inventory_outer_detail','oms_inventory_inner',
|
||
-- 'oms_purchase_order','oms_inventory_info')
|
||
-- GROUP BY table_name, index_name;
|
||
|
||
-- =====================================================================
|
||
-- 回滚(仅在需要撤销新增索引时执行)
|
||
-- =====================================================================
|
||
-- ALTER TABLE oms_inventory_outer DROP INDEX idx_outer_code, ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_inventory_outer_detail DROP INDEX idx_outer_code, ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_inventory_inner DROP INDEX idx_order_code, ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_purchase_order DROP INDEX idx_vendor_id, ALGORITHM=INPLACE, LOCK=NONE;
|
||
-- ALTER TABLE oms_inventory_info DROP INDEX idx_pc_status_amt, ALGORITHM=INPLACE, LOCK=NONE;
|