简体   繁体   中英

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.

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.

Sql's groupby and linq's groupby return differently shaped results.

Sql's groupby returns keys and aggregates (no group members)

Linq's groupby returns keys and group members.

If you use those group members, they must be (re-)fetched by the grouping key. This can result in +1 database roundtrip per group .

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 eg 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.

well, i had the same question some time ago. basically: your linq-statement is converted to a sql-statement. however: some groups will get translated, others not - depending on how you write your statement.
so yes - both is possible

example:

var a = (from entity in myTable where entity.Property == 1 select entity).ToList();

versus

var a = (from entity in myTable.ToList() where entity.Property == 1 select entity).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM