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).
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.

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