[後端] 優化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`)
) 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<NiagaraTags> result;
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb.Append($@" SELECT m.*
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");
WHERE NOT EXISTS (
SELECT 1
FROM device d
WHERE m.niagara_tags = d.device_number
);");
result = (await conn.QueryAsync<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
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<NiagaraTags>(sb.ToString())).ToList<NiagaraTags>();
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<NiagaraTagsForItem>(sb.ToString())).ToList<NiagaraTagsForItem>();
sb.Clear();