Please enable Javascript to correctly display the contents on Dot Net Tricks!

Tips to improve Entity Framework Performance

  Authors : Shailendra Chauhan
Updated On : 24 Sep 2016
Total Views : 74,178   
Support : EF 4.1, 5.0+

LINQ to Entity is a great ORM for querying and managing database. It offers a lot of things, so it is mandatory to know about performance of it. These are right up to a certain point as LINQ comes with its own penalties. There are some tips and tricks that we should keep in mind while desiging and query database using entity framework ORM. Here is a list of some tips that I would like to share with you.

  1. Avoid to put all the DB Objects into One Single Entity Model

    Entity Model specifies a single unit of work, not all our database. If we have many database objects that are not connected to one another or these(log tables, objects used by batch processes,etc.) are not used at all. Hence these objects are consuming space in the memory and cause performance degrades. So try to make separate entity models of related database objects.

  2. Disable change tracking for entity if not needed

    Whenever you retrieve the data only for reading purpose, not for modification then there is no need of object tracking. So disable object tracking by using MergeOption as below:

     NorthwindDataContext context = new NorthwindDataContext() context.tblCities.MergeOption = MergeOption.NoTracking; 

    This option allow us to turn off the object cache and unnecessary identity management of the objects.

  3. Use Pre-Generating Views to reduce response time for first request

    When the object of ObjectContext is created first time in the application, the entity framework creates a set of classes that is required to access the database. This set of classes is called view and if your data model is large then creating the view may delay the web application response to the first request for a page. We can reduce this response time by creating view at compile time by using T4 template or EdmGen.exe command-line tool.

  4. Avoid fetching all the fields if not required

    Avoid fetching not required fields from the database. Suppose I have table of Customer with 20 fields and I am interested only in three fields - CustomerID, Name, Address then fetch only these three fields instead of fetching all the fields of the Customer table.

     //Bad Practice
    var customer =
     (from cust in dataContext.Customers
     select cust).ToList();
     //Good Practice
    var customer =
     (from cust in dataContext.Customers
     select new {
     customer. CustomerID,
     }). ToList (); 
  5. Choose appropriate Collection for data manipulation

    In linq we have Var, IEnumerable, IQueryable, IList type collection for data manipulation. Each collection has its importance and performance impact on the query, so beware of using all these collection for data manipulation. For learning difference among all these collection refer my articles IEnumerable VS IQueryable, IEnumerable VS IList and Var VS IEnumerable.

  6. Use Compiled Query wherever needed

    Make a query to compiled query if it is frequently used to fetch records from the database. This query is slow in first time but after that it boost the performance significantly. We use Compile method of CompiledQuery class for making compiled query.

    Suppose you required to retrieve customers details again and again based on city then make this query to compiled query like as

     // create the entity object
    NorthwindEntities mobjentity = new NorthwindEntities();
     //Simple Query
    IQueryable lstCus = from customer in mobjentity.tblCustomers
    where customer.City == "Delhi"
    select customer;
     //Compiled Query
    Func> compiledQuery
    = CompiledQuery.Compile>(
    (ctx, city) =>from customer in ctx.Customers
    where customer.City == city
    select customer); 

    In above query we are passing the string parameter city for filtering the records. For more about anonymous method.

  7. Retrieve only required number of records

    When we are binding data to grid or doing paging, retrieve only required no of records to improve performance. This can achieved by using Take,While and Skip methods.

     // create the entity object
    NorthwindEntities mobjentity = new NorthwindEntities();
    int pageSize=10,startingPageIndex=2;
    List lstCus = mobjentity.tblCustomers.Take(pageSize)
     .Skip(startingPageIndex * pageSize)
  8. Avoid using Contains

    In LINQ, we use contains method for checking existence. It is converted to "WHERE IN" in SQL which cause performance degrades.

  9. Avoid using Views

    Views degrade the LINQ query performance costly. These are slow in performance and impact the performance greatly. So avoid using views in LINQ to Entities.

  10. Debug and Optimize LINQ Query

    If you want to debug and optimize your query then LINQ Pad is a great tool for this purpose. I am a big fan of LINQ Pad. It is very useful for query construction, debugging and optimization.

     IQueryable lstCus = from customer in mobjentity.tblCustomers
    where customer.City == "Delhi"
    select customer;

    Dump method of LINQ Pad give the result of above query in the result window.


I hope you will enjoy these tips and tricks while programming with LINQ to Entity. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Free Interview Books
PPC Marketing (offline)

Sat, Sun 09:00 AM-10:30 AM IST

Know More
NodeJS Development (online)

MON-FRI 06:00 AM - 07:30 AM IST

Know More
AngularJS Development (online)

MON-FRI 08:00 PM-09:30 PM IST.

Know More
27 OCT
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

24 OCT
MEAN Stack Development (online)

MON-FRI     06:00 AM -07:30 AM IST

22 OCT
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

12 OCT
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

17 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     05:00 PM-06:30 PM IST

NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST


Professional Speaks