SQL query with ROW_NUMBER is ineffective in LINQ
I have this query in SQL:
SELECT * FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY p.category ORDER BY (
SELECT AVG(CAST(r.users AS NUMERIC)) FROM description r WHERE r.company = p.id
) DESC) AS rownb, p.* FROM company p
) rs WHERE rownb <= 2
The farthest I came up with converting this query to LINQ:
Companies
.SelectMany(r =>
Descriptions
.GroupBy(t => t.Company)
.Select(t => new {
Average = t.Average(a => (double)a.Users),
Company = t.Key })
.OrderByDescending(t => t.Average)
, (p, r) => new { Companies = p, Descriptions = r })
.Where(t => t.Companies.Id == t.Descriptions.Company)
.GroupBy(t => t.Companies.Category)
.Select(t => t.Take(2))
.SelectMany(t => t)
.Select(t => new { t.Companies.Name, t.Descriptions.Average, t.Companies.Category})
.OrderBy(t => t.Category)
But the problem is performance. Although the cost of the SQL query is 28% (relative to the package), the LINQ query is 72%.
I've already replaced Join in SelectMany in LINQ, which reduced the cost by 20%. But now I don't know how to optimize this query.
Also, my understanding is that LINQ is missing ROW_NUMBER.
I am using LINQPad to validate the resulting SQL query.
Question: Is ROW_NUMBER responsible for this performance difference? Is it possible to optimize a LINQ query?
+3
source to share