SQL Server Filtered Indexes: Faster Queries with Lower Maintenance Cost

SQL Server Filtered Indexes: Faster Queries with Lower Maintenance Cost

Adding a full-table nonclustered index is not always the right answer in SQL Server. Many workloads repeatedly read only a small, clearly defined subset of a table: pending queue items, active records, non-NULL values, or rows belonging to a particular category. In these cases, a filtered index can reduce storage, maintenance, and query costs by indexing only the required rows.

A filtered index is a nonclustered index created over rows that satisfy a filter predicate. When the predicate matches the access pattern of the application, SQL Server can search a smaller structure and use statistics that describe the filtered subset rather than the entire table. The result can be a better plan with fewer logical reads, but only if the filter and query are designed together.

When should you consider a filtered index?

The first question should not be “Is the table large?” It should be “Do important queries target a small and well-defined subset of this table?” A filtered index is a strong candidate when the subset is relatively small, the predicate is stable, and the same access pattern appears frequently.

  • Pending work: A queue table may contain millions of completed items while workers repeatedly search for rows where ProcessedAt IS NULL.
  • Active rows: Historical data may dominate a table, while application requests usually need only Status = 'Active'.
  • Non-NULL values: If most rows contain NULL in a column and queries need only populated values, indexing the populated subset can be more efficient.
  • Specific categories: A heterogeneous table may contain a well-defined product, tenant, or transaction segment that deserves a focused access path.

The benefit is not limited to a smaller index. SQL Server also creates filtered statistics for a filtered index. These statistics describe the indexed subset and may provide better cardinality information than statistics covering all rows in the table.

A practical design example

Assume an application frequently retrieves unprocessed work items from a queue:

CREATE NONCLUSTERED INDEX IX_WorkItems_Pending
ON dbo.WorkItems (QueueName, CreatedAt)
INCLUDE (WorkItemId, PayloadType)
WHERE ProcessedAt IS NULL;

The index contains only rows whose ProcessedAt value is NULL. QueueName supports equality filtering, while CreatedAt can support ordering or range predicates. A matching query might look like this:

SELECT TOP (100)
       WorkItemId, PayloadType, CreatedAt
FROM dbo.WorkItems
WHERE ProcessedAt IS NULL
  AND QueueName = @QueueName
ORDER BY CreatedAt;

The query explicitly includes the filtered condition, and the index contains the columns needed for the result. This can reduce both the number of pages read and the number of lookups back to the base table.

Match the filter to the query

A query can use a filtered index only when its result set is compatible with the rows included by the index. In practical terms, the query predicate must identify rows within the subset defined by the index filter. An index filtered with EndDate IS NOT NULL, for example, is a natural candidate for queries that explicitly require a non-NULL end date.

A filter that is too broad makes the index unnecessarily large. A filter that is too narrow may help only a few special cases. Before creating the index, inspect real production queries, measure the percentage of matching rows, and consider how that percentage changes over time.

Should the filter column be a key column?

The column used in the filter does not always have to appear in the key or included columns. If the query predicate is equivalent to the filter expression and the column is not returned, SQL Server may still use the filtered index. However, the design may need to include the column when the query applies a different comparison or returns that column in the result.

For example, an index filtered with ProcessedAt IS NULL is not automatically ideal for a query that uses a different predicate such as ProcessedAt < @Cutoff. The filter, key columns, and query predicate must be evaluated as a complete design rather than according to a blanket rule.

Syntax and design limitations

Filtered indexes are nonclustered indexes defined on a single table. Their filter predicates use relatively simple comparison logic. They cannot express arbitrary business rules or conditions that reference multiple tables.

  • A filtered index cannot be created on a view.
  • A computed column cannot be referenced in the filter definition.
  • Some data types, including CLR, spatial, and hierarchyid types, are not valid for the filter expression.
  • For NULL checks, use IS NULL or IS NOT NULL rather than comparison operators.
  • Be careful with implicit or explicit conversions in the filter predicate; an unsuitable conversion can cause index creation errors.
  • Filtered indexes do not support complex expressions such as arbitrary LIKE logic in the filter definition.

A valid range filter might look like this:

WHERE StartDate >= '20250101'
  AND EndDate < '20260101'

If the required rule depends on multiple tables, dynamic calculations, or complex transformations, consider a different design such as an indexed view, a separate work table, or a persisted data attribute that can be indexed appropriately.

Required SET options

Filtered indexes depend on specific session settings when they are created, maintained, and considered by the optimizer. Important settings include ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, and QUOTED_IDENTIFIER being ON, while NUMERIC_ROUNDABORT should be OFF.

This matters when different clients use different connection settings. A query might use the filtered index in SQL Server Management Studio but receive a different plan through an application driver. Test using the same driver, connection options, and parameterization behavior used by the production application.

How to verify that the index helps

  1. Capture the query’s actual execution plan and logical reads before creating the index.
  2. Create the filtered index in a representative environment.
  3. Run the same query with comparable parameters and workload conditions.
  4. Inspect Index Seek, Index Scan, Key Lookup, and residual predicate operators.
  5. Compare estimated and actual row counts.
  6. Measure CPU time, elapsed time, and logical reads rather than relying only on whether the index appears in the plan.
SET STATISTICS IO, TIME ON;

SELECT TOP (100)
       WorkItemId, PayloadType, CreatedAt
FROM dbo.WorkItems
WHERE ProcessedAt IS NULL
  AND QueueName = @QueueName
ORDER BY CreatedAt;

The presence of a filtered index in an execution plan is not, by itself, proof of improvement. The goal is lower I/O, lower CPU, and predictable latency. Conversely, if the optimizer does not choose the index, that does not automatically indicate a problem. The filtered subset may have grown, the query may return a large portion of it, or a scan may simply be cheaper for that execution.

Maintenance and monitoring

Because a filtered index contains fewer rows, its maintenance cost is often lower than that of an equivalent full-table index. It is not free, however. When a row enters or leaves the filtered subset, SQL Server must update the index. A frequently changing status column can therefore create substantial write activity even when the filtered index is small.

Monitor the following over time:

  • Index size, page count, and row count,
  • Read and update usage,
  • Logical reads and latency for the target queries,
  • Estimated versus actual row counts,
  • Statistics freshness and data modification volume,
  • Overlap with redundant, unused, or broader indexes.

Do not rebuild or update statistics on a fixed schedule without measuring the workload. A targeted maintenance policy should consider how quickly the filtered rows change and whether stale statistics are associated with plan quality or latency problems.

Final checklist

  • Identify a small, stable, and frequently queried subset of rows.
  • Confirm that the query predicate is compatible with the filter predicate.
  • Choose key columns based on equality predicates, range predicates, joins, and ordering.
  • Use included columns only when they reduce lookups for important queries.
  • Check filtered index syntax and supported data types.
  • Validate required SET options across application connections.
  • Compare logical reads, CPU, elapsed time, and row estimates before and after deployment.
  • Review usage and maintenance cost after the workload changes.

Filtered indexes work best when they are treated as deliberate access paths rather than automatic replacements for regular indexes. Start with measured query behavior, design the subset carefully, and verify the result with actual workload evidence. Used this way, a filtered index can improve performance while avoiding the storage and write overhead of indexing every row.

Sources

//

0 Comments

Write Comment

E-Mail address will not be published