Skip to main content

StatsProvider

Trait StatsProvider 

Source
pub trait StatsProvider: Send + Sync {
    // Required methods
    fn table_stats(&self, table: &str) -> Option<TableStats>;
    fn index_stats(&self, table: &str, column: &str) -> Option<IndexStats>;

    // Provided methods
    fn column_stats(&self, table: &str, column: &str) -> Option<ColumnStats> { ... }
    fn has_index(&self, table: &str, column: &str) -> bool { ... }
    fn distinct_values(&self, table: &str, column: &str) -> Option<u64> { ... }
    fn column_histogram(&self, _table: &str, _column: &str) -> Option<Histogram> { ... }
    fn column_mcv(
        &self,
        _table: &str,
        _column: &str,
    ) -> Option<MostCommonValues> { ... }
}
Expand description

Read-only interface the planner uses to look up storage statistics.

Implementations must be cheap (O(1) or O(log n)) — the planner calls these during plan construction and must not block on I/O. Pre-aggregate expensive data into memory before exposing a provider.

Required Methods§

Source

fn table_stats(&self, table: &str) -> Option<TableStats>

Return row-count / page-count / column metadata for table, or None when stats are not available.

Source

fn index_stats(&self, table: &str, column: &str) -> Option<IndexStats>

Return the IndexStats backing a secondary index on (table, column), if one exists. The planner uses IndexStats::point_selectivity to derive equality selectivity instead of the 0.01 constant.

Provided Methods§

Source

fn column_stats(&self, table: &str, column: &str) -> Option<ColumnStats>

Return per-column statistics (distinct count, null count, min/max) when available. Default implementation derives from StatsProvider::table_stats when present.

Source

fn has_index(&self, table: &str, column: &str) -> bool

Convenience: does a usable index exist on (table, column)?

Source

fn distinct_values(&self, table: &str, column: &str) -> Option<u64>

Convenience: distinct-value count for a column, via column stats or an index on that column, whichever is available.

Source

fn column_histogram(&self, _table: &str, _column: &str) -> Option<Histogram>

Optional equi-depth histogram for the column. Defaults to None, in which case the planner falls back to its uniform 0.3 range heuristic.

Implementations should sample once and cache — this is called during plan construction and must not block on I/O.

Source

fn column_mcv(&self, _table: &str, _column: &str) -> Option<MostCommonValues>

Optional most-common-values list for the column. Defaults to None, in which case the planner falls back to its uniform 0.01 equality heuristic. Use for skewed columns where one or two values dominate the table.

Implementors§