20260512_s8_exception_indexes.sql 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. -- ORDER-FLOW-S8-INTEGRATED-DOMAIN-RESET-1 t1c
  2. -- Target table: ado_s8_exception
  3. -- Purpose: add 3 indexes for order-flow related exception queries
  4. -- (related_object_code aggregation, order_flow_code filter,
  5. -- related+flow joint lookup for chain drill-down).
  6. --
  7. -- Path: A (SugarIndex Attribute in AdoS8Exception.cs) is primary.
  8. -- This SQL file is path B: aidopdev fallback if CodeFirst skips
  9. -- pre-existing tables. Idempotent via pre-check SELECT against
  10. -- INFORMATION_SCHEMA.STATISTICS (MySQL 8 has no CREATE INDEX IF NOT EXISTS).
  11. --
  12. -- Usage:
  13. -- 1. Run the 3 pre-check SELECTs below; skip the matching CREATE INDEX
  14. -- when the SELECT returns >0 rows.
  15. -- 2. Or run the 3 CREATE INDEX statements directly; MySQL will error
  16. -- "Duplicate key name" if the index already exists, which is safe.
  17. -- 3. This file does NOT touch column types, data rows, or other indexes.
  18. -- =========================================================================
  19. -- Pre-check (run first; non-destructive)
  20. -- =========================================================================
  21. -- Pre-check A: idx_s8_exception_related_object_code
  22. SELECT COUNT(*) AS exists_related_object_code
  23. FROM INFORMATION_SCHEMA.STATISTICS
  24. WHERE TABLE_SCHEMA = DATABASE()
  25. AND TABLE_NAME = 'ado_s8_exception'
  26. AND INDEX_NAME = 'idx_s8_exception_related_object_code';
  27. -- Pre-check B: idx_s8_exception_order_flow
  28. SELECT COUNT(*) AS exists_order_flow
  29. FROM INFORMATION_SCHEMA.STATISTICS
  30. WHERE TABLE_SCHEMA = DATABASE()
  31. AND TABLE_NAME = 'ado_s8_exception'
  32. AND INDEX_NAME = 'idx_s8_exception_order_flow';
  33. -- Pre-check C: idx_s8_exception_related_flow
  34. SELECT COUNT(*) AS exists_related_flow
  35. FROM INFORMATION_SCHEMA.STATISTICS
  36. WHERE TABLE_SCHEMA = DATABASE()
  37. AND TABLE_NAME = 'ado_s8_exception'
  38. AND INDEX_NAME = 'idx_s8_exception_related_flow';
  39. -- =========================================================================
  40. -- Index creation (run each only when its pre-check returned 0)
  41. -- =========================================================================
  42. -- Index 1: order-by-order aggregation (exceptionCount per order_code).
  43. -- Columns: tenant_id, factory_id, related_object_code, is_deleted
  44. CREATE INDEX idx_s8_exception_related_object_code
  45. ON ado_s8_exception (tenant_id, factory_id, related_object_code, is_deleted);
  46. -- Index 2: order-flow-level filter (chain page baseline / drill-down).
  47. -- Columns: tenant_id, factory_id, order_flow_code, status, is_deleted
  48. CREATE INDEX idx_s8_exception_order_flow
  49. ON ado_s8_exception (tenant_id, factory_id, order_flow_code, status, is_deleted);
  50. -- Index 3: joint key for per-order per-stage lookup.
  51. -- Columns: tenant_id, factory_id, related_object_code, order_flow_code
  52. CREATE INDEX idx_s8_exception_related_flow
  53. ON ado_s8_exception (tenant_id, factory_id, related_object_code, order_flow_code);
  54. -- =========================================================================
  55. -- Post-check (run after creation; expect 3 rows total)
  56. -- =========================================================================
  57. SELECT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS cols
  58. FROM INFORMATION_SCHEMA.STATISTICS
  59. WHERE TABLE_SCHEMA = DATABASE()
  60. AND TABLE_NAME = 'ado_s8_exception'
  61. AND INDEX_NAME IN (
  62. 'idx_s8_exception_related_object_code',
  63. 'idx_s8_exception_order_flow',
  64. 'idx_s8_exception_related_flow'
  65. )
  66. GROUP BY INDEX_NAME;