简体   繁体   English

在Linq-to-SQL中,IMultipleResults是否多次访问数据库?

[英]In Linq-to-SQL, does IMultipleResults hit the database more than once?

I have a stored proc as follows : 我有一个存储过程如下:

CREATE PROCEDURE GetMultipleResults
   @SomeID int
AS
BEGIN
   SELECT * FROM SomeTable1 where SomeColumn1 = @SomeID
   SELECT * FROM SomeTable2 where SomeColumn2 = @SomeID
   SELECT * FROM SomeTable3 where SomeColumn3 = @SomeID
   SELECT * FROM SomeTable4 where SomeColumn4 = @SomeID
END

Then I use the following code to execute and retrieve the data: 然后我使用以下代码来执行和检索数据:

[Function(Name = "dbo.SPROCName")]
[ResultType(typeof(ResultSet1))]
[ResultType(typeof(ResultSet2))]
[ResultType(typeof(ResultSet3))]
[ResultType(typeof(ResultSet4))]
public IMultipleResults SomeMethod([Parameter(DbType = "INT")] int? SomeID
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), SomeID);
    return ((IMultipleResults)(result.ReturnValue));
}

Now finally, to get each result, I execute something like : 现在最后,为了获得每个结果,我执行以下操作:

public void SomeOtherMethod(int SomeID)
{
    DataContext1 context = new DataContext1 (dbConnString);
    IMultipleResults results = context.SomeMethod(SomeID);

    ResultSet1 resultSet1= results.GetResult().FirstOrDefault();
    IEnumerable resultSet2 = results.GetResult().ToList();
    IEnumerable resultSet3 = results.GetResult().ToList();
    ResultSet4 resultSet4= results.GetResult().FirstOrDefault();
}

My question is, does Linq-to-SQL execute the entire stored procedure once, then the GetResult() is used to get it from memory, or does it execute n queries (round trip db calls) where n is the number of select statements in the stored proc (4 in this example)? 我的问题是,Linq-to-SQL执行整个存储过程一次,然后GetResult()用于从内存中获取它,或者它是否执行n个查询(往返db调用),其中n是select语句的数量在存储过程中(本例中为4)?

The reason I'm asking is because the following line almost takes the same amount of time to execute even for large data: 我问的原因是因为即使对于大数据,以下行几乎花费相同的时间来执行:

IMultipleResults results = context.SomeMethod(SomeID);

but the following part takes some noticeable time when run where resultSet3 is a large amount of data : 但是当运行resultSet3是大量数据时,以下部分需要一些明显的时间:

IEnumerable resultSet3 = results.GetResult().ToList();

There will certainly only be one round-trip to the database. 肯定只有一次数据库往返。 LinqToSQL has no visibility of what's inside the stored procedure it has to rely on the ResultType attributes you have applied being correct. LinqToSQL无法看到存储过程中的内容,它必须依赖于您应用的ResultType属性是否正确。

Internally LinqToSQL will execute the stored procedure and then open a data reader to retrieve the results. 内部LinqToSQL将执行存储过程,然后打开数据读取器以检索结果。

For the FirstOrDefault cases it will retrieve the first item (if there is one) and then skip to the next result set using IDataReader.NextResult . 对于FirstOrDefault案例,它将检索第一个项目(如果有的话),然后使用IDataReader.NextResult跳转到下一个结果集。 So even if resultSet1 is potentially large only the first row needs fetched over the network. 因此,即使resultSet1可能很大,只需要通过网络获取第一行。

For the ToList cases the entire result set will be retrieved by the reader. 对于ToList情况,读者将检索整个结果集。 So if there's lot of data it will necessarily take longer. 因此,如果有大量数据,则必然需要更长时间。

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

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