Skip to content

Data filters cause database index scan instead of index seek #6680

@dnimekaf

Description

@dnimekaf

ABP Framework version: 4.11.0
User Interface: Angular
Steps to reproduce: Run IRepository <TEntity:IMustHaveTenant >.GetAllList() method

Data filters use sql data parameters to show if the filter is enabled or not. These parameters sometimes cause performance issues on MSSQL Server, forcing it to use index scan insted of index seek. This is a huge problem for the tables with large number of records.

For example, here is a sample query with enabled data filter and its execution plan (the query is generated automatically):

declare @__ef_filter__IsMustHaveTenantFilterEnabled_0 bit = 1, @__ef_filter__CurrentTenantId_1 int = 1
SELECT top 100 * FROM [Prospects] AS [e] WHERE (@__ef_filter__IsMustHaveTenantFilterEnabled_0 = 0) OR ([e].[TenantId] = @__ef_filter__CurrentTenantId_1)

full-scan
As you can see, it performs index scan for a table. For our database this query takes 1.5 minutes to complete.

And this is the same query except removing of the filter parameter (which is redundant here anyway)
declare @__ef_filter__CurrentTenantId_1 int = 1
SELECT top 100 * FROM [Prospects] AS [e] WHERE ([e].[TenantId] = @__ef_filter__CurrentTenantId_1)
index-seek
And now it uses index seek. This query completes in < 1s on the same database.

The main issue for the MS SQL server is this one: https://stackoverflow.com/questions/27564852/why-is-sql-server-using-index-scan-instead-of-index-seek-when-where-clause-conta

There is no way to prevent data filter to modify the SQL query. Disabling the filter just sets its parameter value to 0.

Suggestion: to avoid using bit parameters in data filters. Such filter should be applied or not before goes down to SQL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions