Why does adding an unnecessary ToList() drastically speed this LINQ query up?

JoeCool

Why does forcing materialization using ToList() make my query orders of magnitude faster when, if anything, it should do the exact opposite?

1) Calling First() immediately

    // "Context" is an Entity Framework DB-first model

    var query = from x in Context.Users
                where x.Username.ToLower().Equals(User.Identity.Name.ToLower())
                select x;

    var User = query.First();

    //  ** The above takes 30+ seconds to run **

2) Calling First() after calling ToList():

    var query = from x in Context.Users
                where x.Username.ToLower().Equals(User.Identity.Name.ToLower())
                select x;

    var User = query.ToList().First();     // Added ToList() before First()

    // ** Now it takes < 1 second to run! **

Update and Resolution

After getting the generated SQL, the only difference is, as expected, the addition of TOP (1) in the first query. As Andyz Smith says in his answer below, the root cause is that the SQL Server optimizer, in this particular case, chooses a worse execution plan when TOP (1) is added. Thus the problem has nothing to do with LINQ (which did the right thing by adding TOP (1)) and everything to do with the idiosyncrasies of SQL Server.

Andyz Smith

So, the optimizer chooses a bad way to run the query.

Since you can't add optimizer hints to the SQL to force the optimizer to choose a better plan I see two options.

  1. Add a covering index/indexed view on all the columns that are retrieved/included in the select Pretty ludicrous, but I think it will work, because that index will make it easy peasy for the optimizer to choose a better plan.

  2. Always prematurely materialize queries that include First or Last or Take.  Dangerous because as the data gets larger the break even point between pulling all the data locally and doing the First()  and doing the query with Top on the server is going to change.

http://geekswithblogs.net/Martinez/archive/2013/01/30/why-sql-top-may-slow-down-your-query-and-how.aspx

https://groups.google.com/forum/m/#!topic/microsoft.public.sqlserver.server/L2USxkyV1uw

http://connect.microsoft.com/SQLServer/feedback/details/781990/top-1-is-not-considered-as-a-factor-for-query-optimization

TOP slows down query

Why does TOP or SET ROWCOUNT make my query so slow?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LinQ in C#, why does query sentence must convert to tolist()?

From Dev

Does adding between speed up my SQL query?

From Dev

Speed up LINQ Object Query

From Dev

SQL - Why does adding CASE to an ORDER BY clause drastically cut performance? And how can I avoid this?

From Dev

How does one use .ToList() Inside of a Linq Query?

From Dev

Why is Entity Framework .ToList() with .Include very slow and how can I speed it up?

From Dev

Why is Entity Framework .ToList() with .Include very slow and how can I speed it up?

From Dev

How can I speed up this Linq to SQL query?

From Dev

Why is LINQ ToList not used here?

From Dev

Why is LINQ ToList not used here?

From Dev

Does ranging the SQL query speed up the query time?

From Dev

SQL - speed up query

From Dev

Is there a way to speed up this Query?

From Dev

Why does changing int to long speed up the execution?

From Dev

Why does multithreading do not speed up parsing HTML with lxml?

From Dev

Why does emptying disk space speed up computers?

From Dev

Fail to convert LINQ query result to ToList

From Dev

Exception on Inner LINQ query when calling ToList()

From Dev

Linq Query toList() is empty but foreach works

From Dev

Fail to convert LINQ query result to ToList

From Dev

Linq Query toList() is empty but foreach works

From Dev

Speed up the linq group by statement

From Dev

Why does vblank_mode improve framerate drastically in benchmarks?

From Dev

Why does vblank_mode improve framerate drastically in benchmarks?

From Dev

Why does this LinQ query not like chars?

From Dev

Why does my linq to sql query fail?

From Dev

Why does the LINQ query return no element?

From Dev

Does changing a column length from varchar 18 to varchar 8 improve search speed drastically?

From Dev

Does changing a column length from varchar 18 to varchar 8 improve search speed drastically?

Related Related

  1. 1

    LinQ in C#, why does query sentence must convert to tolist()?

  2. 2

    Does adding between speed up my SQL query?

  3. 3

    Speed up LINQ Object Query

  4. 4

    SQL - Why does adding CASE to an ORDER BY clause drastically cut performance? And how can I avoid this?

  5. 5

    How does one use .ToList() Inside of a Linq Query?

  6. 6

    Why is Entity Framework .ToList() with .Include very slow and how can I speed it up?

  7. 7

    Why is Entity Framework .ToList() with .Include very slow and how can I speed it up?

  8. 8

    How can I speed up this Linq to SQL query?

  9. 9

    Why is LINQ ToList not used here?

  10. 10

    Why is LINQ ToList not used here?

  11. 11

    Does ranging the SQL query speed up the query time?

  12. 12

    SQL - speed up query

  13. 13

    Is there a way to speed up this Query?

  14. 14

    Why does changing int to long speed up the execution?

  15. 15

    Why does multithreading do not speed up parsing HTML with lxml?

  16. 16

    Why does emptying disk space speed up computers?

  17. 17

    Fail to convert LINQ query result to ToList

  18. 18

    Exception on Inner LINQ query when calling ToList()

  19. 19

    Linq Query toList() is empty but foreach works

  20. 20

    Fail to convert LINQ query result to ToList

  21. 21

    Linq Query toList() is empty but foreach works

  22. 22

    Speed up the linq group by statement

  23. 23

    Why does vblank_mode improve framerate drastically in benchmarks?

  24. 24

    Why does vblank_mode improve framerate drastically in benchmarks?

  25. 25

    Why does this LinQ query not like chars?

  26. 26

    Why does my linq to sql query fail?

  27. 27

    Why does the LINQ query return no element?

  28. 28

    Does changing a column length from varchar 18 to varchar 8 improve search speed drastically?

  29. 29

    Does changing a column length from varchar 18 to varchar 8 improve search speed drastically?

HotTag

Archive