1.0.208.sql 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -- =============================================================================
  2. -- 版本:1.0.208
  3. -- 目的:为生产排程链路关键表添加 tenant_id 索引,提升多租户查询性能
  4. -- 影响表:WorkOrdRouting / PeriodSequenceDet / ShopCalendarWorkCtr /
  5. -- WorkOrdMaster / ScheduleExceptionMaster / ScheduleResultOpMaster
  6. -- 幂等:通过 information_schema 检查索引是否已存在,重复执行安全
  7. -- 兼容:MySQL 5.x / 8.x 通用(PREPARE/EXECUTE 模式,无需 DELIMITER)
  8. -- =============================================================================
  9. SET NAMES utf8mb4;
  10. SET @db = DATABASE();
  11. -- 1. WorkOrdRouting(tenant_id, WorkOrd) 复合索引
  12. SET @idx = 'idx_workordrouting_tenant';
  13. SET @tbl = 'WorkOrdRouting';
  14. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  15. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id, WorkOrd)'), 'SELECT 1');
  16. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  17. -- 2. PeriodSequenceDet(tenant_id) 单列索引
  18. SET @idx = 'idx_periodseq_tenant';
  19. SET @tbl = 'PeriodSequenceDet';
  20. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  21. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id)'), 'SELECT 1');
  22. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  23. -- 3. PeriodSequenceDet(tenant_id, WorkOrds) 复合索引 — 用于 DeactivateExistingScheduleAsync
  24. SET @idx = 'idx_periodseq_deactivate';
  25. SET @tbl = 'PeriodSequenceDet';
  26. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  27. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id, WorkOrds)'), 'SELECT 1');
  28. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  29. -- 4. ShopCalendarWorkCtr(tenant_id)
  30. SET @idx = 'idx_shopcalendar_tenant';
  31. SET @tbl = 'ShopCalendarWorkCtr';
  32. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  33. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id)'), 'SELECT 1');
  34. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  35. -- 5. WorkOrdMaster(tenant_id)
  36. SET @idx = 'idx_workordmaster_tenant';
  37. SET @tbl = 'WorkOrdMaster';
  38. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  39. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id)'), 'SELECT 1');
  40. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  41. -- 6. ScheduleExceptionMaster(tenant_id)
  42. SET @idx = 'idx_scheduleexception_tenant';
  43. SET @tbl = 'ScheduleExceptionMaster';
  44. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  45. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id)'), 'SELECT 1');
  46. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  47. -- 7. ScheduleResultOpMaster(tenant_id)
  48. SET @idx = 'idx_scheduleresultop_tenant';
  49. SET @tbl = 'ScheduleResultOpMaster';
  50. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  51. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(tenant_id)'), 'SELECT 1');
  52. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  53. -- 8. ScheduleResultOpMaster(WorkOrd, Op) — 高频 JOIN 键
  54. SET @idx = 'idx_scheduleresultop_workord';
  55. SET @tbl = 'ScheduleResultOpMaster';
  56. SET @exists = (SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @db AND table_name = @tbl AND index_name = @idx);
  57. SET @sql = IF(@exists = 0, CONCAT('CREATE INDEX ', @idx, ' ON ', @tbl, '(WorkOrd, Op)'), 'SELECT 1');
  58. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;