[後端] 優化sql語法

This commit is contained in:
dev02 2023-07-19 13:26:30 +08:00
parent b4e4321453
commit f2d6f711f0

View File

@ -57,7 +57,7 @@ namespace Repository.BackendRepository.Implement
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
sql = "delete from import_niagara_tag where device_building_tag = '" + b + "'"; sql = "delete from import_niagara_tag where device_building_tag = '" + b + "' limit 10000";
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
@ -200,7 +200,7 @@ namespace Repository.BackendRepository.Implement
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
sql = "delete from import_niagara_item where device_building_tag = '" + b + "'"; sql = "delete from import_niagara_item where device_building_tag = '" + b + "' limit 10000;";
await conn.ExecuteAsync(sql); await conn.ExecuteAsync(sql);
} }
#endregion #endregion
@ -280,7 +280,7 @@ namespace Repository.BackendRepository.Implement
using (IDbConnection conn = GetDbConnection()) using (IDbConnection conn = GetDbConnection())
{ {
conn.Open(); conn.Open();
using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled, new TimeSpan(0, 0, 200))) using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled))
{ {
try try
{ {
@ -289,9 +289,11 @@ namespace Repository.BackendRepository.Implement
StringBuilder sb2 = new StringBuilder(); StringBuilder sb2 = new StringBuilder();
sb.Append($@"SELECT m.* sb.Append($@"SELECT m.*
FROM import_niagara_tag m FROM import_niagara_tag m
LEFT JOIN device d WHERE NOT EXISTS (
ON m.niagara_tags = d.device_number SELECT 1
WHERE d.device_number IS NULL"); FROM device d
WHERE m.niagara_tags = d.device_number
);");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear(); sb.Clear();
@ -366,9 +368,13 @@ namespace Repository.BackendRepository.Implement
#endregion #endregion
#region device_node #region device_node
sb.Append($@" SELECT m.* FROM import_niagara_tag m sb.Append($@" SELECT m.*
LEFT JOIN device_node d ON m.niagara_tags = d.device_number FROM import_niagara_tag m
WHERE d.device_number IS NULL"); WHERE NOT EXISTS (
SELECT 1
FROM device_node d
WHERE m.niagara_tags = d.device_number
);");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>(); result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
sb.Clear(); sb.Clear();
@ -413,7 +419,7 @@ namespace Repository.BackendRepository.Implement
UPDATE device UPDATE device
SET is_link = 0 SET is_link = 0
WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);"); WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);");
await conn.ExecuteAsync(sb.ToString(), commandTimeout: 100); await conn.ExecuteAsync(sb.ToString());
sb.Clear(); sb.Clear();
@ -422,7 +428,7 @@ namespace Repository.BackendRepository.Implement
UPDATE device_node UPDATE device_node
SET is_link = 0 SET is_link = 0
WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);"); WHERE is_link = 1 AND device_number NOT IN (SELECT niagara_tags FROM import_niagara_tag);");
await conn.ExecuteAsync(sb.ToString(), commandTimeout: 100); await conn.ExecuteAsync(sb.ToString());
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -455,7 +461,7 @@ namespace Repository.BackendRepository.Implement
FROM import_niagara_item m FROM import_niagara_item m
LEFT JOIN device_item d LEFT JOIN device_item d
ON m.device_system_tag = d.device_system_tag and m.device_name_tag = d.device_name_tag and m.device_point_name = d.points and m.device_building_tag collate utf8mb4_0900_ai_ci = d.device_building_tag ON m.device_system_tag = d.device_system_tag and m.device_name_tag = d.device_name_tag and m.device_point_name = d.points and m.device_building_tag collate utf8mb4_0900_ai_ci = d.device_building_tag
WHERE d.points IS NULL"); WHERE d.points IS NULL;");
result = (await conn.QueryAsync<NiagaraTagsForItem>(sb.ToString())).ToList<NiagaraTagsForItem>(); result = (await conn.QueryAsync<NiagaraTagsForItem>(sb.ToString())).ToList<NiagaraTagsForItem>();
sb.Clear(); sb.Clear();