优化
This commit is contained in:
23
db/rollback_normaltemperature_split_columns.sql
Normal file
23
db/rollback_normaltemperature_split_columns.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- =====================================================
|
||||
-- 数据库回滚脚本:normaltemperature表移除联络单号和件号字段
|
||||
-- 创建时间:2026-02-04
|
||||
-- 说明:如果升级出现问题,使用此脚本回滚
|
||||
-- =====================================================
|
||||
|
||||
USE fullautowaterpressure;
|
||||
|
||||
-- 1. 删除索引
|
||||
DROP INDEX IF EXISTS `idx_contact_number` ON `normaltemperature`;
|
||||
DROP INDEX IF EXISTS `idx_item_number` ON `normaltemperature`;
|
||||
|
||||
-- 2. 删除新增字段
|
||||
ALTER TABLE `normaltemperature`
|
||||
DROP COLUMN IF EXISTS `ContactNumber`,
|
||||
DROP COLUMN IF EXISTS `ItemNumber`;
|
||||
|
||||
-- 3. 验证回滚
|
||||
DESCRIBE normaltemperature;
|
||||
|
||||
-- =====================================================
|
||||
-- 回滚完成
|
||||
-- =====================================================
|
||||
50
db/upgrade_normaltemperature_split_columns.sql
Normal file
50
db/upgrade_normaltemperature_split_columns.sql
Normal file
@@ -0,0 +1,50 @@
|
||||
-- =====================================================
|
||||
-- 数据库升级脚本:normaltemperature表添加联络单号和件号字段
|
||||
-- 创建时间:2026-02-04
|
||||
-- 说明:将barcode字段拆分为ContactNumber和ItemNumber两个独立字段
|
||||
-- =====================================================
|
||||
|
||||
USE fullautowaterpressure;
|
||||
|
||||
-- 1. 添加新字段
|
||||
ALTER TABLE `normaltemperature`
|
||||
ADD COLUMN `ContactNumber` VARCHAR(100) NULL COMMENT '联络单号' AFTER `barcode`,
|
||||
ADD COLUMN `ItemNumber` VARCHAR(100) NULL COMMENT '件号' AFTER `ContactNumber`;
|
||||
|
||||
-- 2. 迁移现有数据(假设barcode格式为"联络单号-件号")
|
||||
-- 如果barcode包含"-",则拆分
|
||||
UPDATE `normaltemperature`
|
||||
SET
|
||||
`ContactNumber` = SUBSTRING_INDEX(`barcode`, '-', 1),
|
||||
`ItemNumber` = SUBSTRING_INDEX(`barcode`, '-', -1)
|
||||
WHERE `barcode` IS NOT NULL AND `barcode` LIKE '%-%';
|
||||
|
||||
-- 如果barcode不包含"-",则全部作为联络单号
|
||||
UPDATE `normaltemperature`
|
||||
SET
|
||||
`ContactNumber` = `barcode`,
|
||||
`ItemNumber` = ''
|
||||
WHERE `barcode` IS NOT NULL AND `barcode` NOT LIKE '%-%';
|
||||
|
||||
-- 3. 创建索引以提高查询性能
|
||||
CREATE INDEX `idx_contact_number` ON `normaltemperature`(`ContactNumber`);
|
||||
CREATE INDEX `idx_item_number` ON `normaltemperature`(`ItemNumber`);
|
||||
|
||||
-- 4. 验证数据迁移
|
||||
SELECT
|
||||
COUNT(*) as total_records,
|
||||
COUNT(ContactNumber) as has_contact_number,
|
||||
COUNT(ItemNumber) as has_item_number,
|
||||
COUNT(CASE WHEN ContactNumber IS NOT NULL AND ItemNumber IS NOT NULL THEN 1 END) as both_fields
|
||||
FROM normaltemperature;
|
||||
|
||||
-- 5. 显示示例数据
|
||||
SELECT
|
||||
Id, barcode, ContactNumber, ItemNumber, CreateTime
|
||||
FROM normaltemperature
|
||||
ORDER BY Id DESC
|
||||
LIMIT 10;
|
||||
|
||||
-- =====================================================
|
||||
-- 升级完成
|
||||
-- =====================================================
|
||||
88
db/verify_split_columns.sql
Normal file
88
db/verify_split_columns.sql
Normal file
@@ -0,0 +1,88 @@
|
||||
-- =====================================================
|
||||
-- 验证脚本:检查联络单号和件号字段是否正确
|
||||
-- 创建时间:2026-02-04
|
||||
-- =====================================================
|
||||
|
||||
USE fullautowaterpressure;
|
||||
|
||||
-- 1. 检查normaltemperature表结构
|
||||
SHOW COLUMNS FROM normaltemperature LIKE '%Contact%';
|
||||
SHOW COLUMNS FROM normaltemperature LIKE '%Item%';
|
||||
|
||||
-- 2. 检查scandata表结构
|
||||
SHOW COLUMNS FROM scandata LIKE '%Contact%';
|
||||
SHOW COLUMNS FROM scandata LIKE '%Item%';
|
||||
|
||||
-- 3. 检查索引
|
||||
SHOW INDEX FROM normaltemperature WHERE Key_name LIKE 'idx_%';
|
||||
SHOW INDEX FROM scandata WHERE Key_name LIKE 'idx_%';
|
||||
|
||||
-- 4. 统计normaltemperature表数据
|
||||
SELECT
|
||||
'报表数据统计' as 表名,
|
||||
COUNT(*) as 总记录数,
|
||||
COUNT(ContactNumber) as 有联络单号,
|
||||
COUNT(ItemNumber) as 有件号,
|
||||
COUNT(CASE WHEN ContactNumber IS NOT NULL AND ItemNumber IS NOT NULL THEN 1 END) as 两者都有,
|
||||
COUNT(CASE WHEN ContactNumber IS NULL OR ItemNumber IS NULL THEN 1 END) as 缺失数据
|
||||
FROM normaltemperature;
|
||||
|
||||
-- 5. 统计scandata表数据
|
||||
SELECT
|
||||
'扫码数据统计' as 表名,
|
||||
COUNT(*) as 总记录数,
|
||||
COUNT(ContactNumber) as 有联络单号,
|
||||
COUNT(ItemNumber) as 有件号,
|
||||
COUNT(CASE WHEN ContactNumber IS NOT NULL AND ItemNumber IS NOT NULL THEN 1 END) as 两者都有,
|
||||
COUNT(CASE WHEN ContactNumber IS NULL OR ItemNumber IS NULL THEN 1 END) as 缺失数据
|
||||
FROM scandata;
|
||||
|
||||
-- 6. 查看normaltemperature最新10条记录
|
||||
SELECT
|
||||
Id,
|
||||
barcode as 原条码,
|
||||
ContactNumber as 联络单号,
|
||||
ItemNumber as 件号,
|
||||
CreateTime as 创建时间
|
||||
FROM normaltemperature
|
||||
ORDER BY Id DESC
|
||||
LIMIT 10;
|
||||
|
||||
-- 7. 查看scandata最新10条记录
|
||||
SELECT
|
||||
Id,
|
||||
barcode as 原条码,
|
||||
ContactNumber as 联络单号,
|
||||
ItemNumber as 件号,
|
||||
CreateTime as 创建时间
|
||||
FROM scandata
|
||||
ORDER BY Id DESC
|
||||
LIMIT 10;
|
||||
|
||||
-- 8. 检查数据一致性(barcode应该等于ContactNumber-ItemNumber)
|
||||
SELECT
|
||||
'数据一致性检查' as 检查项,
|
||||
COUNT(*) as 不一致记录数
|
||||
FROM normaltemperature
|
||||
WHERE barcode IS NOT NULL
|
||||
AND ContactNumber IS NOT NULL
|
||||
AND ItemNumber IS NOT NULL
|
||||
AND barcode != CONCAT(ContactNumber, '-', ItemNumber);
|
||||
|
||||
-- 9. 查找可能有问题的数据
|
||||
SELECT
|
||||
Id,
|
||||
barcode,
|
||||
ContactNumber,
|
||||
ItemNumber,
|
||||
CONCAT(ContactNumber, '-', ItemNumber) as 应该的barcode
|
||||
FROM normaltemperature
|
||||
WHERE barcode IS NOT NULL
|
||||
AND ContactNumber IS NOT NULL
|
||||
AND ItemNumber IS NOT NULL
|
||||
AND barcode != CONCAT(ContactNumber, '-', ItemNumber)
|
||||
LIMIT 10;
|
||||
|
||||
-- =====================================================
|
||||
-- 验证完成
|
||||
-- =====================================================
|
||||
Reference in New Issue
Block a user