Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:40:16+00:00 2026-06-01T12:40:16+00:00

I was referring to an article which focuses on Speeding up LINQ to SQL

  • 0

I was referring to an article which focuses on Speeding up LINQ to SQL Queries. One of the techniques it mentions is “Use Compiled Queries” and explains how to use it.

I wanted to see performance improvement of compiled queries and hence i tried the same example provided by the author. I used Northwind Db as datacontext. I tried normal execution and compiledquery execution and checked them on LINQ PAD.

First I tried executing the query without using CompileQuery. It took 2.065 seconds.

var oo =   from o in Orders
   where o.OrderDetails.Any (p => p.UnitPrice > 100)
   select o;

oo.Dump ("Order items with unit price more than $100");

var oo1 = from o in Orders
   where o.OrderDetails.Any (p => p.UnitPrice > 10)
   select o;

oo1.Dump ("Order items with unit price more than $10"); 

Secondly, the queries with using CompileQuery. It took 2.100 seconds.

var oo = CompiledQuery.Compile ((TypedDataContext dc, decimal unitPrice) =>    
   from o in Orders
   where o.OrderDetails.Any (p => p.UnitPrice > unitPrice)
   select o
);

oo (this, 100).Dump ("Order items with unit price more than $100");
oo (this, 10).Dump ("Order items with unit price more than $10");

Re-executing them several times showed that the time taken by both of the approaches are almost similar.

Here we see only two query executions for each method. I tried making 10 queries for each of them. But both of them completed around 7 seconds.

Does pre-compiling the queries really improve the performance? Or am I getting it wrong it terms of usage ?

Thank you for your time and consideration.

Edit:
After reading the accepted answer, readers may also want to go through this article which nicely explains how compiled queries improve performance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:40:17+00:00Added an answer on June 1, 2026 at 12:40 pm

    Bear in mind that there are two main pieces of a LINQ query that can be particularly expensive:

    1. Compiling the LINQ expressions into an SQL Statement.
    2. Running the SQL Statement and retrieving the results

    In your case, you have a relatively simple query, and either a very slow database connection, some very large data sets, or tables that are not indexed in an optimal way to run this particular query. Or maybe a combination of all three.

    So compared to the amount of time it is taking to produce the SQL for your query (maybe 10-50 milliseconds), the second step is taking so much time (~1000 ms) that you can hardly notice the difference.

    You would see significant improvements if the following conditions are all true:

    1. your LINQ query is complex,
    2. you have a fast connection to your database,
    3. the SQL query itself runs quickly on that database, and
    4. the result set is small enough that it gets transferred back from the database relatively quickly.

    In practice, I’ve had queries that can take upwards of 500ms to compile, but only a few milliseconds to actually run. These are usually the cases where I focus on precompiling queries.

    One good way to know ahead of time what kind of performance gains you can expect from precompiled queries is to time the second instance of your query using a Stopwatch object, and then run the generated SQL directly using LINQPad’s Analyze SQL feature. If the SQL query returns quickly but the LINQ query takes a long time, that’s a good candidate for precompiling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Referring to article Gotw 54 by HerbSutter, he explains about The Right Way To
By referring Joel's Article Some people are under the misconception that Unicode is simply
Referring to Brian Goetz's article Are all stateful Web applications broken? for IBM developerWorks,
I'm creating an extender control by referring this article , but I'm facing hard
How does tension relate to cubic spline interpolation? I am referring to this article
Edited........ sorry Sir I was referring this piece of code from Stephen Toub's article..
I'm working through Josh Smith's code and article on CommandSink which is a solution
Referring to the article here I created my .htaccess file with the following lines
By referring article, it is using a pair of volatile to prevent out-of-order execution.
I have created a button widget with custom images by referring this article. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.