C#ADO:优化查询及其性能

问题描述 投票:0回答:1

我有一个C#RestApi,它需要通过执行如下查询来使用ADO从数据库中提取记录的信息:

declare @id1 int 
set @id1 =  (select id from myTable where col1= x1, col2 = y1, col3 = z1 from mappingTable)

declare @id2 int 
set @id2 =  (select id from myTable where col1= x2, col2 = y2, col3 = z2 from mappingTable)

declare @id3 int 
set @id3 =  (select id from myTable where col1= x3, col2 = y3, col3 = z3 from mappingTable)
.
.
.
declare @idN int 
set @idN =  (select id from myTable where col1= xN, col2 = yN, col3 = zN from mappingTable)

select @id1,@id2,@id3, ..., @idN

我在上面运行的查询中使用ADO.NET SqlCommand在其中运行N个查询并读取结果。我有两个问题:

  1. 使用单独的SqlCommand运行每个查询是否会导致性能下降?通常,在I / O任务中,执行许多小型I / O任务的性能要比在一批I / O任务中运行所有小I / O任务的性能低,但是我不知道在Databases和ADO中是否存在相同的情况。
  2. 是否有更好的方法通过更好的SQL查询提取相同的结果?换句话说,我可以用其他方式编写此查询来以更好的性能运行它吗?

注意:在上面的查询中,所有查询中的列和表都相同,只是对where子句中的值进行了修改。

c# sql sql-server ado.net asp.net-apicontroller
1个回答
0
投票

使用表值参数来存储xyz的相应值。使用单个查询,在其中将mappingTable与该表值参数进行内部联接。

© www.soinside.com 2019 - 2024. All rights reserved.