Files
doc-exports/docs/dws/dev/dws_04_0484.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

45 lines
7.1 KiB
HTML

<a name="EN-US_TOPIC_0000001629070645"></a><a name="EN-US_TOPIC_0000001629070645"></a>
<h1 class="topictitle1">Case: Reconstructing Partition Tables</h1>
<div id="body32001227"><p id="EN-US_TOPIC_0000001629070645__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_0000001629070645__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_0000001629070645__en-us_topic_0000001188323616_b18493192591119">FROM</strong> and <strong id="EN-US_TOPIC_0000001629070645__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_0000001629070645__en-us_topic_0000001188323616_sfaa9f7d929b74548870686b901e49068"><h4 class="sectiontitle">Before Optimization</h4><p id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_p522395932114">Create a non-partition table <strong id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_b071814531218">orders_no_part</strong>. The table definition is as follows:</p>
<p id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_p145612012242"><span><img id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_image754811229506" src="figure/en-us_image_0000001601785621.png" width="518.7" height="225.10250000000002" title="Click to enlarge" class="imgResize"></span></p>
<p id="EN-US_TOPIC_0000001629070645__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_0000001629070645__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">&gt;=</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_0000001629070645__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_0000001629070645__en-us_topic_0000001188323616_p198391149192111"><span><img id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_image1987634117517" src="figure/en-us_image_0000001550427578.png" width="518.7" height="77.27300000000001" title="Click to enlarge" class="imgResize"></span></p>
</div>
<div class="section" id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_s71ffd8e8cc0542d180e9be037cf86e23"><h4 class="sectiontitle">After Optimization</h4><p id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_p176281242152911">Create a partitioned table <strong id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_b38311324101218">orders</strong>. The table is defined as follows:</p>
<p id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_p118551312114011"><span><img id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_image8950115312548" src="figure/en-us_image_0000001601788033.png" width="518.7" height="218.0934" title="Click to enlarge" class="imgResize"></span></p>
<p id="EN-US_TOPIC_0000001629070645__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_0000001629070645__en-us_topic_0000001188323616_b2090073681610">Iterations</strong>, the better the partition pruning effect.</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001629070645__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">&gt;=</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_0000001629070645__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_0000001629070645__en-us_topic_0000001188323616_b11550115651610">Iterations</strong> value indicates a better partition pruning effect.</p>
<p id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_p285811272203"><span><img id="EN-US_TOPIC_0000001629070645__en-us_topic_0000001188323616_image1233964912555" src="figure/en-us_image_0000001550748802.png" width="518.7" height="117.3725" title="Click to enlarge" class="imgResize"></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>
<script language="JavaScript">
<!--
image_size('.imgResize');
var msg_imageMax = "view original image";
var msg_imageClose = "close";
//--></script>