ANALYZE [ ( option [, ...] ) ] [ table_and_columns [, ...] ]
ANALYZE [ VERBOSE ] [ table_and_columns [, ...] ]
where option can be one of:
VERBOSE [ boolean ]
SKIP_LOCKED [ boolean ]
and table_and_columns is:
table_name [ ( column_name [, ...] ) ]
ANALYZE收集数据库中表内容的统计信息,并将结果存储在pg_statistic系统表中。随后,查询规划器使用这些统计信息来帮助确定查询的最有效执行计划。如果没有table_and_columns列表,ANALYZE将处理当前用户有权分析的当前数据库中的每个表和物化视图(RELKIND_RELATION
、RELKIND_MATVIEW
、RELKIND_PARTITIONED_TABLE
)。使用列表,只分析这些表。还可以为表提供列名称列表,在这种情况下,只收集这些列的统计信息。当选项列表被括号包围时,选项可以按任何顺序写入。在PostgreSQL 11中添加了括号语法;不推荐使用未解析的语法。ANALYZE collects statistics about the contents of tables in the database, and stores the results in the pg_statistic system catalog. Subsequently, the query planner uses these statistics to help determine the most efficient execution plans for queries. Without a table_and_columns list, ANALYZE processes every table and materialized view in the current database that the current user has permission to analyze. With a list, ANALYZE processes only those table(s). It is further possible to give a list of column names for a table, in which case only the statistics for those columns are collected. When the option list is surrounded by parentheses, the options can be written in any order. The parenthesized syntax was added in PostgreSQL 11; the unparenthesized syntax is deprecated.
参数:
VERBOSE:Enables display of progress messages.
SKIP_LOCKED:指定在开始处理表时,ANALYZE不应等待释放任何冲突的锁:如果无法在不等待的情况下立即锁定表,则跳过该表。请注意,即使使用此选项,在打开表的索引或从分区、表继承子表和某些类型的外部表获取样本行时,ANALYZE仍可能会阻塞。此外,虽然ANALYZE通常处理指定分区表的所有分区,但如果分区表上存在冲突锁,此选项将导致ANALYZE跳过所有分区。Specifies that ANALYZE should not wait for any conflicting locks to be released when beginning work on a relation: if a relation cannot be locked immediately without waiting, the relation is skipped. Note that even with this option, ANALYZE may still block when opening the relation’s indexes or when acquiring sample rows from partitions, table inheritance children, and some types of foreign tables. Also, while ANALYZE ordinarily processes all partitions of specified partitioned tables, this option will cause ANALYZE to skip all partitions if there is a conflicting lock on the partitioned table.
boolean:指定是否应打开或关闭所选选项。您可以写入TRUE、on或1以启用该选项,写入FALSE、off或0以禁用该选项。布尔值也可以省略,在这种情况下假定为真。Specifies whether the selected option should be turned on or off. You can write TRUE, ON, or 1 to enable the option, and FALSE, OFF, or 0 to disable it. The boolean value can also be omitted, in which case TRUE is assumed.
table_name:要分析的特定表的名称(可能是schema限定的)。如果省略,将分析当前数据库中的所有常规表、分区表和物化视图(但不分析外部表)。如果指定的表是分区表,则整个分区表的继承统计信息和各个分区的统计信息都会更新。The name (possibly schema-qualified) of a specific table to analyze. If omitted, all regular tables, partitioned tables, and materialized views in the current database are analyzed (but not foreign tables). If the specified table is a partitioned table, both the inheritance statistics of the partitioned table as a whole and statistics of the individual partitions are updated.
column_name:The name of a specific column to analyze. Defaults to all columns.
详情:
当指定了VERBOSE时,ANALYZE会打印进度消息以指示当前正在处理哪个表。关于表的各种统计数据也被打印出来。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.
要分析表,通常必须是表的所有者或超级用户。但是,允许数据库所有者分析其数据库中的所有表,shared catalogs除外。(对shared catalogs的限制意味着真正的数据库范围分析只能由超级用户执行。)分析将跳过调用用户无权分析的任何表。To analyze a table, one must ordinarily be the table’s owner or a superuser. However, database owners are allowed to analyze all tables in their databases, except shared catalogs. (The restriction for shared catalogs means that a true database-wide ANALYZE can only be performed by a superuser.) ANALYZE will skip over any tables that the calling user does not have permission to analyze.
仅当显式选择时,才会分析外部表。并非所有的外部数据包装器都支持分析。如果表的包装器不支持分析,则该命令将打印警告并不执行任何操作。Foreign tables are analyzed only when explicitly selected. Not all foreign data wrappers support ANALYZE. If the table’s wrapper does not support ANALYZE, the command prints a warning and does nothing.
在默认的PostgreSQL配置中,autovacuum守护程序(参见第24.1.6节)负责表首次加载数据时的自动分析,以及在整个常规操作过程中的变化。禁用自动真空时,最好定期运行分析,或者在对表的内容进行重大更改之后运行分析。准确的统计数据将帮助查询规划器选择最合适的查询计划,从而提高查询处理的速度。对于以读取为主的数据库,一种常见的策略是在一天中使用率较低的时间内每天运行一次VACUUM 和ANALYZE 。(如果有大量更新活动,这将是不够的。)In the default PostgreSQL configuration, the autovacuum daemon (see Section 24.1.6) 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 ANALYZE 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 for read-mostly databases is to run VACUUM and ANALYZE once a day during a low-usage time of day. (This will not be sufficient if there is heavy update activity.) 分析只需要目标表上的读锁,因此它可以与表上的其他活动并行运行。ANALYZE requires only a read lock on the target table, so it can run in parallel with other activity on the table.
通过分析收集的统计数据通常包括每列中一些最常见值的列表,以及显示每列中近似数据分布的直方图。如果ANALYZE认为它们无趣(例如,在唯一键列中,没有公共值),或者列数据类型不支持适当的运算符,则可以省略其中一个或两个。有关统计数据的更多信息,请参见第24章。The statistics collected by ANALYZE 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 ANALYZE 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 Chapter 24.
对于大型表,ANALYZE对表内容进行随机抽样,而不是检查每一行。这允许在很短的时间内分析非常大的表。但是,请注意,统计数据只是近似值,每次运行分析时都会略有变化,即使实际的表内容没有变化。这可能会导致查询规划器的估算成本发生微小变化,如解释所示。在极少数情况下,这种非确定性会导致查询规划器在运行分析后更改查询计划的选择。为了避免这种情况,请提高Analysis收集的统计数据量,如下所述。For large tables, ANALYZE 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 ANALYZE 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 EXPLAIN. In rare situations, this non-determinism will cause the planner’s choices of query plans to change after ANALYZE is run. To avoid this, raise the amount of statistics collected by ANALYZE, as described below.
可以通过调整default_statistics_target配置变量来控制分析范围,也可以通过使用ALTER TABLE…ALTER column…SET statistics
设置每列统计目标来逐列控制分析范围(请参见ALTER TABLE)。目标值设置最常用值列表中的最大条目数和直方图中的最大箱数。默认目标值为100,但可以向上或向下调整,以根据分析所需时间和pg_statistic中占用的空间量权衡查询规划器估计的准确性。特别是,将统计信息目标设置为零将禁用该列的统计信息收集。对于从不作为查询的WHERE、GROUP BY或ORDER BY子句的一部分使用的列,这样做可能很有用,因为规划器将不使用此类列的统计信息。The extent of analysis can be controlled by adjusting the default_statistics_target configuration 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). 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 ANALYZE and the amount of space occupied in pg_statistic. 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 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 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 ANALYZE.
ANALYZE估计的值之一是每列中出现的不同值的数量。因为只检查了行的一个子集,所以即使使用最大可能的统计目标,这种估计有时也可能非常不准确。如果这种不准确导致错误的查询计划,则可以手动确定更准确的值,然后使用ALTER TABLE…ALTER COLUMN…SET(n_distinct=…)
改变(请参见ALTER TALLE)。One of the values estimated by ANALYZE is the number of distinct values that appear in each column. Because only a subset of the rows are examined, this estimate can sometimes be quite inaccurate, even with the largest possible statistics target. If this inaccuracy leads to bad query plans, a more accurate value can be determined manually and then installed with ALTER TABLE … ALTER COLUMN … SET (n_distinct = …) (see ALTER TABLE).
如果正在分析的表有一个或多个子表,则ANALYZE将收集两次统计信息:一次仅收集父表的行,第二次收集父表及其所有子表的行。在规划遍历整个继承树的查询时,需要第二组统计信息。然而,在决定是否触发对父表的自动分析时,autovacuum守护进程将只考虑父表本身的插入或更新。如果该表很少插入或更新,除非您手动运行ANALYZE,否则继承统计信息将不会是最新的。
If the table being analyzed has one or more children, ANALYZE will gather statistics twice: once on the rows of the parent table only, and a second time on the rows of the parent table with all of its children. This second set of statistics is needed when planning queries that traverse the entire inheritance tree. The autovacuum daemon, however, will only consider inserts or updates on the parent table itself when deciding whether to trigger an automatic analyze for that table. If that table is rarely inserted into or updated, the inheritance statistics will not be up to date unless you run ANALYZE manually.对于分区表,ANALYZE通过从所有分区中采样行来收集统计信息;此外,它将递归到每个分区并更新其统计信息。每个叶分区只分析一次,即使使用多级分区也是如此。仅父表(不包括其分区中的数据)没有收集统计信息,因为分区保证它是空的。For partitioned tables, ANALYZE gathers statistics by sampling rows from all partitions; in addition, it will recurse into each partition and update its statistics. Each leaf partition is analyzed only once, even with multi-level partitioning. No statistics are collected for only the parent table (without data from its partitions), because with partitioning it’s guaranteed to be empty.相比之下,如果正在分析的表具有继承子表,则 ANALYZE 会收集两组统计信息:一组仅针对父表的行,另一组包括父表及其所有子表的行。在规划将继承树作为一个整体处理的查询时,需要第二组统计信息。在这种情况下,不会单独分析子表本身。By contrast, if the table being analyzed has inheritance children, ANALYZE gathers two sets of statistics: one on the rows of the parent table only, and a second including rows of both the parent table and all of its children. This second set of statistics is needed when planning queries that process the inheritance tree as a whole. The child tables themselves are not individually analyzed in this case.autovacuum 守护进程不处理分区表,也不会处理继承父级(如果只修改了子级)。通常需要定期运行手动 ANALYZE 以使表层次结构的统计信息保持最新。The autovacuum daemon does not process partitioned tables, nor does it process inheritance parents if only the children are ever modified. It is usually necessary to periodically run a manual ANALYZE to keep the statistics of the table hierarchy up to date.
如果任何子表或分区是其外部数据包装器不支持 ANALYZE 的外部表,则在收集继承统计信息时将忽略这些表。If any child tables or partitions are foreign tables whose foreign data wrappers do not support ANALYZE, those tables are ignored while gathering inheritance statistics. 如果正在分析的表完全为空,则 ANALYZE 将不会为该表记录新的统计信息。任何现有的统计数据都将被保留。If the table being analyzed is completely empty, ANALYZE will not record new statistics for that table. Any existing statistics will be retained.
上述内容摘自PostgreSQL手册,用于理解分析的主要知识点。
文章评论