Trips to the database

I have a general performance question that needs clarification. Assuming an object graph of Customer, Orders, OrderDetails and assume I am retrieving one customer with 10 orders and each order has 5 orderdetails, how many trips to the database will be required? Is it 3 or 12?

Thanks,
Dave Foderick

[305 byte] By [DaveFoderick] at [2008-3-6]
# 1
Deferred execution will run more queries, but will only do it when needed. This is the default for most things you have seen, most likely. For example, in the following LINQ query, a SQL query will be fired for each customer to get the orders for that customer:

var q = from c in db.Customers select new {c.CustomerID, c.Orders};
ObjectDumper.Write(q, 1);

Immediate execution will run the queries in 1 round trip, and return the populated object graph to you in one trip. You can tell DLINQ to use Immediate execution by using the Including operator, which looks something like this:

var q = (from c in db.Customers select new {c.CustomerID, c.Orders}).Including(c => c.Orders);
ObjectDumper.Write(q, 1);

Including() can be nested to force loading the entire graph.

Hope that helps.

Dan Miser

DanMiser at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 2
I'm talking about the non-deferred case. I'm also talking about non-projection where the relationship between customers and orders is specified in the attributes or generics, after all the whole point of OR Mapping is to do declarative, not imperative, programming.

I'm highly sceptical that (d)linq knows which orderdetails to load apriori. I guess I'll have to try it myself.

-Dave Foderick

DaveFoderick at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 3
Taking a guess I would say 12 as I don't see that complexity implemented.
Mihies at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...
# 4
If you use the Including() operator to specify immediate loading of Orders and OrderDetails then you will get all your data in 3 queries. If you don't use Including, it will take many more.
MattWarren at 2007-9-9 > top of Msdn Tech,Visual Studio Orcas,LINQ Project General...

Visual Studio Orcas

Site Classified