question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nikschuilingcommented, Jul 17, 2023

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.

0reactions
ismcagdascommented, Jan 4, 2023

@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 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Are Your Databases Slow with Large Tables? An ...
Slow database ? As databases and the tables within them get larger database performance starts to drop – here's why that happens and...
Read more >
Solution for speeding up a slow SELECT DISTINCT query ...
Pretending that I'm 100% certain the DISTINCT portion of the query is the reason it runs slowly, I've omitted the rest of the...
Read more >
Optimizing slow performance of simple SELECT query
Ends up filtering 1.7M rows. This reeks of inaccurate table statistics. And there is a likely explanation, too: When upgrading major versions ...
Read more >
Chapter 4. Query Performance Optimization
Slow Query Basics: Optimize Data Access. The most basic reason a query doesn't perform well is because it's working with too much data....
Read more >
Troubleshoot a query that shows different performance ...
If multiple queries run slower on one server than the other, the most probable cause is the differences in server or data environment....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found