doc-exports/docs/dws/dev/dws_04_0038.html
Lu, Huayi ef0ada5a59 DWS DEV 20240716 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-11-02 09:07:47 +00:00

116 lines
31 KiB
HTML

<a name="EN-US_TOPIC_0000001460722648"></a><a name="EN-US_TOPIC_0000001460722648"></a>
<h1 class="topictitle1">Creating and Managing Indexes</h1>
<div id="body8662426"><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p238961455517">Indexes accelerate the data access speed but also add the processing time of the insert, update, and delete operations. Therefore, before creating an index, consider whether it is necessary and determine the columns where indexes will be created. You can determine whether to add an index for a table by analyzing the service processing and data use of applications, as well as columns that are frequently used as search criteria or need to be sorted.</p>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sbb02960e207848d980c8e0b17efa7074"><h4 class="sectiontitle">Index type</h4><ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ul2994380316231"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li4067585616318"><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b133852621641840">btree</strong>: The B-tree index uses a structure that is similar to the B+ tree structure to store data key values, facilitating index search. <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b185323258841840">btree</strong> supports comparison queries with ranges specified.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li1033356216332"><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b94208032141840">gin</strong>: GIN indexes are reverse indexes and can process values that contain multiple keys (for example, arrays).</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li1587951012811"><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b389357841840">gist</strong>: GiST indexes are suitable for the set data type and multidimensional data types, such as geometric and geographic data types.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li575600616353"><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b66012409741840">Psort</strong>: psort index. It is used to perform partial sort on column-store tables.</li></ul>
<p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p879698416446">Row-based tables support the following index types: <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b34313837441840">btree</strong> (default), <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b7260385841840">gin</strong>, and <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b50818418941840">gist</strong>. Column-based tables support the following index types: <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b93638691941840">Psort</strong> (default), <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b199437232841840">btree</strong>, and <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b113738551241840">gin</strong>.</p>
<div class="note" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_note559718204141"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p1597112016146">Create a B-tree index for point queries.</p>
</div></div>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_s60f0d0d9b17d471987e2f0dfcbbe63ed"><h4 class="sectiontitle">Index Selection Principles</h4><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p112261316555">Indexes are created based on columns in database tables. When creating indexes, you need to determine the columns, which can be:</p>
<ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ul0226910559"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li1322619111559">Columns that are frequently searched: The search efficiency can be improved.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li16226181205512">The uniqueness of the columns and the data sequence structures is ensured.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li1722619165518">Columns that usually function as foreign keys and are used for connections. Then the connection efficiency is improved.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li1822613119554">Columns that are usually searched for by a specified scope. These indexes have already been arranged in a sequence, and the specified scope is contiguous.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li22268110558">Columns that need to be arranged in a sequence. These indexes have already been arranged in a sequence, so the sequence query time is accelerated.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li18227211553">Columns that usually use the WHERE clause. Then the condition decision efficiency is increased.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li15227110556">Fields that are frequently used after keywords, such as <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b92653618941840">ORDER BY</strong>, <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b181843586141840">GROUP BY</strong>, and <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b204160907041840">DISTINCT</strong>.<div class="note" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_note142271414553"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ul422710118556"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li02276120559">After an index is created, the system automatically determines when to reference it. If the system determines that indexing is faster than sequenced scanning, the index will be used.</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li322715117554">After an index is successfully created, it must be synchronized with the associated table to ensure new data can be accurately located. Therefore, data operations increase. Therefore, delete unnecessary indexes periodically.</li></ul>
</div></div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_section196518269299"><h4 class="sectiontitle">Creating an Index</h4><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_aab487ec4ae044767b519c45f5d193731"><span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text411673565">GaussDB(DWS)</span> supports four methods for creating indexes. For details, see <a href="#EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_tee4c2092e720429fb74de11b2aab4a23">Table 1</a>.</p>
<div class="note" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_nc68e07a0765047b5b4527710296c3836"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ub3d37e7100eb4e3cbdba9f8f54dc0a6d"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_le47222e16fe3406480e78dae9f4ac64d">After an index is created, the system automatically determines when to reference it. If the system determines that indexing is faster than sequenced scanning, the index will be used.</li></ul>
<ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_u99ac4e18dcd945429fc0d4f703eb598a"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_lf97fed763be24a5baa625f4639a12f49">After an index is successfully created, it must be synchronized with the associated table to ensure new data can be accurately located. Therefore, data operations increase. Therefore, delete unnecessary indexes periodically.</li></ul>
</div></div>
<div class="tablenoborder"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_tee4c2092e720429fb74de11b2aab4a23"></a><a name="en-us_topic_0000001233563161_tee4c2092e720429fb74de11b2aab4a23"></a><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_tee4c2092e720429fb74de11b2aab4a23" frame="border" border="1" rules="all"><caption><b>Table 1 </b><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b4821862241840">Indexing Method</strong></caption><thead align="left"><tr id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_rc1eb0c0004044497ade7024ca535cf3c"><th align="left" class="cellrowborder" valign="top" width="19.52%" id="mcps1.3.4.4.2.3.1.1"><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a89b07a03063047de9f0898a36241aea2"><strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b39421272941840">Indexing Method</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="80.47999999999999%" id="mcps1.3.4.4.2.3.1.2"><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_aefc6b507df4f4a94bc1f7b262f29b7df">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_re005df794ce04f1281ab204afcc77dc7"><td class="cellrowborder" valign="top" width="19.52%" headers="mcps1.3.4.4.2.3.1.1 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a70a1881e6652435683ce459076459102">Unique index</p>
</td>
<td class="cellrowborder" valign="top" width="80.47999999999999%" headers="mcps1.3.4.4.2.3.1.2 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a9044e88b433e4787829ca1877bc807ad">Refers to an index that constrains the uniqueness of an index attribute or an attribute group. If a table declares unique constraints or primary keys, <span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text1692526268">GaussDB(DWS)</span> automatically creates unique indexes (or composite indexes) for columns that form the primary keys or unique constraints. Currently, only B-tree can create a unique index in <span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text1162506775">GaussDB(DWS)</span>.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_r7771da9e34aa4390a26e300fb5247173"><td class="cellrowborder" valign="top" width="19.52%" headers="mcps1.3.4.4.2.3.1.1 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_af348ac7162de4169951818674f8a5106">Composite index</p>
</td>
<td class="cellrowborder" valign="top" width="80.47999999999999%" headers="mcps1.3.4.4.2.3.1.2 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_aefd937a3f4234e9fa33efbfb6ae5eb04">Refers to an index that can be defined for multiple attributes of a table. Currently, composite indexes can be created only for B-tree in <span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text489033848">GaussDB(DWS)</span> and a maximum of 32 columns can share a composite index.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_r2b56bc36d9774a27b1041a438f167f43"><td class="cellrowborder" valign="top" width="19.52%" headers="mcps1.3.4.4.2.3.1.1 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a5d026e8eb3904e0bbcbd7d683684c212">Partial index</p>
</td>
<td class="cellrowborder" valign="top" width="80.47999999999999%" headers="mcps1.3.4.4.2.3.1.2 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a3112f78734be4bc582aa2a1bc6c6cbe4">Refers to an index that can be created for subsets of a table. This indexing method contains only tuples that meet condition expressions.</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_r0425a64347344575baf2eba3950208a8"><td class="cellrowborder" valign="top" width="19.52%" headers="mcps1.3.4.4.2.3.1.1 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a31da4a0679c347f4b114235af34ad974">Expression index</p>
</td>
<td class="cellrowborder" valign="top" width="80.47999999999999%" headers="mcps1.3.4.4.2.3.1.2 "><p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a250ac198cd994590b376728875ed886c">Refers to an index that is built on a function or an expression calculated based on one or more attributes of a table. An expression index works only when the queried expression is the same as the created expression.</p>
</td>
</tr>
</tbody>
</table>
</div>
<ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_u416f0bfddab44747910c5b0251ff1602"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_l8ff1f10b49364616907c89784d0dd2f1">Run the following command to create an ordinary table:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sd410b374ef064e8e816dd0a9bd8de432"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
<ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_uc4082b52a87d48b2be4e951bcaddd461"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_lfd8cace2c5324b569cfc46d132ff3792">Create a common index.<div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ab6e8c5214c644f6c8e2c461765d4a433"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_lfd8cace2c5324b569cfc46d132ff3792"></a><a name="en-us_topic_0000001233563161_lfd8cace2c5324b569cfc46d132ff3792"></a>You need to query the following information in the <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706163658"><span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text83191627186">tpcds.</span>customer_address_bak</strong> table:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sebbfb040b0714b7986f6cd809cdeec42"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">ca_address_sk</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">ca_address_sk</span><span class="o">=</span><span class="mi">14888</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ad412d1574b914451895a72250cbf1b84">Generally, the database system needs to scan the <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b84235270615484"><span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text1185610141818">tpcds.</span>customer_address_bak</strong> table row by row to find all matched tuples. If the size of the <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706154920"><span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text37292115182">tpcds.</span>customer_address_bak</strong> table is large but only a few (possibly zero or one) of the WHERE conditions are met, the performance of this sequential scan is low. If the database system uses an index to maintain the ca_address_sk attribute, the database system only needs to search a few tree layers for the matched tuples. This greatly improves data query performance. Furthermore, indexes can improve the update and delete operation performance in the database.</p>
<p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ab454ca8d814e4691b7c82e4fa45a12b6">Run the following command to create an index:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_s7f822a26a3814e8daa59323b075239c0"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">index_wr_returned_date_sk</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="p">(</span><span class="n">ca_address_sk</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li15827633203713">Create a unique index.<p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p1312243173712"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li15827633203713"></a><a name="en-us_topic_0000001233563161_li15827633203713"></a>If a table declares a unique constraint or primary key, <span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text3875558124217">GaussDB(DWS)</span> automatically creates a unique index (possibly a multi-column index) on the columns that form the primary key or unique constraint. If no unique constraint or primary key is specified during table creation, you can run the CREATE INDEX statement to create an index.</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen61391221134919"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">UNIQUE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">unique_index</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="p">(</span><span class="n">ca_address_sk</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_lb966e2f0ff9148839bb07e96922b1da6">Create a multi-column index.<div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ab628cdf4362c4b5196846b2eb618b956"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_lb966e2f0ff9148839bb07e96922b1da6"></a><a name="en-us_topic_0000001233563161_lb966e2f0ff9148839bb07e96922b1da6"></a>Assume you need to frequently query records with <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706115246">ca_address_sk</strong> being <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706115253">5050</strong> and <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b84235270611533">ca_street_number</strong> smaller than <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706115312">1000</strong> in the <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706115326"><span id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_text179341132187">tpcds.</span>customer_address_bak</strong> table. Run the following command:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sc82a02ffbd234f2aa611cfabbf06c8a8"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">ca_address_sk</span><span class="p">,</span><span class="n">ca_address_id</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">ca_address_sk</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5050</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="n">ca_street_number</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">1000</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_en-us_topic_0059777978_p842884415237">Run the following command to define a multiple-column index on <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706154358">ca_address_sk</strong> and <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b84235270615441">ca_street_number</strong> columns:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_s207222959d014024bd549ecbe983b07a"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">more_column_index</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="p">(</span><span class="n">ca_address_sk</span><span class="w"> </span><span class="p">,</span><span class="n">ca_street_number</span><span class="w"> </span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_l5d1812afb50b43379634adeb791cff25">Create a partition index.<p id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_abc3d91f3aacb4486bb04522741208eff"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_l5d1812afb50b43379634adeb791cff25"></a><a name="en-us_topic_0000001233563161_l5d1812afb50b43379634adeb791cff25"></a>If you only want to find records whose <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b84235270611581">ca_address_sk</strong> is <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b84235270611586">5050</strong>, you can create a partial index to facilitate your query.</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sdfc7a00d266f45809bcbd43dc27e7c62"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">part_index</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="p">(</span><span class="n">ca_address_sk</span><span class="p">)</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">ca_address_sk</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5050</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_l395b04253f7a4ca7b506c7f4edea7081">Create an expression index.<div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_aa8f84414cac0480c88a6dc2356c36c1e"><a name="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_l395b04253f7a4ca7b506c7f4edea7081"></a><a name="en-us_topic_0000001233563161_l395b04253f7a4ca7b506c7f4edea7081"></a>Assume you need to frequently query records with <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706192016">ca_street_number</strong> smaller than <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b842352706192035">1000</strong>, run the following command:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_s007f1c7eb08046bc987e7877ffcafd82"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">trunc</span><span class="p">(</span><span class="n">ca_street_number</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">1000</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_a5379148fced24a9f99cf61e43a88dfd0">The following expression index can be created for this query task:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_sc6cd4c8f81bb4206ba70e9929c1f5221"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">para_index</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="w"> </span><span class="p">(</span><span class="n">trunc</span><span class="p">(</span><span class="n">ca_street_number</span><span class="p">));</span>
</pre></div></td></tr></table></div>
</div>
</div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_section77081421585"><h4 class="sectiontitle">Querying an Index</h4><ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ul6448134162912"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li14487419294">Run the following command to query all indexes defined by the system and users:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen62190125110"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">RELNAME</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">PG_CLASS</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">RELKIND</span><span class="o">=</span><span class="s1">'i'</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li8448194110296">Run the following command to query information about a specified index:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen202180205119"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="err">\</span><span class="n">di</span><span class="o">+</span><span class="w"> </span><span class="n">index_wr_returned_date_sk</span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_section9692231184815"><h4 class="sectiontitle">Recreating an Index</h4><ul id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_ul2240847102918"><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li324012477291">Recreate the index <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b1186171216296">index_wr_returned_date_sk</strong>.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen1711712346593"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">REINDEX</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">index_wr_returned_date_sk</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_li32401347202910">Recreate all indexes of a table.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen1858101718594"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">REINDEX</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">tpcds</span><span class="p">.</span><span class="n">customer_address_bak</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_section17642123210416"><h4 class="sectiontitle">Deleting an Index</h4><div class="p" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_p089317919420">You can use the <strong id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_b77123542241840">DROP INDEX</strong> statement to delete indexes.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722648__en-us_topic_0000001233563161_screen8423208114218"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">DROP</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="n">index_wr_returned_date_sk</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0016.html">Defining Database Objects</a></div>
</div>
</div>