forked from docs/doc-exports
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
45 lines
7.6 KiB
HTML
45 lines
7.6 KiB
HTML
<a name="EN-US_TOPIC_0000001098974988"></a><a name="EN-US_TOPIC_0000001098974988"></a>
|
|
|
|
<h1 class="topictitle1">Updating Statistics</h1>
|
|
<div id="body8662426"><p id="EN-US_TOPIC_0000001098974988__p78113337546">In a database, statistics indicate the source data of a plan generated by a planner. If no collection statistics are available or out of date, the execution plan may seriously deteriorate, leading to low performance.</p>
|
|
<div class="section" id="EN-US_TOPIC_0000001098974988__s3e9d960cc5b64d838fb0c5e268872abf"><h4 class="sectiontitle">Context</h4><p id="EN-US_TOPIC_0000001098974988__p9599144717409">The <strong id="EN-US_TOPIC_0000001098974988__b84235270611236">ANALYZE</strong> statement collects statistic about table contents in databases, which will be stored in the system table <strong id="EN-US_TOPIC_0000001098974988__b66748299">PG_STATISTIC</strong>. Then, the query optimizer uses the statistics to work out the most efficient execution plan.</p>
|
|
<p id="EN-US_TOPIC_0000001098974988__p41451454144012">After executing batch insertion and deletions, you are advised to run the <strong id="EN-US_TOPIC_0000001098974988__b842352706165842">ANALYZE</strong> statement on the table or the entire library to update statistics. By default, 30,000 rows of statistics are sampled. That is, the default value of the GUC parameter <strong id="EN-US_TOPIC_0000001098974988__b842352706174552">default_statistics_target</strong> is <strong id="EN-US_TOPIC_0000001098974988__b842352706174556">100</strong>. If the total number of rows in the table exceeds 1,600,000, you are advised to set <strong id="EN-US_TOPIC_0000001098974988__b842352706174638">default_statistics_target</strong> to <strong id="EN-US_TOPIC_0000001098974988__b842352706174645">-2</strong>, indicating that 2% of the statistics are collected.</p>
|
|
<p id="EN-US_TOPIC_0000001098974988__aea6d99cadd0c43d9a204a39d42556b1d">For an intermediate table generated during the execution of a batch script or stored procedure, you also need to run the <strong id="EN-US_TOPIC_0000001098974988__b59965843211354">ANALYZE</strong> statement.</p>
|
|
<p id="EN-US_TOPIC_0000001098974988__p83021639060">If there are multiple inter-related columns in a table and the conditions or grouping operations based on these columns are involved in the query, collect statistics about these columns so that the query optimizer can accurately estimate the number of rows and generate an effective execution plan.</p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001098974988__s7ffb94e0143e40dcaa62026359ad5efd"><h4 class="sectiontitle">Procedure</h4><p id="EN-US_TOPIC_0000001098974988__a4d7688e421944535bc39b0f8cdb9647c">Run the following commands to update the statistics about a table or the entire database:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098974988__s1e99d92eeaf646c584d59b62cd26d3a6"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ANALYZE</span><span class="w"> </span><span class="n">tablename</span><span class="p">;</span><span class="w"> </span><span class="c1">--Update statistics about a table.</span>
|
|
<span class="k">ANALYZE</span><span class="p">;</span><span class="w"> </span><span class="c1">---Update statistics about the entire database.</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001098974988__p93142485261">Run the following statements to perform statistics-related operations on multiple columns:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098974988__screen2033246152712"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span>
|
|
<span class="normal">6</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ANALYZE</span><span class="w"> </span><span class="n">tablename</span><span class="w"> </span><span class="p">((</span><span class="n">column_1</span><span class="p">,</span><span class="w"> </span><span class="n">column_2</span><span class="p">));</span><span class="w"> </span><span class="c1">--Collect statistics about column_1 and column_2 of tablename.</span>
|
|
|
|
<span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tablename</span><span class="w"> </span><span class="k">ADD</span><span class="w"> </span><span class="k">STATISTICS</span><span class="w"> </span><span class="p">((</span><span class="n">column_1</span><span class="p">,</span><span class="w"> </span><span class="n">column_2</span><span class="p">));</span><span class="w"> </span><span class="c1">--Declare statistics about column_1 and column_2 of tablename.</span>
|
|
<span class="k">ANALYZE</span><span class="w"> </span><span class="n">tablename</span><span class="p">;</span><span class="w"> </span><span class="c1">--Collect statistics about one or more columns.</span>
|
|
|
|
<span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tablename</span><span class="w"> </span><span class="k">DELETE</span><span class="w"> </span><span class="k">STATISTICS</span><span class="w"> </span><span class="p">((</span><span class="n">column_1</span><span class="p">,</span><span class="w"> </span><span class="n">column_2</span><span class="p">));</span><span class="w"> </span><span class="c1">--Delete statistics about column_1 and column_2 of tablename or their statistics declaration.</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<div class="notice" id="EN-US_TOPIC_0000001098974988__note121972410486"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001098974988__p1721912414488">After the statistics are declared for multiple columns by running the <strong id="EN-US_TOPIC_0000001098974988__b16349578249">ALTER TABLE</strong> <em id="EN-US_TOPIC_0000001098974988__i189157082518">tablename</em> <strong id="EN-US_TOPIC_0000001098974988__b149010518250">ADD STATISTICS</strong> statement, the system collects the statistics about these columns next time <strong id="EN-US_TOPIC_0000001098974988__b76462160273">ANALYZE</strong> is performed on the table or the entire database.</p>
|
|
<p id="EN-US_TOPIC_0000001098974988__p165891028115916">To collect the statistics, run the <strong id="EN-US_TOPIC_0000001098974988__b15620312288">ANALYZE</strong> statement.</p>
|
|
</div></div>
|
|
<div class="note" id="EN-US_TOPIC_0000001098974988__n344429db385046788a06702e8a8e981a"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001098974988__a6b3051fc67274e60833bf578622098ab">Use <strong id="EN-US_TOPIC_0000001098974988__b842352706154058">EXPLAIN</strong> to show the execution plan of each SQL statement. If <strong id="EN-US_TOPIC_0000001098974988__b842352706154428">rows=10</strong> (the default value, probably indicating the table has not been analyzed) is displayed in the <strong id="EN-US_TOPIC_0000001098974988__b84235270615448">SEQ SCAN</strong> output of a table, run the <strong id="EN-US_TOPIC_0000001098974988__b842352706154516">ANALYZE</strong> statement for this table.</p>
|
|
</div></div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0430.html">Query Improvement</a></div>
|
|
</div>
|
|
</div>
|
|
|