I have a method on my generated partial class like this:
var pChildren = this.Children
.Skip(skipRelated)
.Take(takeRelated)
.ToList();
When I look at my SQL Server, I can see the generated code is doing a SELECT *.* FROM Children
This code is taken directly from my class, I have verified that the order of my Skip/Take is BEFORE my .ToList.
If I remove the .ToList, that line is fast (and no SQL is sent to my DB), but the moment I try to foreach
over the results, I get the same SQL sent to my DB: SELECT *.* FROM Children
.
Is there something special I need to do when using .Skip and .Take on the navigation properties of my entities?
update
I'll try to get the actual SQL generated, I'm not currently setup for that. I found the first one because it shows up in SSMS's "recenty expensive queries" list.
Running this:
var pChildren = this.Children
//.Skip(skipRelated)
//.Take(takeRelated)
.ToList();
returns ~4,000,000 rows and takes ~25 seconds.
Running this:
var pChildren = this.Children
//.Skip(skipRelated)
.Take(takeRelated)
.ToList();
returns ~4,000,000 rows and takes ~25 seconds.
As I said, I'll grab the SQL generated for these and pose them up as well.
SELECT TOP 10 FROM Children WHERE ParentID = @idOfParentEntity
(I forget how EF handles .Skip, but I've read it is supposed to. If I execute this query against my context directly it appears to limit the data returned to a specific "page") – Hanni.Skip()
precludes the use ofTOP
. Unless you expected it to sumskipRelated
andtakeRelated
and use that as the parameter toTOP
... which could be an optimization, but it may also have no impact on performance. – FlossietakeRelated
row(s) anyway. The real killer here is the lack of a mechanism to skip rows (Skip()
), not the mechanism to limit the result set (Take()
). – FlossieROW_NUMBER
in SQL Server to return rows between10-20
for example. – Surfactant((ObjectQuery<Child>)this.Children.Skip(skipRelated).Take(takeRelated)).ToTraceString()
– JanniejanosUnable to cast object of type '<TakeIterator>d__3a
1' to System.Data.Objects.ObjectQuery1
Once I nail that down, I'll post the results. – Hanni.CreateSourceQuery()
the correct SQL is generated. – Hanni