Expand description
Cardinality Feedback for Query Optimization
This module implements a learning system that improves cardinality estimates by tracking the difference between estimated and actual row counts during query execution. When similar predicates are seen again, the correction factors are applied to produce more accurate estimates.
§How It Works
- During EXPLAIN ANALYZE, we record estimated vs actual row counts
- A fingerprint is computed for each predicate pattern (structure, not values)
- Correction factors are stored:
correction = actual / estimated - Future queries with similar patterns use the correction factor
§Example
-- First query: estimated 100, actual 1000 → correction = 10.0
EXPLAIN ANALYZE SELECT * FROM users WHERE status = 'active';
-- Later query: base estimate 50, corrected estimate 500
SELECT * FROM users WHERE status = 'pending';Structs§
- Cardinality
Feedback - Cardinality feedback entry for a predicate pattern
- Feedback
Cache - Cache for cardinality feedback entries
Constants§
- DEFAULT_
DECAY_ FACTOR - Default decay factor for exponential moving average
- MAX_
CORRECTION_ FACTOR - Maximum correction factor to prevent extreme adjustments
- MIN_
CORRECTION_ FACTOR - Minimum correction factor
- MIN_
SAMPLE_ COUNT - Minimum sample count before applying feedback
Functions§
- extract_
column_ from_ predicate - Extract the column name from a simple predicate (col = value)
- fingerprint_
predicate - Compute a fingerprint for a predicate expression
- global_
feedback_ cache - Get the global feedback cache