ANALYZE <refpurpose>collect statistics about a database</refpurpose> — データベースに関する統計を収集する
ANALYZE [ (option[, ...] ) ] [table_and_columns[, ...] ] <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase> ここでoptionは以下の一つです。 VERBOSE [boolean] SKIP_LOCKED [boolean] BUFFER_USAGE_LIMITsize<phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase> またtable_and_columnsは以下の通りです。 [ ONLY ]table_name[ * ] [ (column_name[, ...] ) ]
<command>ANALYZE</command> collects statistics about the contents
of tables in the database, and stores the results in the <link
linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link>
system catalog. Subsequently, the query planner uses these
statistics to help determine the most efficient execution plans for
queries.
ANALYZEはデータベース内のテーブルの内容に関する統計情報を収集し、その結果をpg_statisticシステムカタログに保存します。
問い合わせプランナが最も効率の良い問い合わせの実行計画を決定する際、この統計情報が使用されます。
Without a <replaceable class="parameter">table_and_columns</replaceable>
list, <command>ANALYZE</command> processes every table and materialized view
in the current database that the current user has permission to analyze.
With a list, <command>ANALYZE</command> 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.
table_and_columnsリストがない場合、ANALYZEは現在のデータベース内で現在のユーザが解析する権限のあるすべてのテーブルとマテリアライズドビューを処理します。
リストがある場合、ANALYZEは指定されたテーブルのみを処理します。
さらにテーブルの列名のリストを与え、その列の統計情報のみを収集することも可能です。
VERBOSE
Enables display of progress messages at <literal>INFO</literal> level.
INFOレベルで進行状況の表示を有効にします。
SKIP_LOCKED
Specifies that <command>ANALYZE</command> 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, <command>ANALYZE</command>
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 <command>ANALYZE</command>
ordinarily processes all partitions of specified partitioned tables,
this option will cause <command>ANALYZE</command> to skip all
partitions if there is a conflicting lock on the partitioned table.
リレーション上で動作を開始する時に、ANALYZEは衝突しているロックが解放されるのを待たないことを指定します。リレーションを待つことなく即時にロックできない場合、そのリレーションは飛ばされます。
このオプションを指定しても、リレーションのインデックスを開く時、パーティションやテーブル継承の子、ある種類の外部テーブルからサンプル行を取得する時には、ANALYZEがブロックするかもしれないことに注意してください。
また、通常ANALYZEは指定されたパーティションテーブルのパーティションをすべて処理しますが、このオプションによりANALYZEは、パーティションテーブル上に衝突するロックがあれば、パーティションをすべてスキップします。
BUFFER_USAGE_LIMIT
Specifies the
<glossterm linkend="glossary-buffer-access-strategy">Buffer Access Strategy</glossterm>
ring buffer size for <command>ANALYZE</command>. This size is used to
calculate the number of shared buffers which will be reused as part of
this strategy. <literal>0</literal> disables use of a
<literal>Buffer Access Strategy</literal>. When this option is not
specified, <command>ANALYZE</command> uses the value from
<xref linkend="guc-vacuum-buffer-usage-limit"/>. Higher settings can
allow <command>ANALYZE</command> to run more quickly, but having too
large a setting may cause too many other useful pages to be evicted from
shared buffers. The minimum value is <literal>128 kB</literal> and the
maximum value is <literal>16 GB</literal>.
ANALYZEのバッファアクセスストラテジリングバッファサイズを指定します。
このサイズは、このストラテジのパートとして再利用される共有バッファの数を計算するために使用されます。
0はバッファアクセスストラテジの使用を無効にします。
このオプションが指定されていない場合、ANALYZEはvacuum_buffer_usage_limitの値を使用します。
設定を高くするとANALYZEの実行速度が速くなりますが、設定が大き過ぎると、とても多くの他の有用なページが共有バッファから追い出されてしまう可能性があります。
最小値は128 kBで、最大値は16 GBです。
boolean
Specifies whether the selected option should be turned on or off.
You can write <literal>TRUE</literal>, <literal>ON</literal>, or
<literal>1</literal> to enable the option, and <literal>FALSE</literal>,
<literal>OFF</literal>, or <literal>0</literal> to disable it. The
<replaceable class="parameter">boolean</replaceable> value can also
be omitted, in which case <literal>TRUE</literal> is assumed.
選択したオプションを有効にするか無効にするか指定します。
オプションを有効にする場合にはTRUE、ONまたは1と書くことができ、無効にする場合にはFALSE、OFFまたは0と書くことができます。
booleanの値は省略することもでき、その場合にはTRUEとみなされます。
size
Specifies an amount of memory in kilobytes. Sizes may also be specified
as a string containing the numerical size followed by any one of the
following memory units: <literal>B</literal> (bytes),
<literal>kB</literal> (kilobytes), <literal>MB</literal> (megabytes),
<literal>GB</literal> (gigabytes), or <literal>TB</literal> (terabytes).
メモリの量をキロバイト単位で指定します。
サイズは、数値のサイズに続いて、B(バイト)、kB(キロバイト)、MB(メガバイト)、GB(ギガバイト)またはTB(テラバイト)のいずれか1つのメモリ単位を含む文字列として指定することもできます。
table_name
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 <literal>ONLY</literal> is specified before
the table name, only that table is analyzed. If <literal>ONLY</literal>
is not specified, the table and all its inheritance child tables or
partitions (if any) are analyzed. Optionally, <literal>*</literal>
can be specified after the table name to explicitly indicate that
inheritance child tables (or partitions) are to be analyzed.
解析の対象とするテーブルの名前です(スキーマ修飾名も可)。
省略された場合、現在のデータベースの中のすべての通常のテーブル、パーティションテーブル、マテリアライズドビュー(外部テーブルは除く)が解析されます。
テーブル名の前にONLYが指定されている場合は、そのテーブルのみ解析されます。
ONLYが指定されていない場合は、対象のテーブルとその継承の子テーブルやパーティション(もしあれば)がすべて解析されます。
オプションで、テーブル名の後に*を指定することで、継承の子テーブル(またはパーティション)が解析の対象であると明示的に示すことができます。
column_nameThe name of a specific column to analyze. Defaults to all columns. 解析の対象とする列名です。 デフォルトは全ての列です。
When <literal>VERBOSE</literal> is specified, <command>ANALYZE</command> emits
progress messages to indicate which table is currently being
processed. Various statistics about the tables are printed as well.
VERBOSEが指定された場合、ANALYZEは進捗メッセージとして処理中のテーブルを表示します。
さらに、テーブルについての各種統計情報も表示されます。
To analyze a table, one must ordinarily have the <literal>MAINTAIN</literal>
privilege on the table. However, database owners are allowed to
analyze all tables in their databases, except shared catalogs.
<command>ANALYZE</command> will skip over any tables that the calling user
does not have permission to analyze.
テーブルを解析するためには、通常そのテーブルに対するMAINTAIN権限が必要です。
しかしながら、データベースの所有者は、共有カタログを除いて、そのデータベースのテーブルをすべて解析できます。
ANALYZEは呼び出したユーザが解析する権限のないテーブルをスキップします。
Foreign tables are analyzed only when explicitly selected. Not all
foreign data wrappers support <command>ANALYZE</command>. If the table's
wrapper does not support <command>ANALYZE</command>, the command prints a
warning and does nothing.
外部テーブルは明示的に選択された場合にのみ解析されます。
すべての外部データラッパーがANALYZEをサポートしているとは限りません。
テーブルのラッパーがANALYZEをサポートしない場合、コマンドは警告を出力し、何も行いません。
In the default <productname>PostgreSQL</productname> configuration,
the autovacuum daemon (see <xref linkend="autovacuum"/>)
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 <command>ANALYZE</command> 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 <link linkend="sql-vacuum"><command>VACUUM</command></link>
and <command>ANALYZE</command> once a day during a low-usage time of day.
(This will not be sufficient if there is heavy update activity.)
デフォルトのPostgreSQLの設定では、自動バキュームデーモン(24.1.6参照)が、データが最初にロードされた時や通常の操作を通して変更された時にテーブルの自動解析まで面倒をみます。
もし自動バキュームが無効にしているならばANALYZEは定期的に、もしくは、テーブルの内容に大きな変更がある度に行うことを推奨します。
統計情報が正確であれば、プランナが最も適切な問い合わせ計画を選択できるようになります。
これによって、問い合わせ処理の速度が向上します。
読み取りの多いデータベースでは、VACUUMとANALYZEは、1日1回、データベースがあまり使用されていない時間帯に実行することが一般的です。
(非常に更新が激しい場合、これでは十分ではありません。)
While <command>ANALYZE</command> is running, the <xref
linkend="guc-search-path"/> is temporarily changed to <literal>pg_catalog,
pg_temp</literal>.
ANALYZEの実行中、search_pathは一時的にpg_catalog, pg_tempに変更されます。
<command>ANALYZE</command>
requires only a read lock on the target table, so it can run in
parallel with other non-DDL activity on the table.
ANALYZEは、対象とするテーブルの読み取りロックのみを必要とします。したがって、そのテーブルに対する他の非DDL操作と並行して実行できます。
The statistics collected by <command>ANALYZE</command> 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
<command>ANALYZE</command> 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 <xref
linkend="maintenance"/>.
通常、ANALYZEによって収集される統計情報には、各列の最頻値と各列の近似的なデータ分布を示すヒストグラムが含まれます。
ANALYZEによってあまり意味がないとみなされた場合(例えば、一意性制約が付加された列では、頻出値というものは存在しません)や、列のデータ型が適切な演算子をサポートしていない場合は、片方もしくは両方の情報を省略することがあります。
第24章に、統計情報についての詳細が記載されています。
For large tables, <command>ANALYZE</command> 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 <command>ANALYZE</command> 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
<link linkend="sql-explain"><command>EXPLAIN</command></link>.
In rare situations, this non-determinism will cause the planner's
choices of query plans to change after <command>ANALYZE</command> is run.
To avoid this, raise the amount of statistics collected by
<command>ANALYZE</command>, as described below.
巨大なテーブルでは、ANALYZEは、全ての行を検査するのではなく、テーブルの中からランダムにサンプルを取り出して使用します。
これによって、非常に巨大なテーブルであっても短時間で解析することが可能です。
しかし、このようにして得られた統計情報はおおよそのものでしかなく、テーブルの内容に変更がなくてもANALYZEを実行する度に変化することに注意してください。
これにより、EXPLAINが表示する、プランナの推定コストも多少変化する可能性があります。
稀に、このような不確定要素のせいで、プランナがANALYZEを実行した後に異なる問い合わせ計画を選択してしまうことがあります。
これを防止するには、以下に示すようにANALYZEで収集される統計情報の量を増やしてください。
The extent of analysis can be controlled by adjusting the
<xref linkend="guc-default-statistics-target"/> configuration variable, or
on a column-by-column basis by setting the per-column statistics
target with <link linkend="sql-altertable"><command>ALTER TABLE ... ALTER COLUMN ... SET
STATISTICS</command></link>.
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
<command>ANALYZE</command> and the amount of space occupied in
<literal>pg_statistic</literal>. 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 <literal>WHERE</literal>, <literal>GROUP BY</literal>,
or <literal>ORDER BY</literal> clauses of queries, since the planner will
have no use for statistics on such columns.
設定パラメータ変数default_statistics_targetを調整するか、もしくはALTER TABLE ... ALTER COLUMN ... SET STATISTICSを使用して列単位の統計目標を列毎に設定することで、解析の範囲を制御することができます。
目標値として設定するのは、典型的な値のリストにおけるエントリ数の最大値とヒストグラムのビンの最大数です。
デフォルトの目標値は100です。
しかし、この値は、プランナの推定精度とANALYZEの処理時間、pg_statisticの占める容量とのトレードオフによって大きくも小さくも調整されることがあります。
目標値を0に設定すると、その列に関する統計情報の収集は無効になります。
決してWHERE句、GROUP BY句、ORDER BY句に使用されない列に対しては、このような設定が有用です。
プランナにとってそのような列の統計情報は不要だからです。
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 <command>ANALYZE</command>.
解析対象列の統計情報目標値の最大値によって、統計情報を作成するためにテーブルから抽出する行数が決定します。
目標値を大きくすると、比例して、ANALYZEに要する時間とディスク容量が増加します。
One of the values estimated by <command>ANALYZE</command> 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
<link linkend="sql-altertable"><command>ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)</command></link>.
ANALYZEで推定される値の1つに各列に出現する個別値の個数があります。
行の部分集合のみしか検査されませんので、統計情報の対象をできる限り大きくしたとしても、この推定値はかなり不正確になることが時々あり得ます。
この不正確性のために悪い問い合わせ計画となる場合、より正確な値を手作業で求めて、ALTER TABLE ... ALTER COLUMN ... SET (n_distinct = ...)で設定することができます。
If the table being analyzed has inheritance children,
<command>ANALYZE</command> 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
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
<command>ANALYZE</command> manually. By default,
<command>ANALYZE</command> will also recursively collect and update the
statistics for each inheritance child table. The <literal>ONLY</literal>
keyword may be used to disable this.
解析中のテーブルが継承の子テーブルを持つ場合、ANALYZEは統計情報を2組収集します。1組目は親テーブルのみの行に関するもので、2組目は親テーブルとその子テーブルすべての両方の行を含むものです。
継承ツリーを全体として処理する問い合わせの計画作成では、この2組目の統計情報が必要です。
しかし自動バキュームデーモンでは、そのテーブルに対して自動的に解析を行うかどうかを決定する際に親テーブル上の挿入や更新のみを考慮します。
このテーブルへの挿入や更新がほとんどなければ、継承関係に対する統計情報は手作業でANALYZEを実行しない限り最新状態にはなりません。
デフォルトでは、ANALYZEは各継承の子テーブルの統計情報も再帰的に収集して更新します。
これを無効にするには、ONLYキーワードを使用します。
For partitioned tables, <command>ANALYZE</command> gathers statistics by
sampling rows from all partitions. By default,
<command>ANALYZE</command> will also recursively collect and update the
statistics for each partition. The <literal>ONLY</literal> keyword may be
used to disable this.
パーティションテーブルの場合、ANALYZEはすべてのパーティションの行をサンプリングして統計情報を収集します。
デフォルトでは、ANALYZEは各パーティションの統計情報も再帰的に収集して更新します。
これを無効にするには、ONLYキーワードを使用します。
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
<command>ANALYZE</command> 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 <command>ANALYZE</command>, those tables are
ignored while gathering inheritance statistics.
子テーブルやパーティションが、外部データラッパーがANALYZEをサポートしない外部テーブルであった場合には、そのテーブルは継承統計情報を集めるときに無視されます。
If the table being analyzed is completely empty, <command>ANALYZE</command>
will not record new statistics for that table. Any existing statistics
will be retained.
解析しようとするテーブルが完全に空である場合、ANALYZEはそのテーブルに対する新しい解析情報を記録しません。
これまでの統計情報はすべて保持されます。
Each backend running <command>ANALYZE</command> will report its progress
in the <structname>pg_stat_progress_analyze</structname> view. See
<xref linkend="analyze-progress-reporting"/> for details.
ANALYZEを実行している各バックエンドはその進捗をpg_stat_progress_analyzeビューで報告します。
詳細は27.4.1を参照してください。
There is no <command>ANALYZE</command> statement in the SQL standard.
標準SQLにはANALYZE文はありません。
The following syntax was used before <productname>PostgreSQL</productname> version 11 and is still supported: PostgreSQLバージョン11より前では次の構文が使われていましたが、今でもサポートされています。
ANALYZE [ VERBOSE ] [ table_and_columns [, ...] ]