Global filters on a large database table caused SELECT query to run really slow
See original GitHub issueAbp 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.
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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.
@DamienLaw as far as I know, it is not possible to change the parameter value sof query filters on runtime. This is the only workaround we can found. Otherwise, CurrentTenantId will have the same value for all tenants. Maybe you can optimize your tables to make this query faster, is that possible ?