Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If I have a LINQ query like this:

query.OrderBy(e => e.Name).Where(e => e.Name.Contains("Test")).Take(10);

What would be the execution order of ORDERBY, WHERE and TAKE.

  1. Is ORDERBY done only on records that match the search criteria or on the whole table?
  2. Will it search whole table for search criteria and then do TAKE 10 records or it will stop search when it gets 10 records that match criteria?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
263 views
Welcome To Ask or Share your Answers For Others

1 Answer

It occurs in this order -

WHERE predicate is used to filter results,

THEN ORDER BY to sort and

THEN 10 from that, which happens to be TOP 10 syntax.

You could easily see how its executed in SQL Server by running the query with "INCLUDE ACTUAL EXECUTION PLAN" on.

enter image description here

Here is the query i used to demonstarte plus the output. Sorry had to use Ozar's tool as getting the image here on SO might be annoying.

https://www.brentozar.com/pastetheplan/?id=H1v7h-q6D


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...