Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

461
Views
Is there a way to optimize a query with Includes() in Entity Framework?

I am developing a .NET 5.0 API. I use EF to connect to a MySQL database and it works great. Though EF is very optimized I have one query that takes over a minute to complete :(

This endpoint combines user data from a lot of tables together in one query. To do this I use the Linq .Include(x => x.UserId). I have noticed that .Include() is very, very slow when you need to do it a lot of times like so:

var user = DatabaseContext.Users
    .Include(user => user.Item1)
    .ThenInclude(x => x.Item1)
    .Include(user => user.Item2)
    .Include(user => user.Item3)
    .Include(user => user.Item4)
    .Include(user => user.Item5)
    .ThenInclude(x => x.Item1)
    .SingleOrDefaultAsync(x => x.Id == userId)
);

In my case this leads to the query querying over 200.000 rows of data from the database because EF selects everything from a table and only in the end adds a WHERE clause to filter the data. Example below:

SELECT t.item,
       t.item,
       t.item,
       u.item,
       u.item,
       ..etc
FROM (SELECT a.item,
             a.item
      FROM table as a
      LEFT JOIN table2 as c
             ON a.foreignid = c.id
      WHERE a.id == id
      LIMIT 2) as t
      LEFT JOIN table as u
             ON t.id = u.id
      LEFT JOIN (... another select)
ORDER BY t.id

I could use lazy loading which leads to significantly lower loading times but I have limited CPU power on the server so it would be problematic to run about 12 queries everytime I need this data. Especially if a lot of clients will use this endpoint at the same time (possibly hundreds at one time), so my preference is to do it in one database query.

So my question is: Is there any way to optimize the way EF handles the .Include() statement or is there another way to gain the expected result in a more efficient way?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!