I have a table of 200,000 record where I am getting only the top 10 using .Take()
but it is taking about 10 seconds to get the data.
My question is: does the .Take()
method get all the data from the database and filter the top 10 on the client side?
Here is my code:
mylist = (from mytable in db.spdata().OrderByDescending(f => f.Weight)
group feed by mytable.id into g
select g.FirstOrDefault()).Take(10).ToList();
spdata()
is a function Import from stored procedure.
Thanks
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.
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.
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. In all, there are four steps which need to be performed right from the time LINQ queries are built till they are fired.
So LINQ uses the query plan from the static class object rather than building and preparing the query plan from scratch. 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.
The stored procedure probably returns a lot of data to the client which is very slow. You cannot remote a query to an sproc. That would be possible using a view or a table-valued function.
There's no way to use an sproc in a query. You can only execute it by itself.
Your intention probably was to execute the Take(10)
on the server. For that to work you need to switch to an inline query, a view or a TVF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With