Referencia · §8.13

The SQL analyzer

profile, statements, lint, advisor

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

EXPLAIN ANALYZE <query> (also EXPLAIN (ANALYZE [bool], TIMING [bool]) …; other options are refused with 0A000) runs the query and prints the plan with, on every node, the optimizer’s estimate beside what the node did: (rows=40) (actual rows=38 loops=1 time=0.120 ms) — rows over all its starts, the starts (a nested loop’s inner side, a correlated subquery: once per outer row), and the time spent in it, its inputs included; a scan a hash join’s key filter narrowed adds (join key filter) (§8.5). A node the run never reached says (never executed); a query that called embedding models has a line Models: n calls, n texts sent, n cached, n tokens, n errors, t ms (§8.27); the last line is Execution: n rows in t ms. TIMING OFF leaves the times out (repeatable output, for tests). Time is the database’s monotonic clock (Kv::elapsed). The query runs with the session’s privileges, budget and row-level security, like the query itself; only queries for now (UPDATE and DELETE have plain EXPLAIN); it reads the columns the query reads, as it would run. A large gap between rows= and actual rows= points at stale or missing statistics (ANALYZE).

Statement statistics (nc_catalog.statements): every statement a front end (or Session::execute) gave the text of, per role (current_user) and normalized text — constants replaced by $n (numbered after the statement’s own parameters), white space as one space; case and spacing do not split a statement, and the text shown is the first run’s. Per statement: calls, failures, total, mean, shortest and longest time (ms), rows read (scanned) and rows returned or changed, its calls to embedding models (calls, texts sent, cache hits, tokens, errors, time: §8.27), the last call. An administrator sees every role’s, anyone else their own; nc_statements_reset() forgets every role’s (an administrator) or the caller’s own. They live in the process, by database (a restart starts over), at most 5000 statements (the least called makes room); statements of the database’s own probe sessions are not counted.

The linter: ANALYZE QUERY <statement> (a query, UPDATE or DELETE; ANALYZE query alone still analyzes a table named query) plans the statement without running it and returns one row per finding: rule, place (the table and the condition, with column names; the join; the subquery), finding and fix. Rules, over the optimized plan and its subqueries:

Rule Fires on Fix
full_scan a table read in full for a filter that keeps a tenth of it or less (by the statistics, else the default selectivities) and that an index could serve; not for an analyzed table under 1000 rows CREATE INDEX ON t (…): the equality columns, then one range column
index_defeated a comparison with a value where the first column of an index (or the primary key) is under a function, arithmetic or a cast (the binder’s too), and the scan uses neither that index nor an expression index compare the column itself; move the arithmetic; cast the other side; or index the expression (CREATE INDEX ON t ((expr)))
foreign_key_without_index a foreign key whose columns lead no index, of a table the statement reads — or, for UPDATE / DELETE, of a table referencing the one changed CREATE INDEX ON t (…)
cartesian_product a join with no condition (or ON true) between inputs estimated at more than one row each a join condition (ignore when every pair is meant)
not_in_nullable NOT IN (subquery) whose column can be NULL (nullability followed through scans, filters that reject NULLs, projections, joins, aggregates) NOT EXISTS, or IS NOT NULL in the subquery

SELECT * in views comes with views.

The index advisor (nc_catalog.index_advice): from the statement statistics (the caller’s own, or every role’s for an administrator) and ANALYZE data, the indexes that would have paid off. Each recorded query, UPDATE and DELETE is planned again from its last run’s text; its full scans of analyzed tables of 1000 rows or more give candidates (the filter’s equality columns, then one range column). Each candidate is put on its table as a hypothetical index — the catalog reads a definition with it in place of the stored one (catalog::with_hypothetical), so the binder and optimizer plan as if it existed — and every statement is planned again: where the optimizer picks it and the estimated rows read fall by a tenth or more, it serves that statement. Each statement counts, by its calls, for the candidate that serves it best (a tie goes to the one of more columns), so one index on (region, score) stands for statements on region alone too. Columns: table_name, columns, create_statement (CREATE INDEX ON t (…)), statements, calls, rows_read and rows_read_with_index (estimated, over the calls), example_query (the one it helps most, normalized) and plan (that statement’s plan with the index, with its constants). Most rows saved first. Joins do not give candidates yet (the executor has no index nested loop).