David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
LinqDataSource And How You Can *Maximize* Roundtrips to Your Database

I was reading Scott Guthrie's excellent series of posts on LINQ when I came to the following post that doesn't quite work the way you would think and will actually kill the performance of your ASP.NET Web Application:

LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)

When you select the asterisk when configuring the LinqDataSource it only relates to columns not relationships. So, when I select the "*" when configuring the LinqDataSource, the Blog Class for which each Category has a 1:1 relationship will not be loaded during a select. Only all the columns are grabbed, no relationships.

 

 LinqDataSource

 

If you then display all the categories in a GridView and then replace the foreignkey BlogId with say the title of the Blog as a Template Column:

 

<asp:TemplateField HeaderText="Blog" SortExpression="Blog.Title">
    <ItemTemplate>
        <%# Eval("Blog.Title") %>
    </ItemTemplate>
</asp:TemplateField>

 

You are traversing a relationship which will be demand loaded at that moment, causing another roundtrip to the database for each category being displayed in the GridView. So, what might seem like 1 database roundtrip to populate your GridView will actually be 51 roundtrips to the database if you are displaying 50 categories in the GridView. Checking this out with Sql Server Profiler verifies this quite easily.

If you definitely want to replace those foreign keys with more sensible values, you cannot use that "*", at least not in Visual Studio 2008 beta 2. You will need to select the columns you want to display including the relationship to avoid all the extra roundtrips.

 

 LinqDataSource2

 

The above will now only cause 1 rountrip to the database to fill out the GridView.

Of course, since we did not choose "*" we lose the option of enabling edits and deletes via the Visual Designer(s), which brings up a whole other problem. The LinqDataSource could use a few enhancements to make all this a bit more intuitive when released.

 

by David Hayden

 


Posted 08-03-2007 1:00 PM by David Hayden

[Advertisement]

Comments

Ayende Rahien wrote re: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database
on 08-03-2007 3:13 PM

That is the classic SELECT N+1 in ORM

That is a somewhat reasonable decision to make, since otherwise you would run into the other issue, The Entire Database In Memory.

But how would you handle three levels?

Category.Blog.Author.Name ?

Scottgu wrote re: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database
on 08-03-2007 3:20 PM

My next blog post is going to be covering the DataContext shaping support with LINQ to SQL.

You can use this to override the default lazy load semantics that you might have indicated in your model.

Because this can be configured per DataContext usage, it ends up being pretty flexible.  You can configure this both via code as well as when using the LinqDatasource.

Stay tuned to my blog for a post in the next few days on this.

Thanks,

Scott

David Hayden wrote re: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database
on 08-03-2007 4:51 PM

I figured it out Scott. It was driving me crazy:

davidhayden.com/.../LinqDataSourceHighPerformanceQueriesDataLoadOptionsAvoidingDatabaseRoundtrips.aspx

I look forward to your more precise take on the solution if it is similar to mine.

David Hayden wrote re: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database
on 08-03-2007 4:55 PM

Ayende,

I would assume that you just traverse the relationships as you mention. I don't have anything to test and verify, but your conclusion seems logical since that is how you do it for 2.

Ayende Rahien wrote re: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database
on 08-03-2007 5:22 PM

David,

That is actually a more difficult problem, because now you need to fetch data from association tables along the way.

It makes the query more complicated, and the way to specify such constraint is extremely important.

Because it has such an affect on perf.

David Hayden [MVP C#] wrote LINQ To SQL Query Tuning for Performance Optimization
on 08-05-2007 9:56 PM

In my last post: LinqDataSource And How You Can *Maximize* Roundtrips to Your Database I discussed how

Christopher Steen wrote Link Listing - August 6, 2007
on 08-07-2007 1:07 AM

Link Listing - August 6, 2007