LINQ-to-SQL - 什么时候访问数据库?
在我的 asp.net mvc 2 应用程序中,我有一个数据量很大的操作。我使用 linq-to-sql 进行查询,同时使用存储库模式。 当我在 SQL Server Profiler 中检查操作的执行情况时,我发现大约有 60 个查询正在执行。我的问题是 - 我希望问题中有足够的信息来回答它 - asp.net mvc 是否对每个查询进行一次完整的往返,来回和第四次到数据库,或者是因为使用linq,实际上只有一次往返?
In my asp.net mvc 2 app I have an action which is rather data-heavy. I'm using linq-to-sql for the queries, while using the repository pattern.
When I check the execution of the action in SQL Server profiler, I find that approximately 60 queries are being executed. My question is - and I hope there is enough infomation in the question to answer it - does asp.net mvc make a full round trip, back and fourth, to the database, for each and every query, or is it that because of the use of linq, there is actually only one round trip ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信每个查询都是一次往返,无论是 LINQ 查询还是原始 SQL。我想这取决于您所说的“每个查询”的含义。
要回答您上面的评论:
var result = LINQ; <--- 查询已生成,但未执行
foreach( YourDataType ydt in result ) { <--- 查询在您对结果执行某些操作时执行
...
}
I believe it's one round trip per query, whether that is a LINQ query or raw SQL. I suppose it depends on what you mean by "each and every query."
To answer your comment above:
var result = LINQ; <--- Query is generated, but not executed
foreach( YourDataType ydt in result ) { <--- Query executes when you do something with result
...
}
这取决于您的 Linq 的外观。据我了解,当您的结果集枚举时,您将访问数据库。
It depends on what your Linq looks like. It's my understanding that when your result set enumerates you will hit the database.