3 changed files with 67 additions and 1 deletions
@ -0,0 +1,46 @@ |
|||||
|
-- 1. 添加字段 |
||||
|
ALTER TABLE f10_journal_log |
||||
|
ADD COLUMN send_timestamp bigint DEFAULT NULL COMMENT 'send_date+send_time的毫秒级时间戳' |
||||
|
AFTER receive_timestamp; |
||||
|
|
||||
|
|
||||
|
-- 2. 回填日本时间 -> UTC毫秒时间戳 |
||||
|
UPDATE f10_journal_log |
||||
|
SET send_timestamp = |
||||
|
UNIX_TIMESTAMP( |
||||
|
CONVERT_TZ( |
||||
|
STR_TO_DATE( |
||||
|
CONCAT(send_date, send_time), |
||||
|
'%Y%m%d%H%i%s' |
||||
|
), |
||||
|
'Asia/Tokyo', |
||||
|
'+00:00' |
||||
|
) |
||||
|
) * 1000 |
||||
|
WHERE send_date IS NOT NULL |
||||
|
AND send_time IS NOT NULL; |
||||
|
|
||||
|
|
||||
|
-- 3. 删除重复,保留id最大 |
||||
|
DELETE t1 |
||||
|
FROM f10_journal_log t1 |
||||
|
JOIN f10_journal_log t2 |
||||
|
ON t1.device_id = t2.device_id |
||||
|
AND t1.instruction_flag = t2.instruction_flag |
||||
|
AND t1.send_timestamp = t2.send_timestamp |
||||
|
AND t1.id < t2.id; |
||||
|
|
||||
|
|
||||
|
-- 4. 删除旧唯一索引 |
||||
|
ALTER TABLE f10_journal_log |
||||
|
DROP INDEX uk_device_alarm_ts; |
||||
|
|
||||
|
|
||||
|
-- 5. 创建新唯一索引 |
||||
|
ALTER TABLE f10_journal_log |
||||
|
ADD UNIQUE KEY uk_device_instruction_send_time |
||||
|
( |
||||
|
device_id, |
||||
|
instruction_flag, |
||||
|
send_timestamp |
||||
|
) USING BTREE; |
||||
@ -0,0 +1,20 @@ |
|||||
|
-- 判断索引是否存在 |
||||
|
SET @index_exists = ( |
||||
|
SELECT COUNT(*) |
||||
|
FROM information_schema.statistics |
||||
|
WHERE table_schema = DATABASE() |
||||
|
AND table_name = 'alert_history' |
||||
|
AND index_name = 'idx_alert_history_search' |
||||
|
); |
||||
|
|
||||
|
-- 根据判断结果生成 SQL |
||||
|
SET @sql = IF( |
||||
|
@index_exists = 0, |
||||
|
'CREATE INDEX idx_alert_history_search ON alert_history (block_flag, source_type, handle_status)', |
||||
|
'SELECT "Index idx_alert_history_search already exists." AS message' |
||||
|
); |
||||
|
|
||||
|
-- 执行 SQL |
||||
|
PREPARE stmt FROM @sql; |
||||
|
EXECUTE stmt; |
||||
|
DEALLOCATE PREPARE stmt; |
||||
Loading…
Reference in new issue