SubSonic 读取连接表的成本
假设我现在有以下模式,
table A:
ID int primary key
value varchar(255) not null
table B:
ID int primary key
AID foreign key refences A (ID)
name varchar(40) not null
当我执行以下亚音速 linq 时,
var items =
from a in A.All()
where any-condition select a;
一切都很好。
杀手是当我执行以下操作时,
for(var item in items)
{
for(var nestedItem in item.B) // troubling!
{
DoSomething(nestedItem)
}
}
我不太熟悉内部工作原理,但我很确定它会额外访问数据库以获取连接的表行。
您能告诉我如何避免如此昂贵的旅行吗?
Assuming I have the following schema
table A:
ID int primary key
value varchar(255) not null
table B:
ID int primary key
AID foreign key refences A (ID)
name varchar(40) not null
now when I execute the following subsonic linq
var items =
from a in A.All()
where any-condition select a;
all is fine.
the killer is when I do the following
for(var item in items)
{
for(var nestedItem in item.B) // troubling!
{
DoSomething(nestedItem)
}
}
I am not very familiar with the inner workings, But I am pretty sure it does additional trips to the DB to get the joined table rows.
Can you please tell me how can I avoid such expensive trips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 ORM 的一个经典问题 - SELECT N + 1。您遇到的是延迟加载与急切加载,在这种情况下,您需要执行某种外连接(使用查询工具)或将 B 拉入一个清单。
This is a classic problem with ORMs - SELECT N + 1. What you're coming up against is Lazy vs. Eager loading and in this case you need to either do some kind of outer join (using the query tool) or pull B into a list.