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>
30 lines
5.3 KiB
HTML
30 lines
5.3 KiB
HTML
<a name="EN-US_TOPIC_0000001188642148"></a><a name="EN-US_TOPIC_0000001188642148"></a>
|
|
|
|
<h1 class="topictitle1">Case: Creating an Appropriate Index</h1>
|
|
<div id="body8662426"><p id="EN-US_TOPIC_0000001188642148__p11454447387">Creating a proper index can accelerate the retrieval of data rows in a table. Indexes occupy disk space and reduce the speed of adding, deleting, and updating rows. If data needs to be updated very frequently or disk space is limited, you need to limit the number of indexes. Create indexes for large tables. Because the more data in the table, the more effective the index is. You are advised to create indexes on:</p>
|
|
<ul id="EN-US_TOPIC_0000001188642148__ul829422494115"><li id="EN-US_TOPIC_0000001188642148__li17294172418418">Columns that need to be queried frequently</li><li id="EN-US_TOPIC_0000001188642148__li1829452418416">Joined columns. For a query on joined columns, you are advised to create a composite index on the joined columns. For example, if the join condition is <strong id="EN-US_TOPIC_0000001188642148__b62931313391">select * from t1 join t2 on t1.a=t2.a and t1.b=t2.b</strong>. You can create a composite index on the <strong id="EN-US_TOPIC_0000001188642148__b829343133912">a</strong> and <strong id="EN-US_TOPIC_0000001188642148__b529315314398">b</strong> columns of table <strong id="EN-US_TOPIC_0000001188642148__b42938373918">t1</strong>.</li><li id="EN-US_TOPIC_0000001188642148__li429452484113">Columns having filter criteria (especially scope criteria) of a <strong id="EN-US_TOPIC_0000001188642148__b9897193618396">where</strong> clause</li><li id="EN-US_TOPIC_0000001188642148__li14294824204118">Columns that appear after <strong id="EN-US_TOPIC_0000001188642148__b981144113916">order by</strong>, <strong id="EN-US_TOPIC_0000001188642148__b4813447390">group by</strong>, and <strong id="EN-US_TOPIC_0000001188642148__b18114416393">distinct</strong></li></ul>
|
|
<div class="section" id="EN-US_TOPIC_0000001188642148__s2062b77aceed41a3b1ae82bf76721251"><h4 class="sectiontitle">Before optimization</h4><p id="EN-US_TOPIC_0000001188642148__p168734252456">The column-store partitioned table <strong id="EN-US_TOPIC_0000001188642148__b187835718429">orders</strong> is defined as follows:</p>
|
|
<p id="EN-US_TOPIC_0000001188642148__p144416315575"><span><img id="EN-US_TOPIC_0000001188642148__image15968111074514" src="figure/en-us_image_0000001551160526.png"></span></p>
|
|
<p id="EN-US_TOPIC_0000001188642148__p10556926125619">Run the SQL statement to query the execution plan when no index is created. It is found that the execution time is 48 milliseconds.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188642148__screen1579418411148"><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">EXPLAIN</span><span class="w"> </span><span class="n">PERFORMANCE</span><span class="w"> </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">orders</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">o_custkey</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'1106459'</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188642148__p434014227455"><span><img id="EN-US_TOPIC_0000001188642148__image34641755125615" src="figure/en-us_image_0000001601476601.png"></span></p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188642148__s7593216994f141f0aba9d3da199ba4d7"><h4 class="sectiontitle">After optimization</h4><p id="EN-US_TOPIC_0000001188642148__p17509175618420">The filtering condition column of the <strong id="EN-US_TOPIC_0000001188642148__b085134518423">where</strong> clause is <strong id="EN-US_TOPIC_0000001188642148__b59531733154215">o_custkey</strong>. Add an index to the <strong id="EN-US_TOPIC_0000001188642148__b11253725174310">o_custkey</strong> column.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188642148__screen36734451688"><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">idx_o_custkey</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">orders</span><span class="w"> </span><span class="p">(</span><span class="n">o_custkey</span><span class="p">)</span><span class="w"> </span><span class="k">LOCAL</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188642148__p735412551980">Run the SQL statement to query the execution plan after the index is created. It is found that the execution time is 18 milliseconds.</p>
|
|
<p id="EN-US_TOPIC_0000001188642148__p760651431418"><span><img id="EN-US_TOPIC_0000001188642148__image14665019181417" src="figure/en-us_image_0000001602017897.png"></span></p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0474.html">Optimization Cases</a></div>
|
|
</div>
|
|
</div>
|
|
|