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. In some cases, 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:
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:
-- 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.
Example | All rows | Selected rows | Select rate |
---|---|---|---|
A low selective filter | 1000 | 900 | 90% |
A medium selective filter | 1000 | 300 | 30% |
A highly selective filter | 1000 | 10 | 1% |
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%
