<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >ANALYZE</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.4.12 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="ALTER VIEW" HREF="sql-alterview.html"><LINK REL="NEXT" TITLE="BEGIN" HREF="sql-begin.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2012-05-31T23:30:11"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.4.12 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-alterview.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-alterview.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-begin.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-begin.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-ANALYZE" ></A >ANALYZE</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN54984" ></A ><H2 >Name</H2 >ANALYZE -- collect statistics about a database</DIV ><A NAME="AEN54987" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN54989" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >ANALYZE [ VERBOSE ] [ <TT CLASS="REPLACEABLE" ><I >table</I ></TT > [ ( <TT CLASS="REPLACEABLE" ><I >column</I ></TT > [, ...] ) ] ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN54993" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >ANALYZE</TT > collects statistics about the contents of tables in the database, and stores the results in the <A HREF="catalog-pg-statistic.html" ><TT CLASS="STRUCTNAME" >pg_statistic</TT ></A > system catalog. Subsequently, the query planner uses these statistics to help determine the most efficient execution plans for queries. </P ><P > With no parameter, <TT CLASS="COMMAND" >ANALYZE</TT > examines every table in the current database. With a parameter, <TT CLASS="COMMAND" >ANALYZE</TT > examines only that table. It is further possible to give a list of column names, in which case only the statistics for those columns are collected. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55002" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >VERBOSE</TT ></DT ><DD ><P > Enables display of progress messages. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >table</I ></TT ></DT ><DD ><P > The name (possibly schema-qualified) of a specific table to analyze. Defaults to all tables in the current database. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >column</I ></TT ></DT ><DD ><P > The name of a specific column to analyze. Defaults to all columns. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55020" ></A ><H2 >Outputs</H2 ><P > When <TT CLASS="LITERAL" >VERBOSE</TT > is specified, <TT CLASS="COMMAND" >ANALYZE</TT > emits progress messages to indicate which table is currently being processed. Various statistics about the tables are printed as well. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55025" ></A ><H2 >Notes</H2 ><P > In the default <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > configuration, <A HREF="routine-vacuuming.html#AUTOVACUUM" ><I >The Autovacuum Daemon</I ></A > takes care of automatic analyzing of tables when they are first loaded with data, and as they change throughout regular operation. When autovacuum is disabled, it is a good idea to run <TT CLASS="COMMAND" >ANALYZE</TT > periodically, or just after making major changes in the contents of a table. Accurate statistics will help the planner to choose the most appropriate query plan, and thereby improve the speed of query processing. A common strategy is to run <A HREF="sql-vacuum.html" ><I >VACUUM</I ></A > and <TT CLASS="COMMAND" >ANALYZE</TT > once a day during a low-usage time of day. </P ><P > <TT CLASS="COMMAND" >ANALYZE</TT > requires only a read lock on the target table, so it can run in parallel with other activity on the table. </P ><P > The statistics collected by <TT CLASS="COMMAND" >ANALYZE</TT > usually include a list of some of the most common values in each column and a histogram showing the approximate data distribution in each column. One or both of these can be omitted if <TT CLASS="COMMAND" >ANALYZE</TT > deems them uninteresting (for example, in a unique-key column, there are no common values) or if the column data type does not support the appropriate operators. There is more information about the statistics in <A HREF="maintenance.html" >Chapter 23</A >. </P ><P > For large tables, <TT CLASS="COMMAND" >ANALYZE</TT > takes a random sample of the table contents, rather than examining every row. This allows even very large tables to be analyzed in a small amount of time. Note, however, that the statistics are only approximate, and will change slightly each time <TT CLASS="COMMAND" >ANALYZE</TT > is run, even if the actual table contents did not change. This might result in small changes in the planner's estimated costs shown by <A HREF="sql-explain.html" ><I >EXPLAIN</I ></A >. In rare situations, this non-determinism will cause the planner's choices of query plans to change after <TT CLASS="COMMAND" >ANALYZE</TT > is run. To avoid this, raise the amount of statistics collected by <TT CLASS="COMMAND" >ANALYZE</TT >, as described below. </P ><P > The extent of analysis can be controlled by adjusting the <A HREF="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET" >default_statistics_target</A > configuration variable, or on a column-by-column basis by setting the per-column statistics target with <TT CLASS="COMMAND" >ALTER TABLE ... ALTER COLUMN ... SET STATISTICS</TT > (see <A HREF="sql-altertable.html" ><I >ALTER TABLE</I ></A >). The target value sets the maximum number of entries in the most-common-value list and the maximum number of bins in the histogram. The default target value is 100, but this can be adjusted up or down to trade off accuracy of planner estimates against the time taken for <TT CLASS="COMMAND" >ANALYZE</TT > and the amount of space occupied in <TT CLASS="LITERAL" >pg_statistic</TT >. In particular, setting the statistics target to zero disables collection of statistics for that column. It might be useful to do that for columns that are never used as part of the <TT CLASS="LITERAL" >WHERE</TT >, <TT CLASS="LITERAL" >GROUP BY</TT >, or <TT CLASS="LITERAL" >ORDER BY</TT > clauses of queries, since the planner will have no use for statistics on such columns. </P ><P > The largest statistics target among the columns being analyzed determines the number of table rows sampled to prepare the statistics. Increasing the target causes a proportional increase in the time and space needed to do <TT CLASS="COMMAND" >ANALYZE</TT >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55056" ></A ><H2 >Compatibility</H2 ><P > There is no <TT CLASS="COMMAND" >ANALYZE</TT > statement in the SQL standard. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN55060" ></A ><H2 >See Also</H2 ><A HREF="sql-vacuum.html" ><I >VACUUM</I ></A >, <A HREF="app-vacuumdb.html" ><I ><I >vacuumdb</I ></I ></A >, <A HREF="runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-VACUUM-COST" ><I >Cost-Based Vacuum Delay</I ></A >, <A HREF="routine-vacuuming.html#AUTOVACUUM" ><I >The Autovacuum Daemon</I ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-alterview.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-begin.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ALTER VIEW</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >BEGIN</TD ></TR ></TABLE ></DIV ></BODY ></HTML >