doc-exports/docs/dws/dev/dws_04_0466.html
Lu, Huayi e6fa411af0 DWS DEV 830.201 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2024-05-16 07:24:04 +00:00

49 lines
17 KiB
HTML

<a name="EN-US_TOPIC_0000001188163698"></a><a name="EN-US_TOPIC_0000001188163698"></a>
<h1 class="topictitle1">Routinely Maintaining Tables</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_acdca08c4da3c4c6cb8366ddc98614e10">To ensure proper database running, after INSERT and DELETE operations, you need to routinely do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b842352706175739">VACUUM FULL</strong> and <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b842352706175730">ANALYZE</strong> as appropriate for customer scenarios and update statistics to obtain better performance.</p>
<div class="section" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_s732ce2f9879f4175afc07e8da9739d9d"><h4 class="sectiontitle">Related Concepts</h4><p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a1e60e18d23864cdba58179c21c36e29d">You need to routinely run <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706232818">VACUUM</strong>, <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b84235270618031">VACUUM FULL</strong>, and <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706232822">ANALYZE</strong> to maintain tables, because:</p>
<ul id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_u5e922633a5284f8e833fc14f531167c0"><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l6d89ef79d74c4b429b44fc78c16cdee2"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706235625">VACUUM FULL</strong> reclaims disk space occupied by updated or deleted data and combines small-size data files.</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_ldf4950dd1b4b4bdc8bd388d48a40d0a2"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706233051">VACUUM</strong> maintains a visualized mapping to track pages that contain arrays visible to other active transactions. A common index scan uses the mapping to obtain the corresponding array and check whether pages are visible to the current transaction. If the array cannot be obtained, the visibility is checked by fetching stack arrays. Therefore, updating the visible mapping of a table can accelerate unique index scans.</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l79eef3ef4e404fdd905c61e0c66bd2f7"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b8782151032919">VACUUM</strong> can avoid old data loss caused by duplicate transaction IDs when the number of executed transactions exceeds the database threshold.</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_lc83f31fbf2ce43b5be1f8c812c3320ba"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b84235270623398">ANALYZE</strong> collects statistics on tables in databases. The statistics are stored in the PG_STATISTIC system catalog. Then, the query optimizer uses the statistics to work out the most efficient execution plan.</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_s04cbc946ce5747c7bb6143f0eba2329e"><h4 class="sectiontitle">Procedure</h4><ol id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_o5adf6adc69964f9bae656a1bd67f2165"><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l83b3bb16043b4e2aa3ff786374342ec6"><span>Run the <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b20802114101410">VACUUM</strong> or <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b1980291421415">VACUUM FULL</strong> command to reclaim disk space.</span><p><ul id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_u4aad06d6d6304dca87f130bfe7bf9b30"><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_le53c628431a1465b80ec5d93515fb98a"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706103036">VACUUM</strong>:<div class="p" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_aaefc545f012040999d6e38a17f487b3b">Do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b84235270615196">VACUUM</strong> to the table:<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_sc85cc543cb944e9395190d56a4fb8220"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_teb0c8ce655d84d9e811b9a8785e0d309"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b985972819581">VACUUM</strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_af5807f4226b149c1a96f047e8a106ca8"> customer</em><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a0b6504e055484d7285f3acf0b15dade8">;</strong></pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen14582131174215">VACUUM</pre>
</div>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a88c948c7122344608519f2d8b65d6543">This command can be concurrently executed with database operation commands, including <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b3907913173019">SELECT</strong>, <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b16475151306">INSERT</strong>, <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b015310521674">UPDATE</strong>, and <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b18313141716308">DELETE</strong>; excluding <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b384413373309">ALTER TABLE</strong>.</p>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a867af852413b48789b2090816b1aee06">Do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706151859">VACUUM</strong> to the partitioned table:</p>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_scd619a818dc2456a9413dd7beb3f9e1a"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_td85288a7f6974220a843296f5767401e"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a95090d1651bd4e16bb57e43ba9af3a13">VACUUM </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a3c1df250e80b4f9f945baa1cd4b75512">customer_par</em> <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a3622429bb8654eafb67ab62e647594d6">PARTITION </strong>( <em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a65140177294c435fbe195df36fdfdabb">P1 </em>);</pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen14206131711426">VACUUM</pre>
</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l6f5d5d3f6c904e23a230929ab369e89f">VACUUM FULL:<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_se24fe821980240ad945cb1779f9f63ea"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_t687772d581e647ab9126b6f233ce30fb"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_aa3bc91b54dc14f55a6f1fe0b312fd3ad">VACUUM FULL</strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a85cd57a1a1db430da8b643d038104610"> <em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a4ef8efa37e284458bfa97af56d9d7d3b">customer</em></em><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_aa7c37954c84b4045b465294a5743fcc0">;</strong></pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen1012252815432">VACUUM</pre>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a58fcf4c5214b4fd7a33f5c832e104f26"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b1740746625234512">VACUUM FULL</strong> needs to add exclusive locks on tables it operates on and requires that all other database operations be suspended.</p>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_p181224443818">When reclaiming disk space, you can query for the session corresponding to the earliest transactions in the cluster, and then end the earliest long transactions as needed to make full use of the disk space.</p>
<ol type="a" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_ol1524317527337"><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_li12244145283313">Run the following command to query for oldestxmin on the GTM:<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen93755157267">select * from pgxc_gtm_snapshot_status();</pre>
</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_li192861456113318">Run the following command to query for the PID of the corresponding session on the CN. <em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_i1731194418124">xmin</em> is the oldestxmin obtained in the previous step.<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen13357173372618">select * from pgxc_running_xacts() where xmin=1400202010;</pre>
</li></ol>
</li></ul>
</p></li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l0517aa71dd254704baeac0545dafff99"><span>Do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b8423527060017">ANALYZE</strong> to update statistical information.</span><p><pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_se51793b5e2824f0092db75b702e67306"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_t453ed79cc3fd428681e8783eb86536d2"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a6685f2ccecb74c8084a63e8a02ba36bb">ANALYZE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_ad07f407a5f6e410c8778150331122fc3">customer</em><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a46d6abbee76b4390a5d5f8ae78f57ea4">;</strong></pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen108711518194412">ANALYZE</pre>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_af317bd602a8941a7b9b2eae9bd933805">Do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706152015">ANALYZE VERBOSE</strong> to update statistics and display table information.</p>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_s207aaf572421446b86ae4c9cabd68d36"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_tb06b668b857d4fec972ed808983adbf6"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a4c76497dc8d44b67ae3ba745036b4cde">ANALYZE VERBOSE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a9766f829c9d946fa8a3b3dacece1aa41">customer</em><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_aadf9f7070c1849caa1161517bc882faa">;</strong></pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen1824752711445">ANALYZE</pre>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a5ef686c946f94dd5a54eb6350451fbbb">You can use <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706234623">VACUUM ANALYZE</strong> at the same time to optimize the query.</p>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_s41568ede43fd4574b4fc7993aba9d60e"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_te05c7ac40128444881eb63b8bedd5904"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a401d1ffb58a741d086074a09b7258d63">VACUUM ANALYZE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a360a6be9b3b34bffa0d75c6a76683561">customer</em><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a57b0b47420594786868d0727cb8e2267">;</strong></pre>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_screen1134313331447">VACUUM</pre>
<div class="note" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_ncd3e49af7ba34d4aad87a259e1eaba35"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a21d6f89a3e8442c6868a4a5f3ed94a40"><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b6661830315442">VACUUM</strong> and <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706233341">ANALYZE</strong> cause a substantial increase in I/O traffic, which may cause poor performance of other active sessions. Therefore, you are advised to set by specifying the <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b84235270623474">vacuum_cost_delay</strong> parameter.</p>
</div></div>
</p></li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_lbe1d349295d248ec953f8999cf0ef5ab"><span>Delete a table</span><p><pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_seb075b8bb1064f8e9d7c5056846ac71a"><span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_t48c958be640f49ec856d378cbb7cbc49"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a1d4d293cdf4b4d06b4d58081ab23e95d">DROP TABLE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a5a0d043db2e6446d9073cc3cc4c94ae7">customer</em>;
<span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_t55cd55d4fd9c49c691305ca7c52a0d63"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b32616311968">DROP TABLE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_i112693115610">customer_par</em>;
<span id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_tca94a0db438a40e7964feda5ef064bb4"></span><strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a2e5b1476b9b042be962825cd8894c89e">DROP TABLE </strong><em id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a587ffd122a2e45d481d21ef447b3b07d">part</em>;</pre>
<p id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_a94c3f61d76a146f3ba3e52e21df0ab66">If the following output is displayed, the index has been deleted.</p>
<pre class="screen" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_sddc58c1250754469a55f0162fb4ccf08">DROP TABLE</pre>
</p></li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_sae9ba1b13e4e48589cead0d4a2d227d8"><h4 class="sectiontitle">Maintenance Suggestion</h4><ul id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_u1bd72e00ad62444b848006d814e7589c"><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l55e28a12a3734cd6bb26693512ff1a3a">Routinely do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706234724">VACUUM FULL</strong> to large tables. If the database performance deteriorates, do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b1402323241234753">VACUUM FULL</strong> to the entire database. If the database performance is stable, you are advised to monthly do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706234825">VACUUM FULL</strong>.</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_le7e0cb97163e45448c4a09a1f8912c03">Routinely do <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b84235270623494">VACUUM FULL</strong> to system catalogs, mainly <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_en-us_topic_0111591987_b842352706234916">PG_ATTRIBUTE</strong>.</li><li id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_l752ed51456924220aa00bc1a533148d1">The automatic vacuum process (<strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b2603143953111">AUTOVACUUM</strong>) in the system automatically runs the <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b154013492314">VACUUM</strong> and <strong id="EN-US_TOPIC_0000001188163698__en-us_topic_0000001145505853_b3401251113115">ANALYZE</strong> statements to reclaim the record space marked as the deleted state and to update statistics related to the table.</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0430.html">SQL Optimization Guide</a></div>
</div>
</div>