From f2d6f711f0aea8f4541d66518ce1c8bd27275aeb Mon Sep 17 00:00:00 2001 From: dev02 Date: Wed, 19 Jul 2023 13:26:30 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E7=AB=AF]=20=E5=84=AA=E5=8C=96sql?= =?UTF-8?q?=E8=AA=9E=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NiagaraDataSynchronizeRepository.cs | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index d5f2077..f00747a 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -57,7 +57,7 @@ namespace Repository.BackendRepository.Implement PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; 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); } @@ -200,7 +200,7 @@ namespace Repository.BackendRepository.Implement PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; 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); } #endregion @@ -280,18 +280,20 @@ namespace Repository.BackendRepository.Implement using (IDbConnection conn = GetDbConnection()) { conn.Open(); - using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled, new TimeSpan(0, 0, 200))) + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) { try { List result; StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); - sb.Append($@" SELECT m.* - FROM import_niagara_tag m - LEFT JOIN device d - ON m.niagara_tags = d.device_number - WHERE d.device_number IS NULL"); + sb.Append($@"SELECT m.* + FROM import_niagara_tag m + WHERE NOT EXISTS ( + SELECT 1 + FROM device d + WHERE m.niagara_tags = d.device_number + );"); result = (await conn.QueryAsync(sb.ToString())).ToList(); sb.Clear(); @@ -366,9 +368,13 @@ namespace Repository.BackendRepository.Implement #endregion #region device_node 新增 - sb.Append($@" SELECT m.* FROM import_niagara_tag m - LEFT JOIN device_node d ON m.niagara_tags = d.device_number - WHERE d.device_number IS NULL"); + sb.Append($@" SELECT m.* + FROM import_niagara_tag m + WHERE NOT EXISTS ( + SELECT 1 + FROM device_node d + WHERE m.niagara_tags = d.device_number + );"); result = (await conn.QueryAsync(sb.ToString())).ToList(); sb.Clear(); @@ -413,7 +419,7 @@ namespace Repository.BackendRepository.Implement UPDATE device SET is_link = 0 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(); @@ -422,7 +428,7 @@ namespace Repository.BackendRepository.Implement UPDATE device_node SET is_link = 0 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) { @@ -455,7 +461,7 @@ namespace Repository.BackendRepository.Implement FROM import_niagara_item m 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 - WHERE d.points IS NULL"); + WHERE d.points IS NULL;"); result = (await conn.QueryAsync(sb.ToString())).ToList(); sb.Clear();