简体   繁体   English

SQL查询性能-哪种方法更好?

[英]SQL Query Performance - Which approach is better if any?

I am having to return several record sets from SQL Server to constructs a C# object. 我必须从SQL Server返回几个记录集以构造一个C#对象。 While EF doesn't currently support (possibly the beta version) returning complex objects like this I am having to resort to returning a DataSet using ADO.NET to retrieve the data before transforming it into a pleasant C# representation, see below. 尽管EF目前不支持(可能是beta版)返回此类复杂对象,但我不得不诉诸使用ADO.NET返回DataSet来检索数据,然后再将其转换为舒适的C#表示形式,请参见下文。

SELECT * FROM ...
exec dbo.usp_SP1 @ProductID,@CatalogName
exec dbo.usp_SP2 @ProductID,@CatalogName    

Its always better to make fewer database calls however due to how this query is being executed would making several requests for each DataSet be that much worst? 减少数据库调用总是更好的方法,但是由于此查询的执行方式,对每个DataSet的多个请求会变得更糟吗?

One database roundtrip is always better that three (or two). 一个数据库往返总是比三个(或两个)更好。

Your code could be clean even with this set of queries. 即使使用这组查询,您的代码也可能是干净的。 You just have to call DataTable.Load three times, in succession, to load all three results sets from the DbDataReader . 您只需要连续调用DataTable.Load三次,即可从DbDataReader加载所有三个结果集。

So yes, I recommend one bigger query and a single DataSet for all result sets. 因此,是的,我建议对所有结果集使用一个更大的查询和一个DataSet

You can reference each table result inside of a dataset . 您可以在dataset引用每个表结果。 ds.Tables[index].Rows... . ds.Tables[index].Rows... So I would make one call, get all the data and return it via whether you call another sproc or just a simple SELECT . 因此,我将进行一次调用,获取所有数据,然后通过调用另一个sproc还是简单的SELECT返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM