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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:56:27+00:00 2026-05-24T13:56:27+00:00

I’m trying to improve perf in an app that uses Linq2Sql heavily. I have

  • 0

I’m trying to improve perf in an app that uses Linq2Sql heavily. I have found a particular method that is really slow. It is a nasty, nested,.Sum() statement. When executed it is taking just over 30 seconds to return data. The total rows of data are maybe 3000. If I take the sql that LinqPad generates and run it, I get data back in less than a second.

I’m at a loss as to what the framework will return here. It should be filtered down by the where clause, right? I also imagine it is going to work from the inside out. starting with the where, then sum all the Scores, then sum each in T2, then sum each in T1.

ParentTable.Table1.Sum
(
 t1=>
  t1.Table2.Sum
  (
   t2=>
     t2.Table3.Where(t3=>t3.Table4.Id==275).Sum(t3=>t3.Score)
  )
)

To complicate things even further, LinqPad can execute the same statement in under half a second.

I guess my question is, why is there such a difference in Linq2Sql and TSql speeds? Is Linq bringing back all rows and filtering on the app box?

Now, the webapp reuses the same DataContext for the lifetime of the users session. I was always under the impression you should dispose of it after each operation. Could this be the issue?

Let me add that when I profile SQL (when executed from the app) I see nothing to worry about. Reads are <15, CPU is <5, Writes are nothing, and duration is at most 20. So I am pretty sure it isn’t the execution of the statements, but some processing LINQ2Sql is doing.

  • 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-05-24T13:56:28+00:00Added an answer on May 24, 2026 at 1:56 pm

    I think I figured out what is going on.

    The data relations are not as simple as LINQ makes it out to be.
    Table1 has a reference to Table2
    Table2 does reference Table3
    However Table4 isn’t tied directly to T3, there are 2 others in the way.

    That shouldn’t affect anything, but shimmed in a partial class of the Table1 object is a custom global “caching” dictionary. Any time you request ANYTHING from Table1 it makes sure every record from that table is loaded into memory. The same pattern exists for all the objects.

    So basically, by doing that quick and simple sum, it is loading EVERY record in all 6 tables (t1,t2,t3,t3b,t3c,t4) into memory and THEN doing the sum.

    On subsequent requests, it would look at every item in the cache to see if it was stale. This also explains the other problem of data inconsistency.

    Wow…just wow!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.