1.0.212.sql 1.0 KB

123456789101112131415161718
  1. SET NAMES utf8mb4;
  2. SET @db = DATABASE();
  3. -- 1. ScheduleResultOpMaster 增加 PeriodDetRecId 列(关联 PeriodSequenceDet.RecID,一对一)
  4. SET @sql = (SELECT IF(
  5. COUNT(*) = 0,
  6. 'ALTER TABLE ScheduleResultOpMaster ADD COLUMN PeriodDetRecId BIGINT NULL COMMENT ''关联PeriodSequenceDet.RecID(一对一)''',
  7. 'SELECT ''ScheduleResultOpMaster.PeriodDetRecId already exists'' AS info'
  8. ) FROM INFORMATION_SCHEMA.COLUMNS
  9. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'ScheduleResultOpMaster' AND COLUMN_NAME = 'PeriodDetRecId');
  10. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
  11. -- 2. 为 PeriodDetRecId 建索引(加速 JOIN)
  12. SET @idx = 'idx_srom_perioddetrecid';
  13. SET @tbl = 'ScheduleResultOpMaster';
  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, '(PeriodDetRecId)'), 'SELECT 1');
  16. PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;