Performance question
Hey guys,
I have an web app and I tried to optimize the performance of one report. One stuff that seems unefficient to me is this one:
If I understand it correctly, it'll execute the whole query 4 (5 if I count also counting but guess it's necessary) times so I tried to do optimize it to do everything in one step like this:
but according the Network in Chrome this optimalization is even a bit slower and I cant understand why. Any explanation ? Thanks
18 Replies
5 times, yes.
.Count()
, .ToList()
, .Sum()
x 3
None of them being async
either, so the pinnacle of unoptimised
I'd definitely check what the performance actually is
As in, what takes the most time, how long is the database round trip, how long the endpoint takes to execute, how much of that is network latency, and so onThank you. I tried it repeatedly and it's slower in same condition (network, hw, etc) and it really seems it consistently slower. It's small difference but I'd expect that my optimalization supposed to be faster. There are enother issues with the code and query like you said but cuurently I just want to understand this part
What are you doing with
result
?
ToList will bring the entire query result back to your application. But, for what?
If you need result, why not just sum it in app memory, rather than summing query and avoid three roundtrips to db?I mean, that's what the latter version does
And yet it is, apparently, slower
Ah, no, wait, what is
query
in the latter example?That came to my mind to and I changed it like that "var result = query.ToList();
and it is faster but I understand why so I did not mentioned in 🙂
Because if you're looping over an
IQueryable
, then each loop iteration is a new db callBut that doesn't answer why result is needed in the first example.
not sure I understand. Query is IQueryable and We are returning list from our repositories ... well, we returing something like that:
So, yeah, each loop over iquerable is another db call
Resolve the query first, then loop over that
Basically a mixture of the two versions
Yeah it's IQueryable andyeah maybe that's the reason. But ... ToList() handles it differently ?
.ToList()
resolves the query in a single db call
And returns an in-memory list of itemsok, so now I understand why it's slower. Not it makes sense. Thank you
Then, summing stuff in memory is easy and fast
I expeccted it does something similar like foreach ... I dont know how it's implemented to be honest
I'd use the original code, but do the summing on
result
not on query
i did it before and it's faster, thanks
yeah. exactly.
also don't test it with the debugger enabled
I just wanted to know why the first example wasnt and now I got my answer ... so thanks again.
I did not. I just look to network tab to see how long the request takes
thanks guys