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 160793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:05:53+00:00 2026-05-11T11:05:53+00:00

The executeTime below is 30 seconds the first time, and 25 seconds the next

  • 0

The executeTime below is 30 seconds the first time, and 25 seconds the next time I execute the same set of code. When watching in SQL Profiler, I immediately see a login, then it just sits there for about 30 seconds. Then as soon as the select statement is run, the app finishes the ToList command. When I run the generated query from Management Studio, the database query only takes about 400ms. It returns 14 rows and 350 columns. It looks like time it takes transforming the database results to the entities is so small it is not noticable.

So what is happening in the 30 seconds before the database call is made?

If entity framework is this slow, it is not possible for us to use it. Is there something I am doing wrong or something I can change to speed this up dramatically?

UPDATE: All right, if I use a Compiled query, the first time it take 30 seconds, and the second time it takes 1/4th of a second. Is there anything I can do to speed up the first call?

using (EntitiesContext context = new EntitiesContext())  {      Stopwatch sw = new Stopwatch();      sw.Start();      var groupQuery = (from g in context.Groups.Include('DealContract')                      .Include('DealContract.Contracts')                      .Include('DealContract.Contracts.AdvertiserAccountType1')                      .Include('DealContract.Contracts.ContractItemDetails')                      .Include('DealContract.Contracts.Brands')                      .Include('DealContract.Contracts.Agencies')                      .Include('DealContract.Contracts.AdvertiserAccountType2')                      .Include('DealContract.Contracts.ContractProductLinks.Products')                      .Include('DealContract.Contracts.ContractPersonnelLinks')                      .Include('DealContract.Contracts.ContractSpotOrderTypes')                      .Include('DealContract.Contracts.Advertisers')                  where g.GroupKey == 6                  select g).OfType<Deal>();      sw.Stop();      var queryTime = sw.Elapsed;      sw.Reset();      sw.Start();      var groups = groupQuery.ToList();      sw.Stop();      var executeTime = sw.Elapsed;  }  
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T11:05:54+00:00Added an answer on May 11, 2026 at 11:05 am

    I had this exact same problem, my query was taking 40 seconds.

    I found the problem was with the .Include('table_name') functions. The more of these I had, the worse it was. Instead I changed my code to Lazy Load all the data I needed right after the query, this knocked the total time down to about 1.5 seconds from 40 seconds. As far as I know, this accomplishes the exact same thing.

    So for your code it would be something like this:

    var groupQuery = (from g in context.Groups             where g.GroupKey == 6              select g).OfType<Deal>();   var groups = groupQuery.ToList();  foreach (var g in groups) {     // Assuming Dealcontract is an Object, not a Collection of Objects     g.DealContractReference.Load();     if (g.DealContract != null)     {         foreach (var d in g.DealContract)         {             // If the Reference is to a collection, you can just to a Straight '.Load'             //  if it is an object, you call '.Load' on the refence instead like with 'g.DealContractReference' above             d.Contracts.Load();             foreach (var c in d.Contracts)             {                 c.AdvertiserAccountType1Reference.Load();                 // etc....             }         }     } } 

    Incidentally, if you were to add this line of code above the query in your current code, it would knock the time down to about 4-5 seconds (still too ling in my option) From what I understand, the MergeOption.NoTracking option disables a lot of the tracking overhead for updating and inserting stuff back into the database:

    context.groups.MergeOption = MergeOption.NoTracking; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code which needs to do something like this There is a list
My view has errors. I see these errors only if I open the view
After executing this code: var runtime = IronRuby.Ruby.CreateRuntime(); var engine = IronRuby.Ruby.CreateEngine(); var scrope
I'm writing a DLL to change permissions on a folder and everything underneath the
Hi im trying to write a lockless list i got the adding part working
I am making a Windows service which needs to continuously check for database entries
I have a wordpress site with 5k post and each post has average 25
I'm attempting to run an IronRuby script from C#: var runtime = IronRuby.Ruby.CreateRuntime(); runtime.ExecuteFile(ruby/foo.rb);
I'm trying to add a plugin architecture to my C# app. I've chosen IronScheme

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.