Tuesday, December 7, 2010

Quick Tip: LINQ Queries Require Optimization Too.

Recently I was asked by a colleague to look at a poorly performing LINQ query that was taking an unreasonable amount of time to execute (> 3 min). It didn't require much investigation to see that the query needed reordering of its joins and limiting of the elements returned. Once we rearranged the joins (inner joins first!) and limited the elements returned in the select, the query times dropped to under a sec.

This made me review some of my own LINQ queries and sure enough I had often made the same mistakes as my colleague did.  I bet if you review your own LINQ queries you may find the same issues.

Lesson learned by me today?   You should take care of how you form your LINQ statements.  LINQ is just different enough to make you lose sight of the fact its a query language.

See my earlier post on SQL query optimization. The tips outlined in this post will apply to LINQ queries as well.  

No comments:

Post a Comment