aspnetboilerplate: Global filters on a large database table caused SELECT query to run really slow

Abp version: 7.4 .NET 6 EF Core 7

Global filters such as Soft Delete, May Have Tenant, Must Have Tenant are causing SELECT query in a large database table (eg: AbpAuditLogs) with more than 5 million records executes very slowly (most of the time, timed out - exceeding 30 seconds).

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/ae2849321ac856369e8a07b0e9e954c507bacc20/src/Abp.EntityFrameworkCore/EntityFrameworkCore/AbpDbContext.cs#L162-L186

In the above piece of codes, !IsSoftDeleteFilterEnabled, !IsMayHaveTenantFilterEnabled and !IsMustHaveTenantFilterEnabled are a part of expressions and are redundant and don’t really contribute to the query. The expressions are then used to generate the SQL statements.

image

The SQL Statement above is being generated and the underlined codes correspond to the 3 boolean variables mentioned above. As you can see, they don’t really do anything. They’re just constant values that are passed as parameters to the SQL query and compared thus increasing the overhead of the execution.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (16 by maintainers)

Most upvoted comments

Thank you for your great effort 🙏 We will test this and include into next version if everything goes well.

Here are some other global filter adjustment code references, it worked well in my project

//1.  Define C# method
public static bool MustHaveTenantPredicate(int? tenantId, int? currentTenantId)
{
    throw new NotSupportedException("This method should be replaced by the database function call.");
}

//2. Mapping C# method
modelBuilder.HasDbFunction(typeof(MyDbContext).GetMethod(nameof(MustHaveTenantPredicate)))
   .HasTranslation(args =>
   {
       if (IsMustHaveTenantFilterEnabled)
       {
           //TenantId == CurrentTenantId
           return new SqlBinaryExpression(
               ExpressionType.Equal,
               args.First(),
               args.Skip(1).First(),
               args.First().Type,
               args.First().TypeMapping);
       }
       else
       {
           //empty where sql
           return new SqlConstantExpression(Expression.Constant(true), new BoolTypeMapping("bool", DbType.Boolean));
       }
   });
   
 //3. Override CreateFilterExpression
 if (typeof(IMustHaveTenant).IsAssignableFrom(typeof(TEntity)))
 {
     Expression<Func<TEntity, bool>> mustHaveTenantFilter = e => MustHaveTenantPredicate(((IMustHaveTenant)e).TenantId, CurrentTenantId);
     expression = expression == null ? mustHaveTenantFilter : CombineExpressions(expression, mustHaveTenantFilter);
 }

Here’s my solution, use efcore’s “User-defined function mapping” (https://learn.microsoft.com/en-us/ef/core/querying/user-defined-function-mapping)

  1. Define a C# function
 public static bool SoftDeletePredicate(bool isDeleted)
 {
     throw new NotSupportedException("This method should be replaced by the database function call.");
 }
  1. C# function definition be associated with user-defined function in the model configuration
modelBuilder.HasDbFunction(typeof(MyDbContext).GetMethod(nameof(SoftDeletePredicate)))
   .HasTranslation(args =>
   {
       if (IsSoftDeleteFilterEnabled)
       {
           //IsDeleted=0
           return new SqlBinaryExpression(
               ExpressionType.Equal,
               args.First(),
               new SqlConstantExpression(Expression.Constant(false), new BoolTypeMapping("bool", DbType.Boolean)),
               args.First().Type,
               args.First().TypeMapping);
       }
       else
       {
           //1=1
           return new SqlBinaryExpression(
               ExpressionType.Equal,
               new SqlConstantExpression(Expression.Constant(true), new BoolTypeMapping("bool", DbType.Boolean)),
               new SqlConstantExpression(Expression.Constant(true), new BoolTypeMapping("bool", DbType.Boolean)),
               args.First().Type,
               args.First().TypeMapping);
       }
   });
  1. Override CreateFilterExpression
 if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
 {
     Expression<Func<TEntity, bool>> softDeleteFilter = e => SoftDeletePredicate(((ISoftDelete)e).IsDeleted);
     expression = expression == null ? softDeleteFilter : CombineExpressions(expression, softDeleteFilter);
 }

Please give up this solution, there are still defects, I found that if the same query is executed, the query condition is unchanged, EF will cache the generated expression, as long as the query condition is changed, it will trigger the re-generation of the expression, we need to think about other ways 😦

We’re also facing this issue since we upgraded aspnetboilerplate and moved from EF to EF Core. Conditional WHERE clauses such as “((@__ef_filter__p_0 = CAST(1 AS bit)) OR ([a].[IsDeleted] = CAST(0 AS bit))) AND ((@__ef_filter__p_1 = CAST(1 AS bit)) OR ([a].[TenantId] = @__ef_filter__CurrentTenantId_2)))” are causing high CPU usage on our SQL servers for even simple queries and small tables (say 10k records max).

We ended up using IgnoreQueryFilters() in several queries and added explicit filters on TenantId and IsDeleted where required. This resolved the issue, but we’d love to see a better solution.