Seth Woolley's Man Viewer

analyze(7) - ANALYZE - collect statistics about a database - man 7 analyze

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

ANALYZE(7)                       SQL Commands                       ANALYZE(7)



NAME
       ANALYZE - collect statistics about a database


SYNOPSIS
       ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ]


DESCRIPTION
       ANALYZE  collects  statistics about the contents of tables in(1,8) the data-
       base, and stores the results in(1,8) the system table  pg_statistic.  Subse-
       quently,  the query planner uses these statistics to help determine the
       most efficient execution plans for queries.

       With no parameter, ANALYZE examines every table in(1,8)  the  current  data-
       base. With a parameter, ANALYZE examines only that table. It is further
       possible to give a list of column names, in(1,8) which case only the statis-
       tics for those columns are collected.

PARAMETERS
       VERBOSE
              Enables display of progress messages.

       table  The name (possibly schema-qualified) of a specific table to ana-
              lyze. Defaults to all tables in(1,8) the current database.

       column The name of a specific column to analyze. Defaults to  all  col-
              umns.

OUTPUTS
       When  VERBOSE is specified, ANALYZE emits progress messages to indicate
       which table is currently being processed. Various statistics about  the
       tables are printed as well.

NOTES
       It  is  a  good  idea to run ANALYZE periodically, or just after making
       major changes in(1,8) 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 VAC-
       UUM  [vacuum(7)] and ANALYZE once a day during a low-usage time(1,2,n) of day.

       Unlike VACUUM FULL, ANALYZE requires only a read(2,n,1 builtins) lock on the target ta-
       ble, so it can run in(1,8) parallel with other activity on the table.

       The  statistics  collected by ANALYZE usually include a list of some of
       the most common values in(1,8) each  column  and  a  histogram  showing  the
       approximate  data distribution in(1,8) each column. One or both of these may
       be omitted if(3,n) ANALYZE deems  them  uninteresting  (for  example,  in(1,8)  a
       unique-key  column,  there  are no common values) or if(3,n) the column data
       type does not support the appropriate operators. There is more informa-
       tion  about  the  statistics  in(1,8)  the chapter called ``Routine Database
       Maintenance'' in(1,8) the documentation.

       For large tables, ANALYZE takes a random(3,4,6) sample of the table  contents,
       rather  than examining every row. This allows even very large tables to
       be analyzed in(1,8) a small amount of time. Note, however, that the  statis-
       tics  are  only approximate, and will change slightly each time(1,2,n) ANALYZE
       is run, even if(3,n) the actual table contents  did  not  change.  This  may
       result  in(1,8)  small  changes  in(1,8)  the  planner's estimated costs shown by
       EXPLAIN. In rare situations, this non-determinism will cause the  query
       optimizer  to choose a different query plan between runs of ANALYZE. To
       avoid this, raise(3,n) the amount of statistics  collected  by  ANALYZE,  as
       described below.

       The  extent of analysis can be controlled by adjusting the DEFAULT_STA-
       TISTICS_TARGET parameter variable, or on a  column-by-column  basis  by
       setting  the  per-column  statistics  target with ALTER TABLE ... ALTER
       COLUMN ... SET STATISTICS (see ALTER TABLE [alter_table(7)]). The  tar-
       get  value  sets the maximum number of entries in(1,8) the most-common-value
       list and the maximum number of bins in(1,8) the histogram. The default  tar-
       get value is 10, but this can be adjusted up or down to trade off accu-
       racy of planner estimates against the time(1,2,n) taken for  ANALYZE  and  the
       amount  of  space  occupied in(1,8) pg_statistic. In particular, setting the
       statistics target to zero disables collection of  statistics  for  that
       column.  It may be useful to do that for columns that are never used as
       part of the WHERE, GROUP BY, or ORDER BY clauses of queries, since  the
       planner will have no use for statistics on such columns.

       The  largest  statistics target among the columns being analyzed deter-
       mines the number of table  rows  sampled  to  prepare  the  statistics.
       Increasing  the  target  causes a proportional increase in(1,8) the time(1,2,n) and
       space needed to do ANALYZE.

COMPATIBILITY
       There is no ANALYZE statement in(1,8) the SQL standard.



SQL - Language Statements         2003-11-02                        ANALYZE(7)

References for this manual (incoming links)