Click here to Skip to main content
Click here to Skip to main content

How to improve your LINQ query performance by 5 X times?

, 16 Jul 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Tips to improve your LINQ query performance.

Table of contents

Introduction and goal

LINQ has been criticized by many early adopters for its performance issues. Well, if you are just going to drag and drop using a DBML code generator, I am sure you will land up in a mess. Try doing this, make a simple LINQ to SQL project using DBML and see your SQL profiler. I am sure you will never like to touch a DBML code generator again.

In this article, we will first look into how LINQ queries are executed, and then we will touch base on how compiled LINQ queries can help us improve our application performance at least 5 times. My numbers can be 10% up and down as I had come to that figure using my environmental situations.

Still new to LINQ? Below are some real quick starters

This article requires some pre-requisites, in case you are new to LINQ I would suggest you to go through the below links.

Deep dive into how LINQ queries work

Before we get into how we can improve LINQ query performance, let’s first try to understand the various steps involved in a LINQ query execution. All LINQ queries are first converted to SQL statements. This conversion also involves checking of LINQ query syntaxes and translating the queries to SQL.

Below is a simple LINQ query which selects data from a customer table. This LINQ query is then transformed into the necessary SQL statements by the LINQ engine.

The checking of syntaxes and generating the SQL query accordingly is a bit of a tedious job. This task is performed every time we fire a LINQ query. So if we can cache the LINQ query plan, we can execute much faster.

LINQ has provided something called as compiled LINQ queries. In compiled LINQ queries, the plan is cached in a static class. As we all know, a static class is a global cache. So LINQ uses the query plan from the static class object rather than building and preparing the query plan from scratch.

Figure: LINQ query caching

In all, there are four steps which need to be performed right from the time LINQ queries are built till they are fired. By using compiled LINQ queries, the four steps are reduced to two.

Figure: Query plan bypasses many steps

Steps involved in writing compiled LINQ queries

The first thing to do is to import the Data.Linq namespace.

Import namespace using System.Data.Linq;

The syntax to write compiled queries is a bit cryptic and many developers do not like the way the syntax is. So let us break the syntax into small pieces and then we will try to see how the complete syntax looks like. To execute a compiled function, we need to write a function to pointer. This function should be static so that the LINQ engine can use the query plan stored in the static class objects. Below is how we define the function. It starts with public static stating that this function is static. Then we use the Func keyword to define the input parameters and output parameters. Below is how the parameter sequence needs to be defined:

  • The first parameter should be a data context. So we have defined the data type as DataContext.
  • Followed by one or many input parameters. Currently we have only one, i.e., customer code, so we have defined the second parameter data type as string.
  • Once we are done with all input parameters, we need to define the data type of the output. Currently we have defined the output data type as IQueryable.

We have named this delegate function getCustomers.

public static Func<DataContext, string, IQueryable<clsCustomerEntity>> getCustomers

We need to call the method Compiled of the static class CompiledQuery with the DataContext object and necessarily define input parameters followed by the LINQ query. For the below snippet, we have not specified the LINQ query to minimize complications.

CompiledQuery.Compile((DataContext db, string strCustCode)=> Your LINQ Query );

Uniting the above two code snippets, below is how the complete code snippet looks like:

public static Func<DataContext, string, IQueryable<clsCustomerEntity>>
getCustomers= CompiledQuery.Compile((DataContext db, string strCustCode)=> Your LINQ Query );

We then need to wrap this static function in a static class. We have taken the above defined function and wrapped that function in a static class clsCompiledQuery.

public static class clsCompiledQuery
    public static Func<DataContext, string, IQueryable<clsCustomerEntity>>
    getCustomers = CompiledQuery.Compile((DataContext db, string strCustCode)
        => from objCustomer in db.GetTable<clsCustomerEntity>()
        where objCustomer.CustomerCode == strCustCode
        select objCustomer);

Consuming the compiled query is pretty simple; we just call the static function. Currently, this function returns the data type IEnumerable. We have to define an IEnumerable customer entity which will be flourished through the getCustomers delegate function. We can loop through the customer entity using the clsCustomerEntity class.

IQueryable<clsCustomerEntity> objCustomers = 
    clsCompiledQuery.getCustomers(objContext, txtCustomerCode.Text);
foreach (clsCustomerEntity objCustomer in objCustomers)
    Response.Write(objCustomer.CustomerName + "<br>");

Performance comparison

Out of curiosity, we thought to do some kind of comparison to see how much the performance difference is. We took a simple customer table with 3000 records in it and we ran a simple query on the customer code. We have attached the sample source with the article. Below is a simple screenshot:

What we have done in this project is we have executed a LINQ SQL without query compilation and with query compilation. We have recorded the time using the System.Diagnostic.StopWatch class. Here’s how the performance recording has taken place. We start the stop watch, run the LINQ SQL without compile, and then we stop the watch and record the timings. The same way we have recorded the performance LINQ query with compilation.

We create the data context object and start the stop watch.

System.Diagnostics.Stopwatch objStopWatch = new System.Diagnostics.Stopwatch(); 
DataContext objContext = new DataContext(strConnectionString);

We run the LINQ query without compilation. After execution, stop the watch and record the time differences.

var MyQuery = from objCustomer in objContext.GetTable<clsCustomerEntity>()
where objCustomer.CustomerCode == txtCustomerCode.Text
select objCustomer;
foreach (clsCustomerEntity objCustomer in MyQuery)
    Response.Write(objCustomer.CustomerName + "<br>");
Response.Write("The time taken to execute query without compilation is : " + 
objStopWatch.ElapsedMilliseconds.ToString() + " MillionSeconds<br>");

Now we start the stop watch again, run the LINQ query with compilation, and record the time taken.

IQueryable<clsCustomerEntity> objCustomers = 
    clsCompiledQuery.getCustomers(objContext, txtCustomerCode.Text);
foreach (clsCustomerEntity objCustomer in objCustomers)
    Response.Write(objCustomer.CustomerName + "<br>");
Response.Write("The time taken to execute query with compilation is : " + 
   objStopWatch.ElapsedMilliseconds.ToString() + " MillionSeconds");

Analyzing the results

When we measure performance, we need to see the time of execution the first time as well as subsequent times. At least 8 recordings are needed so that any kind of .NET runtime performance is averaged out. There are two important points we can conclude from the experiment:

  • We need to excuse the first reading as there can be a lot of .NET Dramework object initialization. It can lead to a lot of wrong conclusions as there is a lot of noise associated with the first run.
  • The subsequent readings have the real meat difference. The average difference between them is 5 times. In other words, a LINQ query executed using no compilation is 5 ms slower than compiled LINQ queries.
  No Compilation Milliseconds Query Compilation
First time 4 124
Second time 9 2
Third time 7 2
Fourth time 7 1
Fifth time 6 2
Sixth time 7 2
Seventh time 6 2
Eight time 6 2

Below is a graphical representation and you can see how compiled queries have better performance than non-compiled ones.

Hardware and software configuration used for test conduction

  • The web application and database application where on different boxes.
  • Web application was running on Windows XP using a simple personal web server provided by VS 2008 (sorry for that guys, but did not have other options at that moment). Web application PC hardware configuration was 2 GB RAM, P4, 80 GB hard disk.
  • Database was SQL Server 2005 on a Windows 2003 Server with 2 GB RAM, P4, 80 GB hard disk.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shivprasad koirala
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers

Comments and Discussions

General5 PinmemberOmar Gameel Salem26-Feb-14 23:24 
GeneralMy vote of 4 PinmemberGuillaume Ranslant29-Nov-11 3:12 
QuestionGeneric method for compiled Linq ? Pinmemberyaweriqbal4-Jul-11 0:36 
QuestionWhy CompiledQuery fast? Pinmemberdevvvy17-Mar-11 23:39 
GeneralVery good and neat article! PinmemberVirat Kothari27-Jan-11 18:20 
GeneralMy vote of 5 PinmemberSohelElite8-Jan-11 0:25 
GeneralMy vote of 5 PinmemberRuna Jack6-Oct-10 19:20 
Generalawesome PinmemberPranay Rana26-May-10 0:00 
QuestionWhat happened if i used Stored procedures with Linq !? Pinmemberthe black angel28-Feb-10 23:47 
AnswerRe: What happened if i used Stored procedures with Linq !? PinmvpShivprasad koirala1-Mar-10 5:32 
GeneralRe: What happened if i used Stored procedures with Linq !? Pinmemberneoandrew27-Apr-10 6:15 
GeneralDrop LINQ! Pinmemberare_all_nicks_taken_or_what21-Sep-09 6:21 
GeneralRe: Drop LINQ! Pinmemberemission1-Nov-09 10:17 
GeneralRe: Drop LINQ! PinmemberGuillaume Ranslant29-Nov-11 3:17 
GeneralRe: Drop LINQ! Pinmemberjohannesnestler8-Mar-12 4:34 
GeneralRe: Drop LINQ! PinmemberHaBiX17-May-12 21:04 
GeneralRe: Drop LINQ! PinmemberAndy Missico25-May-12 10:41 
Stored procedures are a technology with significant limitations. There has been no big improvement in years and years. There is a complete disconnect between the script, the database, and the business logic.

LINQ is about building expressions that work against multiple types of data and data providers. Which is something stored procedures were never designed to do.

Before you reply with some out of context argument, think about the limitations of a relational database and the advantages that LINQ2SQL brings.
GeneralLinq project is dropped by Microsoft, this article has no relevance ignore this article, dont give vote to this article PinmemberMember 639203619-Aug-09 20:39 
GeneralRe: Linq project is dropped by Microsoft PinmemberShivprasad koirala19-Aug-09 23:06 
GeneralHere it is PinmemberMember 639203620-Aug-09 0:23 
GeneralRe: Here it is PinmemberMREcoolio17-Sep-09 14:53 
Generalstop drinking Kool Aid Pinmemberdmihailescu19-Feb-10 12:17 
GeneralSee also PinmemberGünther M. FOIDL28-Jul-09 5:59 
GeneralOne more point of consideration PinmemberRahul D.27-Jul-09 4:57 
GeneralWon't work for dynamically composed queries PinmemberMR_SAM_PIPER20-Jul-09 15:18 
GeneralRe: Won't work for dynamically composed queries PinmemberShivprasad koirala20-Jul-09 17:42 
GeneralRe: Won't work for dynamically composed queries PinmemberMR_SAM_PIPER20-Jul-09 21:19 
GeneralRe: Won't work for dynamically composed queries PinmemberShivprasad koirala20-Jul-09 21:48 
GeneralMy vote of 1 PinmemberVivek Thakur20-Jul-09 4:13 
GeneralRe: My vote of 1 PinmemberShivprasad koirala20-Jul-09 4:51 
GeneralCongrats for becoming Microsoft MVP and one more great article Pinmembergurungdesai18-Jul-09 16:11 
GeneralRe: Congrats for becoming Microsoft MVP and one more great article PinmemberShivprasad koirala20-Jul-09 5:00 
GeneralMy vote of 2 PinmemberTheArchitectualizer18-Jul-09 6:30 
GeneralRe: My vote of 2 [modified] PinmemberShivprasad koirala18-Jul-09 15:48 
AnswerRe: My vote of 2 [modified] PinmemberTheArchitectualizer18-Jul-09 21:41 
GeneralRe: My vote of 2 PinmemberShivprasad koirala19-Jul-09 19:44 
GeneralMisleading conclusions PinmemberGuinnessKMF17-Jul-09 9:24 
GeneralRe: Misleading conclusions PinmemberShivprasad koirala17-Jul-09 18:03 
GeneralRe: Misleading conclusions [modified] PinmemberGuinnessKMF20-Jul-09 4:37 
GeneralRe: Misleading conclusions PinmemberShivprasad koirala20-Jul-09 4:53 
GeneralRe: Misleading conclusions Pinmemberkensurferca18-Feb-10 7:47 
GeneralRe: Misleading conclusions [modified] PinmemberTheArchitectualizer18-Jul-09 6:33 
GeneralRe: Misleading conclusions PinmemberShivprasad koirala18-Jul-09 15:48 
GeneralRe: Misleading conclusions PinmemberTheArchitectualizer18-Jul-09 22:12 
GeneralRe: Misleading conclusions PinmemberShivprasad koirala19-Jul-09 19:42 
GeneralStored Procedure and Linq PinmemberAhasanhabib16-Jul-09 19:11 
GeneralRe: Stored Procedure and Linq PinmemberShivprasad koirala16-Jul-09 19:28 
GeneralTnx a lot PinmemberVMykyt16-Jul-09 6:20 
GeneralLINQ vs. Stored Procedures PinmemberYury Revotyuk16-Jul-09 0:21 
AnswerRe: LINQ vs. Stored Procedures Pinmemberkct16-Jul-09 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 16 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid