1ANALYZE() SQL Commands ANALYZE()
2
3
4
6 ANALYZE - collect statistics about a database
7
8
10 ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ]
11
12
14 ANALYZE collects statistics about the contents of tables in the data‐
15 base, and stores the results in the system table pg_statistic. Subse‐
16 quently, the query planner uses these statistics to help determine the
17 most efficient execution plans for queries.
18
19 With no parameter, ANALYZE examines every table in the current data‐
20 base. With a parameter, ANALYZE examines only that table. It is further
21 possible to give a list of column names, in which case only the statis‐
22 tics for those columns are collected.
23
25 VERBOSE
26 Enables display of progress messages.
27
28 table The name (possibly schema-qualified) of a specific table to ana‐
29 lyze. Defaults to all tables in the current database.
30
31 column The name of a specific column to analyze. Defaults to all col‐
32 umns.
33
35 When VERBOSE is specified, ANALYZE emits progress messages to indicate
36 which table is currently being processed. Various statistics about the
37 tables are printed as well.
38
40 It is a good idea to run ANALYZE periodically, or just after making
41 major changes in the contents of a table. Accurate statistics will help
42 the planner to choose the most appropriate query plan, and thereby
43 improve the speed of query processing. A common strategy is to run VAC‐
44 UUM [vacuum(7)] and ANALYZE once a day during a low-usage time of day.
45
46 Unlike VACUUM FULL, ANALYZE requires only a read lock on the target ta‐
47 ble, so it can run in parallel with other activity on the table.
48
49 The statistics collected by ANALYZE usually include a list of some of
50 the most common values in each column and a histogram showing the
51 approximate data distribution in each column. One or both of these may
52 be omitted if ANALYZE deems them uninteresting (for example, in a
53 unique-key column, there are no common values) or if the column data
54 type does not support the appropriate operators. There is more informa‐
55 tion about the statistics in in the documentation.
56
57 For large tables, ANALYZE takes a random sample of the table contents,
58 rather than examining every row. This allows even very large tables to
59 be analyzed in a small amount of time. Note, however, that the statis‐
60 tics are only approximate, and will change slightly each time ANALYZE
61 is run, even if the actual table contents did not change. This may
62 result in small changes in the planner's estimated costs shown by
63 EXPLAIN. In rare situations, this non-determinism will cause the query
64 optimizer to choose a different query plan between runs of ANALYZE. To
65 avoid this, raise the amount of statistics collected by ANALYZE, as
66 described below.
67
68 The extent of analysis can be controlled by adjusting the default_sta‐
69 tistics_target configuration variable, or on a column-by-column basis
70 by setting the per-column statistics target with ALTER TABLE ... ALTER
71 COLUMN ... SET STATISTICS (see ALTER TABLE [alter_table(7)]). The tar‐
72 get value sets the maximum number of entries in the most-common-value
73 list and the maximum number of bins in the histogram. The default tar‐
74 get value is 10, but this can be adjusted up or down to trade off accu‐
75 racy of planner estimates against the time taken for ANALYZE and the
76 amount of space occupied in pg_statistic. In particular, setting the
77 statistics target to zero disables collection of statistics for that
78 column. It may be useful to do that for columns that are never used as
79 part of the WHERE, GROUP BY, or ORDER BY clauses of queries, since the
80 planner will have no use for statistics on such columns.
81
82 The largest statistics target among the columns being analyzed deter‐
83 mines the number of table rows sampled to prepare the statistics.
84 Increasing the target causes a proportional increase in the time and
85 space needed to do ANALYZE.
86
88 There is no ANALYZE statement in the SQL standard.
89
90
91
92SQL - Language Statements 2008-06-08 ANALYZE()