forked from docs/doc-exports
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>
34 lines
9.3 KiB
HTML
34 lines
9.3 KiB
HTML
<a name="EN-US_TOPIC_0000001233883195"></a><a name="EN-US_TOPIC_0000001233883195"></a>
|
|
|
|
<h1 class="topictitle1">Routinely Recreating an Index</h1>
|
|
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_sa41d40b5410f428db79d599302b472d2"><h4 class="sectiontitle">Context</h4><p id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a18fcea0899f04be2901c901742ff90a2">When data deletion is repeatedly performed in the database, index keys will be deleted from the index page, resulting in index distention. Recreating an index routinely improves query efficiency.</p>
|
|
<p id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_af43c59ec6e31453f9bc37d9030f97a1c">The database supports B-tree, GIN, and psort indexes.</p>
|
|
<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_u9475839c5480412086d37be593eec516"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_l5abdacab132f4f239f9f525d21bde147">Recreating a B-tree index helps improve query efficiency.<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ucc95c122c9d641b2838517f2974fac71"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_l2c1f7c94372e4428b9141df6fd685640">If massive data is deleted, index keys on the index page will be deleted. As a result, the number of index pages reduces and index bloat occurs. Recreating an index helps reclaim wasted space.</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_lf98fd50193a548319966a813d6ac02fe">In the created index, pages adjacent in its logical structure are adjacent in its physical structure. Therefore, a created index achieves higher access speed than an index that has been updated for multiple times.</li></ul>
|
|
</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_lb8ee2e0bbb74478f82b4bc2b53cfca44">You are advised not to recreate a non-B-tree index.</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_section1483310439110"><h4 class="sectiontitle">Rebuilding an Index</h4><p id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_p16712110218">Use either of the following two methods to recreate an index:</p>
|
|
<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ul18015242214"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li121142913216">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b8423527060519">DROP INDEX</strong> statement to delete an index and run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b8423527060522">CREATE INDEX</strong> statement to create an index.<p id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_p11224299210">When you delete an index, a temporary exclusive lock is added in the parent table to block related read/write operations. When you create an index, the write operation is locked but the read operation is not. The data is read and scanned by order.</p>
|
|
</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li1686516531521">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b8423527060729">REINDEX</strong> statement to recreate an index:<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ul19134164510314"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li14187701549">When you run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b842352706113955">REINDEX TABLE</strong> statement to recreate an index, an exclusive lock is added to block related read/write operations.</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li13253124310315">When you run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b842352706114411">REINDEX INTERNAL TABLE</strong> statement to recreate an index for a <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b842352706114441">desc</strong> table (), an exclusive lock is added to block read/write operations on the table.</li></ul>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_s9829b9578b1c4f188f5dc44d1ecfb174"><h4 class="sectiontitle">Procedure</h4><div class="p" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_abc53c82a15f245169fd3d2e206c6d712">Assume the ordinary index areaS_idx exists in the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b842352706183910">area_id</strong> column of the imported table <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b842352706114125">areaS</strong>. Use either of the following two methods to recreate an index:<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ub9c7dd39e2014f34aa493f2f9756603e"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_l7ff2c2c0f46e43fa908d6e808e842e6b">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b1729148538">DROP INDEX</strong> statement to delete the index and run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b1971208381">CREATE INDEX</strong> statement to create an index.<ol id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ob10661b72f6140cfb0e58ac02aa7a4d7"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_le3cf4fcf1aa741e998b89cb2a2896857">Delete an index.<pre class="screen" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_sefefb8492b0f4e50a1db2047e2677f11"><span id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_text1986344834517"></span><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a6258097ec3b04f9698ce3145a8fdb6d5">DROP INDEX</strong> <em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a8b11bcd846804a5392be3c9e5222ecb8">areaS_idx</em><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a6c4fa978a91f441eab8d95ee77196457">;</strong>
|
|
DROP INDEX</pre>
|
|
</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_l5b05a7097f39444897c2a8d2cd0fc35a">Create an index.<pre class="screen" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_sde1191baf9e34869a629605d1adb0ec2"><span id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_text12634124912451"></span><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_afc32bc92b9be41f88317daa542eaee69">CREATE INDEX</strong> <em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a639a718a297445fb89aedf84a8e7f59b">areaS_idx </em><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a27b4b314645b4a02a1b6c3d26c57f947">ON </strong><em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ae3b84b2caee34c5fb44c2d2280a1ac27">areaS</em> <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ad7b72c751e814e28bbfa4b6eeb85b4c0">(</strong><em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ad8454b16803c445fb147d105da9b6dee">area_id</em><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_abf0810b17a594d8bb61c69817bbe9b0e">);</strong>
|
|
CREATE INDEX</pre>
|
|
</li></ol>
|
|
</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_l6c32fb4a8ca64aed8f2b97720a3f5260">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b2142315699">REINDEX</strong> statement to recreate an index.<ul id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_ul182241810163212"><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li49571217325">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b241884215418">REINDEX TABLE</strong> statement to recreate an index.<pre class="screen" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_screen5258637152320"><span id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_text19706165011452"></span><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b227563716234">REINDEX TABLE</strong> <em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_i527514379231">areaS</em><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b162751237102315">;</strong>
|
|
REINDEX</pre>
|
|
</li><li id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_li15534176203213">Run the <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b1602762347115124">REINDEX INTERNAL TABLE</strong> statement to recreate an index for a <strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_b818068816115124">desc</strong> table ().<pre class="screen" id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_screen88351159142919"><span id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_text15451125174517"></span><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a60838a1da36a410ebb1161610f8f9f77">REINDEX INTERNAL TABLE</strong> <em id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_aad63c17b73004571bc11f821da2f011f">areaS</em><strong id="EN-US_TOPIC_0000001233883195__en-us_topic_0000001145905453_a419f37eb25634fe5b7546aba62050486">;</strong>
|
|
REINDEX</pre>
|
|
</li></ul>
|
|
</li></ul>
|
|
</div>
|
|
</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>
|
|
|