Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I call Count method in Entity Framework, does it process all the columns or just one or what?

Im looking for otimization.

When I call Count method in Entity Framework, does it process all the columns or only one or what?

If you also have any official site talking about this, I would appreciate.

Thank you.

like image 736
alansiqueira27 Avatar asked Sep 28 '11 22:09

alansiqueira27


People also ask

How do you count related entities without loading them?

You can also write: int count = context. Blogs. Single(blog=> blog.Id = yourCriteriaId).

How do you call a function in Entity Framework?

Step 1: Create an entity class which inherits “DbContext” class. Step 2: The following is the structure of the database with table and stored procedure. Step 3: Create a class to store the returned tabular value. Step 4: Create an object for the entity above and method to call a function.


1 Answers

I did some tests a while ago and found out that EF does a count on the server, it sends a query with a SELECT COUNT so it does not load all records for sure.

about the columns, if you are referring to the difference between COUNT(*) or COUNT(Id) or COUNT(1) I have read somewhere a while ago that for SQL Server there is no difference, the COUNT(*) is optimized as COUNT(1) anyway.

you could read many articles online or question here on SO... not excatly 100% what you asked but similar topics on performances of EF and ORM...

How to COUNT rows within EntityFramework without loading contents?

http://ayende.com/blog/4387/what-happens-behind-the-scenes-nhibernate-linq-to-sql-entity-framework-scenario-analysis

How to optimize Entity Framework Queries

like image 152
Davide Piras Avatar answered Oct 04 '22 22:10

Davide Piras