2 doc/src/sgml/ref/analyze.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-ANALYZE">
8 <refentrytitle>ANALYZE</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>ANALYZE</refname>
15 <refpurpose>collect statistics about a database</refpurpose>
18 <indexterm zone="sql-analyze">
19 <primary>ANALYZE</primary>
24 ANALYZE [ VERBOSE ] [ <replaceable class="PARAMETER">table_name</replaceable> [ ( <replaceable class="PARAMETER">column_name</replaceable> [, ...] ) ] ]
29 <title>Description</title>
32 <command>ANALYZE</command> collects statistics about the contents
33 of tables in the database, and stores the results in the <link
34 linkend="catalog-pg-statistic"><structname>pg_statistic</></>
35 system catalog. Subsequently, the query planner uses these
36 statistics to help determine the most efficient execution plans for
41 With no parameter, <command>ANALYZE</command> examines every table in the
42 current database. With a parameter, <command>ANALYZE</command> examines
43 only that table. It is further possible to give a list of column names,
44 in which case only the statistics for those columns are collected.
49 <title>Parameters</title>
53 <term><literal>VERBOSE</literal></term>
56 Enables display of progress messages.
62 <term><replaceable class="PARAMETER">table_name</replaceable></term>
65 The name (possibly schema-qualified) of a specific table to
66 analyze. If omitted, all regular tables (but not foreign tables)
67 in the current database are analyzed.
73 <term><replaceable class="PARAMETER">column_name</replaceable></term>
76 The name of a specific column to analyze. Defaults to all columns.
84 <title>Outputs</title>
87 When <literal>VERBOSE</> is specified, <command>ANALYZE</> emits
88 progress messages to indicate which table is currently being
89 processed. Various statistics about the tables are printed as well.
97 Foreign tables are analyzed only when explicitly selected. Not all
98 foreign data wrappers support <command>ANALYZE</>. If the table's
99 wrapper does not support <command>ANALYZE</>, the command prints a
100 warning and does nothing.
104 In the default <productname>PostgreSQL</productname> configuration,
105 the autovacuum daemon (see <xref linkend="autovacuum">)
106 takes care of automatic analyzing of tables when they are first loaded
107 with data, and as they change throughout regular operation.
108 When autovacuum is disabled,
109 it is a good idea to run <command>ANALYZE</command> periodically, or
110 just after making major changes in the contents of a table. Accurate
111 statistics will help the planner to choose the most appropriate query
112 plan, and thereby improve the speed of query processing. A common
113 strategy for read-mostly databases is to run <xref linkend="sql-vacuum">
114 and <command>ANALYZE</command> once a day during a low-usage time of day.
115 (This will not be sufficient if there is heavy update activity.)
119 <command>ANALYZE</command>
120 requires only a read lock on the target table, so it can run in
121 parallel with other activity on the table.
125 The statistics collected by <command>ANALYZE</command> usually
126 include a list of some of the most common values in each column and
127 a histogram showing the approximate data distribution in each
128 column. One or both of these can be omitted if
129 <command>ANALYZE</command> deems them uninteresting (for example,
130 in a unique-key column, there are no common values) or if the
131 column data type does not support the appropriate operators. There
132 is more information about the statistics in <xref
133 linkend="maintenance">.
137 For large tables, <command>ANALYZE</command> takes a random sample
138 of the table contents, rather than examining every row. This
139 allows even very large tables to be analyzed in a small amount of
140 time. Note, however, that the statistics are only approximate, and
141 will change slightly each time <command>ANALYZE</command> is run,
142 even if the actual table contents did not change. This might result
143 in small changes in the planner's estimated costs shown by
144 <xref linkend="sql-explain">.
145 In rare situations, this non-determinism will cause the planner's
146 choices of query plans to change after <command>ANALYZE</command> is run.
147 To avoid this, raise the amount of statistics collected by
148 <command>ANALYZE</command>, as described below.
152 The extent of analysis can be controlled by adjusting the
153 <xref linkend="guc-default-statistics-target"> configuration variable, or
154 on a column-by-column basis by setting the per-column statistics
155 target with <command>ALTER TABLE ... ALTER COLUMN ... SET
156 STATISTICS</command> (see <xref linkend="sql-altertable">).
157 The target value sets the
158 maximum number of entries in the most-common-value list and the
159 maximum number of bins in the histogram. The default target value
160 is 100, but this can be adjusted up or down to trade off accuracy of
161 planner estimates against the time taken for
162 <command>ANALYZE</command> and the amount of space occupied in
163 <literal>pg_statistic</literal>. In particular, setting the
164 statistics target to zero disables collection of statistics for
165 that column. It might be useful to do that for columns that are
166 never used as part of the <literal>WHERE</>, <literal>GROUP BY</>,
167 or <literal>ORDER BY</> clauses of queries, since the planner will
168 have no use for statistics on such columns.
172 The largest statistics target among the columns being analyzed determines
173 the number of table rows sampled to prepare the statistics. Increasing
174 the target causes a proportional increase in the time and space needed
175 to do <command>ANALYZE</command>.
179 One of the values estimated by <command>ANALYZE</command> is the number of
180 distinct values that appear in each column. Because only a subset of the
181 rows are examined, this estimate can sometimes be quite inaccurate, even
182 with the largest possible statistics target. If this inaccuracy leads to
183 bad query plans, a more accurate value can be determined manually and then
185 <command>ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)</>
186 (see <xref linkend="sql-altertable">).
190 If the table being analyzed has one or more children,
191 <command>ANALYZE</command> will gather statistics twice: once on the
192 rows of the parent table only, and a second time on the rows of the
193 parent table with all of its children. This second set of statistics
194 is needed when planning queries that traverse the entire inheritance
195 tree. The autovacuum daemon, however, will only consider inserts or
196 updates on the parent table itself when deciding whether to trigger an
197 automatic analyze for that table. If that table is rarely inserted into
198 or updated, the inheritance statistics will not be up to date unless you
199 run <command>ANALYZE</command> manually.
203 If the table being analyzed is completely empty, <command>ANALYZE</command>
204 will not record new statistics for that table. Any existing statistics
210 <title>Compatibility</title>
213 There is no <command>ANALYZE</command> statement in the SQL standard.
218 <title>See Also</title>
220 <simplelist type="inline">
221 <member><xref linkend="sql-vacuum"></member>
222 <member><xref linkend="app-vacuumdb"></member>
223 <member><xref linkend="runtime-config-resource-vacuum-cost"></member>
224 <member><xref linkend="autovacuum"></member>