Inferensys

Glossary

Filter Selectivity

Filter selectivity is a database metric, expressed as a fraction or percentage, that estimates the proportion of records in a dataset that will satisfy a given filter predicate, used by query optimizers to choose efficient execution plans.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
DATABASE QUERY OPTIMIZATION

What is Filter Selectivity?

Filter selectivity is a critical metric in database query optimization, quantifying the expected proportion of records that will pass a given filter condition.

Filter selectivity is a statistical measure, expressed as a fraction or percentage, that estimates the proportion of records in a dataset that will satisfy a given filter predicate. Database query optimizers rely on this estimate to choose the most efficient execution plan, such as deciding whether to use an index or perform a full table scan. High selectivity indicates a filter that excludes most rows, making an index scan efficient, while low selectivity suggests a broad filter where a sequential scan may be faster.

In vector database infrastructure and hybrid search, understanding filter selectivity is essential for optimizing multi-stage retrieval pipelines. For operations like pre-filtering or post-filtering, the optimizer uses selectivity to decide the order of operations—applying a highly selective metadata filter before an expensive approximate nearest neighbor (ANN) search can drastically reduce latency. Accurate selectivity estimation directly impacts the performance of filtered search and the overall efficiency of query planning.

QUERY OPTIMIZATION

Key Characteristics of Filter Selectivity

Filter selectivity is a critical metric used by database query optimizers to estimate the efficiency of different execution plans. It quantifies the proportion of records expected to pass a given filter predicate.

01

Definition and Formula

Filter selectivity is a statistical measure, expressed as a value between 0.0 and 1.0 (or 0% to 100%), that estimates the fraction of rows in a table or index that will satisfy a specific filter condition. It is calculated as:

Selectivity = (Estimated Number of Matching Rows) / (Total Number of Rows)

  • A selectivity of 0.01 (1%) indicates a highly selective filter, likely returning a small result set.
  • A selectivity of 0.9 (90%) indicates a non-selective filter, returning most records. Optimizers use this value to choose between index scans, full table scans, and join algorithms.
02

Role in Query Planning

The query optimizer uses selectivity estimates to generate the lowest-cost execution plan. Key decisions influenced by selectivity include:

  • Access Path Selection: Choosing between a full vector/index scan versus using a specific metadata index.
  • Join Order: Determining the optimal sequence for joining multiple tables, typically processing the most selective filters first to reduce intermediate result sizes.
  • Operator Choice: Deciding whether to use a nested-loop join (good for highly selective outer tables) or a hash join (better for larger, less filtered datasets). In vector databases, it critically informs the choice between pre-filtering and post-filtering strategies.
03

Factors Influencing Selectivity

Selectivity is not a fixed property; it depends on data distribution and the predicate itself. Key factors are:

  • Data Cardinality: The number of distinct values in a column. High cardinality (e.g., user_id) typically leads to higher selectivity for equality filters.
  • Predicate Type: An equality filter (category = 'sports') is generally more predictable than a range filter (price > 100).
  • Data Skew: Real-world data is often skewed. A filter on country='US' might have low selectivity in a global dataset but high selectivity in a dataset for Japan.
  • Correlated Columns: Selectivity estimates for conjunctive filters (AND) assume independence, but if city and zip_code are correlated, the actual selectivity may be much higher than the product of individual selectivities.
04

Estimation Techniques

Databases use various methods to estimate selectivity without scanning all data:

  • Histograms: The most common technique. The system divides column value ranges into buckets and stores the frequency of values in each, enabling accurate estimates for equality and range predicates.
  • HyperLogLog & Distinct Counts: Used to estimate the cardinality of a column, which is essential for equality filter estimates.
  • Sampling: Running the filter on a small, random sample of pages to extrapolate the overall selectivity.
  • Static Catalog Statistics: Pre-computed statistics like n_distinct, most_common_vals, and correlation stored in the system catalog (e.g., pg_statistics in PostgreSQL).
05

Impact on Vector Search Performance

In hybrid search systems, filter selectivity directly dictates the optimal retrieval strategy:

  • High-Selectivity Filters (e.g., user_id=123): Favor pre-filtering. Applying the strict filter first creates a very small candidate set, making the subsequent Approximate Nearest Neighbor (ANN) search over vectors cheap and accurate.
  • Low-Selectivity Filters (e.g., is_published=true): Favor post-filtering. Performing a broad ANN search first and then applying the weak filter avoids the overhead of integrating the filter into the graph traversal of indexes like HNSW. Poor selectivity estimation can lead the optimizer to choose a post-filtering plan for a highly selective query, causing massive unnecessary vector comparisons.
06

Related Optimization Concepts

Filter selectivity interacts with several core database optimization principles:

  • Filter Pushdown: The optimizer strives to push high-selectivity filters down to the storage layer to minimize data movement. High selectivity makes pushdown highly beneficial.
  • Bitmap Index Scans: For medium-selectivity filters on multiple columns, the database may perform bitmap index scans, combining bitmaps using AND/OR before fetching the rows.
  • Conjunctive vs. Disjunctive Queries: The selectivity of a conjunctive query (AND) is the product of individual selectivities (assuming independence). A disjunctive query (OR) has selectivity calculated as 1 - product(1 - sel_i).
  • Cost-Based Optimization (CBO): Selectivity is a primary input into the cost formulas that compare potential execution plans.
DATABASE PERFORMANCE

How Filter Selectivity Drives Query Optimization

Filter selectivity is a foundational metric in database query optimization, quantifying the expected reduction in dataset size from applying a filter predicate.

Filter selectivity is a measure, expressed as a fraction or percentage, that estimates the proportion of records in a dataset that will satisfy a given filter predicate. This statistical estimate is used by a query optimizer to compare the efficiency of potential execution plans. A highly selective filter (e.g., user_id = 123) passes a tiny fraction of rows, making it a prime candidate for filter pushdown to minimize data processing. Conversely, a low-selectivity filter (e.g., status = 'active') is less effective at reducing the working set.

The optimizer uses selectivity to decide critical execution order, such as whether to pre-filter a dataset with metadata constraints before an expensive vector similarity search. For conjunctive queries (AND), selectivities are multiplied; for disjunctive queries (OR), they are combined. Accurate selectivity estimation, often derived from histograms or bitmap index statistics, is essential for the optimizer to choose a plan that minimizes latency and computational cost, directly impacting the performance of hybrid search systems.

QUERY OPTIMIZATION GUIDE

Filter Selectivity Scenarios & Optimization Decisions

A decision matrix for choosing the optimal filtered search strategy based on filter selectivity and system constraints.

Scenario & MetricPre-Filtering StrategyPost-Filtering StrategyANN-with-Filters Strategy

Primary Use Case

High-precision retrieval with strict constraints

Broad semantic recall with secondary filtering

Balanced performance for moderate constraints

Optimal Filter Selectivity

< 1% (Highly Selective)

30% (Non-Selective)

1% - 30% (Moderately Selective)

Index Architecture Requirement

Separate metadata index (e.g., B-tree, Bitmap)

Single vector index (e.g., HNSW, IVF)

Integrated filtered index (e.g., HNSW with Filters)

Query Latency Profile

Fast filter, fast search on small set

Fast search, fast filter on large set

Moderate, single-pass search

Memory & Compute Overhead

Low for search, high for index maintenance

Low for indexing, high for filtering large result sets

Moderate, integrated into core index

Result Recall Guarantee

100% for filtered set

May discard relevant items failing post-filter

100% for filtered set (algorithm-dependent)

Implementation Complexity

Medium (query planning & join logic)

Low (sequential operations)

High (custom index modification)

System Example

PostgreSQL with pgvector (bitmap scan -> IVF)

Pinecone (vector search -> client-side filter)

Weaviate (HNSW with filtered graph traversal)

FILTER SELECTIVITY

Frequently Asked Questions

Filter selectivity is a critical metric for optimizing search performance in vector databases and hybrid retrieval systems. These questions address its definition, calculation, and practical impact on query execution.

Filter selectivity is a statistical measure, expressed as a fraction or percentage, that estimates the proportion of records in a dataset that will satisfy a given filter predicate (e.g., category = 'news' AND date > '2024-01-01'). It is a foundational input used by database query optimizers to choose the most efficient execution plan by predicting the cardinality, or size, of intermediate result sets. A high-selectivity filter (e.g., 0.01 or 1%) is very restrictive, returning few records, while a low-selectivity filter (e.g., 0.8 or 80%) is broad, returning most records.

In the context of hybrid and filtered search, accurately estimating selectivity is paramount for deciding between strategies like pre-filtering (applying metadata constraints first) and post-filtering (applying them after a vector search). An optimizer might choose pre-filtering for a high-selectivity filter to drastically reduce the candidate set for an expensive approximate nearest neighbor (ANN) search. Selectivity estimates are derived from database statistics, such as histograms on column value distributions.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.