Saturday, October 31, 2009

The need of DataLoadOptions

=======
Problem
=======
If you run the following C# code on Northwind database, there will be one query sent to db to get the Customers needed and also one seperate query for each customer to get the individual order count:
-------------------------
var query = from c in db.Customers
where c.CustomerID.StartsWith("A")
select c;

foreach (var c in query){ Console.WriteLine( c.ContactName + " " + c.Orders.Count);}
-------------------------
It can be a lot of queries if customers are large.
============================================================
Witout using DataLoadOptions, we can do the same using single query as below:
=======================================================
------------------------------
var query = from c in db.Customers
where c.CustomerID.StartsWith("A")
select new {name=c.ContactName, count=c.Orders.Count};

foreach (var c in query){ Console.WriteLine( c.name + " " + c.count);}
------------------------------
The above code makes difference because we added the query needed to get the count into the same query for getting Customer. LINQ to SQL is smart enough to parse it into a single query, which has the same effect as seting the DataLoadOptions.
=============================================
Using the following code, you can get Customers with all their Orders in one query just like by setting DataLoadOptions to Orders:
============================================
---------------------------------------
var query = from c in db.Customers
where c.CustomerID.StartsWith("A")
select new {name=c.ContactName, count=c.Orders};
--------------------------------------
Then you can access related Orders collection in you code by looping through the anonimous objects generated.

Saturday, October 3, 2009

Power of SQL indexing

I run a SQL script on a server with 8 CPUs. The script took exceptionally long when updating two ntext fields in a table that has more than 3,000,000 records.
Before adding a nonclustered index:
The server showed 100% cpu committed and updated about 20,000 rows/hr
After adding a nonclusterd index:
The server updated 15,000 rows/min(not hr!).
I was shocked by such a big improvement.