Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework SQL Query Execution

Using the Entity Framework, when one executes a query on lets say 2000 records requiring a groupby and some other calculations, does the query get executed on the server and only the results sent over to the client or is it all sent over to the client and then executed?

This using SQL Server.

I'm looking into this, as I'm going to be starting a project where there will be loads of queries required on a huge database and want to know if this will produce a significant load on the network, if using the Entity Framework.

like image 998
Tony The Lion Avatar asked Dec 23 '22 06:12

Tony The Lion


2 Answers

I would think all database querying is done on the server side (where the database is!) and the results are passed over. However, in Linq you have what's known as Delayed Execution (lazily loaded) so your information isn't actually retrieved until you try to access it e.g. calling ToList() or accessing a property (related table).

You have the option to use the LoadWith to do eager loading if you require it.

So in terms of performance if you only really want to make 1 trip to the Database for your query (which has related tables) I would advise using the LoadWith options. However, it does really depend on the particular situation.

like image 82
James Avatar answered Dec 31 '22 20:12

James


It's always executed on SQL Server. This also means sometimes you have to change this:

from q in ctx.Bar
where q.Id == new Guid(someString)
select q

to

Guid g = new Guid(someString);
from q in ctx.Bar
where q.Id == g
select q

This is because the constructor call cannot be translated to SQL.

like image 43
Sander Rijken Avatar answered Dec 31 '22 20:12

Sander Rijken