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>
41 lines
5.9 KiB
HTML
41 lines
5.9 KiB
HTML
<a name="EN-US_TOPIC_0000001188323616"></a><a name="EN-US_TOPIC_0000001188323616"></a>
|
|
|
|
<h1 class="topictitle1">Case: Reconstructing Partition Tables</h1>
|
|
<div id="body8662426"><p id="EN-US_TOPIC_0000001188323616__p494493931010">Partitioning refers to splitting what is logically one large table into smaller physical pieces based on specific schemes. The table based on the logic is called a partitioned table, and a physical piece is called a partition. Generally, partitioning is applied to tables that have obvious ranges. Partitions on such tables allow scanning on a small part of data, improving the query performance.</p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p13551511610">During query, partition pruning is used to minimize bottom-layer data scanning to narrow down the overall scope of scanning in a table. Partition pruning means that the optimizer can automatically extract partitions to be scanned based on the partition key specified in the <strong id="EN-US_TOPIC_0000001188323616__b18493192591119">FROM</strong> and <strong id="EN-US_TOPIC_0000001188323616__b82227282117">WHERE</strong> statements. This avoids full table scanning, reduces the number of data blocks to be scanned, and improves performance.</p>
|
|
<div class="section" id="EN-US_TOPIC_0000001188323616__sfaa9f7d929b74548870686b901e49068"><h4 class="sectiontitle">Before Optimization</h4><p id="EN-US_TOPIC_0000001188323616__p522395932114">Create a non-partition table <strong id="EN-US_TOPIC_0000001188323616__b071814531218">orders_no_part</strong>. The table definition is as follows:</p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p145612012242"><span><img id="EN-US_TOPIC_0000001188323616__image754811229506" src="figure/en-us_image_0000001601785621.png"></span></p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p205765200811">Run the following SQL statement to query the execution plan of the non-partition table:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323616__screen109551840886"><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></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="k">SELECT</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">orders_no_part</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span>
|
|
<span class="n">o_orderdate</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="s1">'1996-01-01 00:00:00'</span><span class="p">::</span><span class="k">timestamp</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188323616__p3590651181">As shown in the following figure, the execution time is 73 milliseconds, and the full table scanning time is 44 to 45 milliseconds.</p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p198391149192111"><span><img id="EN-US_TOPIC_0000001188323616__image1987634117517" src="figure/en-us_image_0000001550427578.png"></span></p>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188323616__s71ffd8e8cc0542d180e9be037cf86e23"><h4 class="sectiontitle">After Optimization</h4><p id="EN-US_TOPIC_0000001188323616__p176281242152911">Create a partitioned table <strong id="EN-US_TOPIC_0000001188323616__b38311324101218">orders</strong>. The table is defined as follows:</p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p118551312114011"><span><img id="EN-US_TOPIC_0000001188323616__image8950115312548" src="figure/en-us_image_0000001601788033.png"></span></p>
|
|
<p id="EN-US_TOPIC_0000001188323616__a492b4be4f7674246b35ba05392ad0400">Run the SQL statement again to query the execution plan of the partitioned table. The execution time is 40 ms, in which the table scanning time is only 13 ms. The smaller the value of <strong id="EN-US_TOPIC_0000001188323616__b2090073681610">Iterations</strong>, the better the partition pruning effect.</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188323616__screen1114162013442"><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></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="k">SELECT</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">orders_no_part</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span>
|
|
<span class="n">o_orderdate</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="s1">'1996-01-01 00:00:00'</span><span class="p">::</span><span class="k">timestamp</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188323616__p10468020151714">As shown in the following figure, the execution time is 40 milliseconds, and the table scanning time is only 13 milliseconds. A smaller <strong id="EN-US_TOPIC_0000001188323616__b11550115651610">Iterations</strong> value indicates a better partition pruning effect.</p>
|
|
<p id="EN-US_TOPIC_0000001188323616__p285811272203"><span><img id="EN-US_TOPIC_0000001188323616__image1233964912555" src="figure/en-us_image_0000001550748802.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>
|
|
|