Statistics and costs
stats
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
VACUUM (PostgreSQL’s options and tables accepted; the whole database
either way; not in a transaction block, 25001) runs the KV’s maintenance
(Kv::vacuum: versions no snapshot sees removed, committed intents made
versions, §5.2). With ANALYZE — VACUUM ANALYZE t, VACUUM (ANALYZE) t, u — it then analyzes the tables named, or every table when none is
named (VACUUM (ANALYZE false) does not); after a bulk load it is what
PostgreSQL’s documentation advises there too: until a vacuum (the
server’s runs when the database is idle) a load’s spilled writes are
read through their transaction’s status.
ROTATE KEY data (a superuser’s; not in a transaction block, 25001)
gives the database a new data key with it running (§9, A new data
key): the key custody’s new key at once, what the old one sealed
sealed again by the maintenance thread. A second one while the first
runs is 55006; another purpose (secrets, subjects, backup) is
0A000, naming nucleus keys <dir> rotate <purpose>; a database in the
clear is 0A000 too.
ANALYZE [table] stores rows, and per column NULLs, distinct values and a
16-bucket equi-depth histogram — from every row of a table up to 30 000
rows, else from a uniform sample of 30 000 (reservoir sampling by Li’s
Algorithm L — the gap to the next row taken drawn at once, about
k·(1 + ln(N/k)) draws for N rows rather than one each, from SplitMix64
seeded once from Kv::random_bytes: lineitem’s ANALYZE 11.5 → 5.5 s;
NULLs scaled, distinct values by PostgreSQL’s Duj1 estimator); the row
count is exact. The storage samples (TableStorage::sample, offered
rows through stats::Sample, which builds a row only when it takes it):
a row table reads every entry but decodes only the rows taken, a column
table decodes a segment’s columns only when the sample takes one of its
rows (§M14.20 W3b: lineitem 5.5 → 2.7 s in a row table, the column
load’s ANALYZE 5.7 → 2.9 s). On the statement’s workers: a row
table’s range is cut at its split points
(TableStorage::samples_in_parts) and each part is sampled on a worker
over the transaction’s shared reads. The parts’ samples are merged in
one pass (sample::merge_uniform_many): the count each part gives is a
multivariate hypergeometric draw over their row counts, then that many
are taken from each part’s shuffled sample, so the merged sample is
uniform still and the count exact. The rows not taken are dropped on a
thread of their own. The columns’ statistics are worked out a run of
columns a worker (TPC-H SF 1’s ANALYZE 1.70 → 0.76 s, issue #4). Selectivities: = 1/ndv, ranges from the
histogram (a column’s lower and upper bound in one AND together: the
histogram between them, not each side’s product), IS NULL the null
fraction, IN n/ndv, combined by AND/OR/NOT. What the statistics cannot
estimate — LIKE, a distance below a bound, id % 7 = 0, any function
of a column — is counted on a sample ANALYZE keeps (§M14.12 V3; Oracle’s
dynamic sampling, stored rather than read at planning): up to 1,000 of
its sampled rows, at most 1 MiB, under 00 04 <table id> 01, read once
per planning and evaluated by the predicate itself (matches / rows, at
least half a row); a predicate with a subquery or a volatile call, one
that fails on a sampled row, or a table whose columns changed since
falls back to the defaults (LIKE 0.1, NOT LIKE 0.9, other
comparisons 0.33, the rest 0.5). A condition an index method serves is
asked of the method first (index_method::selectivity, §8.12): the text
index tells @@‘s share from its words’ document counts (§8.19), where
the sample would analyze each kept row’s text at every planning. For analyzed tables the access path is the cheapest of a
full scan and every index (a secondary index pays a row fetch per row),
EXPLAIN shows rows=, and chains of up to 8 inner joins are reordered by
dynamic programming (no cross products unless nothing connects; the
smaller input becomes the hash build side; a projection restores the
column order). A join’s equalities multiply their selectivities, except
several between the same two inputs whose columns on one side cover a key
of its table (the primary key, a unique index): those select 1 / its rows
(each row of the other side meets at most one), where the product would
take a join on a two-column key for nearly empty.
Columns are also counted together (§M14.20 V12), per group: the columns
of the primary key and of each index with two or more (an index’s first
8), and each CREATE STATISTICS. For every set of two or more of a
group’s columns ANALYZE stores its distinct combinations (NULL a value;
Duj1 on a sample), and for each two its functional dependency’s degree —
of the sampled rows whose value of one repeats, the fraction whose group
agrees on the other (a value seen once tells nothing of a sample; in a
whole table it determines). Equalities of columns to values in one AND
are then estimated together: one row if they cover a key; else by the
chain rule, from a set counted together (1 / its combinations), each other
column given those before it — combinations(T) / combinations(T and it)
for the largest T counted with it, else the strongest dependency on one
of them (degree + (1 − degree) · its own, PostgreSQL’s), else its own;
of the sets to start from, the likeliest outcome. A join’s equalities
between two inputs whose columns on a side were counted together select
1 / the combinations of the side with more; a GROUP BY of columns (and
a DISTINCT) makes, per table, its columns’ combinations (counted, or
their distinct values multiplied), at most its input’s rows.
nc_catalog.statistics_objects, .column_groups and .dependencies
show them. Without statistics the rule-based plan is kept.