Skip to content

Prefiltering And Postfiltering since v0.4.0

VectorChord provides flexible filtering mechanisms to improve vector search performance and accuracy through prefiltering and postfiltering strategies. These filtering approaches determine when query conditions (like WHERE clauses) are applied during the vector similarity search process.

Unlike postfiltering, which is the default behavior, the prefiltering strategy applies an additional filter before the vector search. This prunes the search space, thereby increasing the speed of the search. However, the additional filter also introduces an extra cost that should be considered.

Only a filter on Index Scan can benefit from prefiltering:

sql
EXPLAIN (COSTS FALSE, TIMING FALSE) 
SELECT val FROM t WHERE tag > 1 ORDER BY val <-> '[0, 0, 0]';
               QUERY PLAN                
-----------------------------------------
 Index Scan using t_val_idx on t
   Order By: (val <-> '[0,0,0]'::vector)
   Filter: (tag > 1)

Configuration

You can control the filtering strategy using the vchordrq.prefilter setting:

sql
-- Enable prefiltering (default: off)
SET vchordrq.prefilter = on;

WARNING

The planner/optimizer greatly affects whether the prefiltering takes effect. Complex queries, such as join queries, are more likely to exhibit unstable behavior. Please evaluate carefully based on experiments.

Performance Trade-offs

Use prefiltering when:

  • Your filtering conditions are highly selective (eliminate many rows)

Use postfiltering when:

  • Your filtering conditions are less selective
  • The filter is a costly operation, such as a LIKE filter with a complicated regular expression.
ExampleAll rowsSelected rowsSelect rate
A low selective filter100090090%
A medium selective filter100030030%
A highly selective filter1000101%

WARNING

If your filter is not a pure function and have some side effects, vchordrq.prefilter could cause a change in behavior.


Based on our experimental results, the QPS speedup at different select rate is as follows:

  • 200% speedup at a select rate of 1%
  • Not significant (5%) speedup at a select rate of 10%
Prefiltering on LAION-5m