How to use STRAIGHT_JOIN with LINQ and Entity Framework - MySQL?

150 Views Asked by At

I am using Pomelo.EntityFrameworkCore.MySql provider for MySQL and Entity Framework.

I have a complex dynamically generated query using LINQ and EF:

// Shortened for brevity
IQueryable<Account> accountQuery = db.Accounts.AsNoTracking()

foreach (var filter in filters)
{
     IQueryable<Account> subQuery = CreateSubQuery(filter);
     accountQuery = accountQuery.Where(aq => !subQuery.Select(a => a.Id).Contains(aq.Id));
}

accountQuery.Count(); // The slow part

Essentially the master query excludes all the account IDs in any of the subquerys.

If I run the raw query in SQL, it takes 2 minutes. If the only thing I change is add STRAIGHT_JOIN to the raw query, it only takes a couple of seconds. I guess it tells the optimizer to run the queries in order instead of trying to optimize.

So how can I add the equivalent of STRAIGHT_JOIN to the LINQ query...?

0

There are 0 best solutions below