doc-exports/docs/dws/dev/dws_04_0456.html
Lu, Huayi e6fa411af0 DWS DEV 830.201 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-05-16 07:24:04 +00:00

48 lines
9.4 KiB
HTML

<a name="EN-US_TOPIC_0000001188163600"></a><a name="EN-US_TOPIC_0000001188163600"></a>
<h1 class="topictitle1">Join Order Hints</h1>
<div id="body1534471018690"><div class="section" id="EN-US_TOPIC_0000001188163600__section97491741123412"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001188163600__p1899334819349">Theses hints specify the join order and outer/inner tables.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001188163600__section128191729143517"><h4 class="sectiontitle">Syntax</h4><ul id="EN-US_TOPIC_0000001188163600__ul188666143526"><li id="EN-US_TOPIC_0000001188163600__li386691417528">Specify only the join order.</li></ul>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188163600__screen1897710421122"><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">leading</span><span class="p">(</span><span class="n">join_table_list</span><span class="p">)</span><span class="w"> </span>
</pre></div></td></tr></table></div>
</div>
<ul id="EN-US_TOPIC_0000001188163600__ul8659131819528"><li id="EN-US_TOPIC_0000001188163600__li1465921812522">Specify the join order and outer/inner tables. The outer/inner tables are specified by the outermost parentheses.</li></ul>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188163600__screen18528112721316"><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">leading</span><span class="p">((</span><span class="n">join_table_list</span><span class="p">))</span><span class="w"> </span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001188163600__section1280444714345"><a name="EN-US_TOPIC_0000001188163600__section1280444714345"></a><a name="section1280444714345"></a><h4 class="sectiontitle">Parameter Description</h4><p id="EN-US_TOPIC_0000001188163600__p15208507581"><em id="EN-US_TOPIC_0000001188163600__i48557201119">join_table_list</em> specifies the tables to be joined. The values can be table names or table aliases. If a subquery is pulled up, the value can also be the subquery alias. Separate the values with spaces. You can add parentheses to specify the join priorities of tables.</p>
<div class="notice" id="EN-US_TOPIC_0000001188163600__note652010508589"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001188163600__p4520195025813">A table name or alias can only be a string without a schema name.</p>
<p id="EN-US_TOPIC_0000001188163600__p35030564144059">An alias (if any) is used to represent a table.</p>
</div></div>
</div>
<p id="EN-US_TOPIC_0000001188163600__p4768657111834">To prevent semantic errors, tables in the list must meet the following requirements:</p>
<ul id="EN-US_TOPIC_0000001188163600__ul17260195975717"><li id="EN-US_TOPIC_0000001188163600__li226010597578">The tables must exist in the query or its subquery to be pulled up.</li><li id="EN-US_TOPIC_0000001188163600__li52601659115711">The table names must be unique in the query or subquery to be pulled up. If they are not, their aliases must be unique.</li><li id="EN-US_TOPIC_0000001188163600__li1326016595570">A table appears only once in the list.</li><li id="EN-US_TOPIC_0000001188163600__li92601599574">An alias (if any) is used to represent a table.</li></ul>
<p id="EN-US_TOPIC_0000001188163600__p1006858011834">For example:</p>
<p id="EN-US_TOPIC_0000001188163600__p10791356174841"><strong id="EN-US_TOPIC_0000001188163600__b14429516152918">leading(t1 t2 t3 t4 t5)</strong>: <strong id="EN-US_TOPIC_0000001188163600__b182774813011">t1</strong>, <strong id="EN-US_TOPIC_0000001188163600__b121731511123013">t2</strong>, <strong id="EN-US_TOPIC_0000001188163600__b6629191411304">t3</strong>, <strong id="EN-US_TOPIC_0000001188163600__b19741131812305">t4</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b081313226302">t5</strong> are joined. The join order and outer/inner tables are not specified.</p>
<p id="EN-US_TOPIC_0000001188163600__p2350836311834"><strong id="EN-US_TOPIC_0000001188163600__b966212011290">leading(t1 t2 t3 t4 t5)</strong>: <strong id="EN-US_TOPIC_0000001188163600__b182774813011_1">t1</strong>, <strong id="EN-US_TOPIC_0000001188163600__b121731511123013_1">t2</strong>, <strong id="EN-US_TOPIC_0000001188163600__b6629191411304_1">t3</strong>, <strong id="EN-US_TOPIC_0000001188163600__b19741131812305_1">t4</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b081313226302_1">t5</strong> are joined in sequence. The table on the right is used as the inner table in each join.</p>
<p id="EN-US_TOPIC_0000001188163600__p62137248175715"><strong id="EN-US_TOPIC_0000001188163600__b4386735102916">leading(t1 (t2 t3 t4) t5)</strong>: First, <strong id="EN-US_TOPIC_0000001188163600__b9534104613396">t2</strong>, <strong id="EN-US_TOPIC_0000001188163600__b1597204920391">t3</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b6725125117395">t4</strong> are joined and the outer/inner tables are not specified. Then, the result is joined with <strong id="EN-US_TOPIC_0000001188163600__b41786115411">t1</strong> and <strong id="EN-US_TOPIC_0000001188163600__b8183716419">t5</strong>, and the outer/inner tables are not specified.</p>
<p id="EN-US_TOPIC_0000001188163600__p760856193620"><strong id="EN-US_TOPIC_0000001188163600__b217774017295">leading(t1 (t2 t3 t4) t5)</strong>: First, <strong id="EN-US_TOPIC_0000001188163600__b88729586418">t2</strong>, <strong id="EN-US_TOPIC_0000001188163600__b14959111104211">t3</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b1493115464219">t4</strong> are joined and the outer/inner tables are not specified. Then, the result is joined with <strong id="EN-US_TOPIC_0000001188163600__b1416212914431">t1</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b115386240443">(t2 t3 t4)</strong> is used as the inner table. Finally, the result is joined with <strong id="EN-US_TOPIC_0000001188163600__b1111345814454">t5</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b1482351094614">t5</strong> is used as the inner table.</p>
<p id="EN-US_TOPIC_0000001188163600__p4896571818053"><strong id="EN-US_TOPIC_0000001188163600__b174814213515">leading((t1 (t2 t3) t4 t5)) leading((t3 t2))</strong>: First, <strong id="EN-US_TOPIC_0000001188163600__b7768174419461">t2</strong> and <strong id="EN-US_TOPIC_0000001188163600__b1996173017478">t3</strong> are joined and <strong id="EN-US_TOPIC_0000001188163600__b143101537154719">t2</strong> is used as the inner table. Then, the result is joined with <strong id="EN-US_TOPIC_0000001188163600__b28041036184816">t1</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b179121440144812">(t2 t3)</strong> is used as the inner table. Finally, the result is joined with <strong id="EN-US_TOPIC_0000001188163600__b15590164135017">t4</strong> and then <strong id="EN-US_TOPIC_0000001188163600__b183521177503">t5</strong>, and the table on the right in each join is used as the inner table.</p>
<div class="section" id="EN-US_TOPIC_0000001188163600__section1127715590585"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001188163600__p1698632723710">Hint the query plan in <a href="dws_04_0455.html#EN-US_TOPIC_0000001188642062__section671421102912">Examples</a> as follows:</p>
</div>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188163600__screen5125050101413"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">explain</span>
<span class="k">select</span><span class="w"> </span><span class="cm">/*+ leading((((((store_sales store) promotion) item) customer) ad2) store_returns) leading((store store_sales))*/</span><span class="w"> </span><span class="n">i_product_name</span><span class="w"> </span><span class="n">product_name</span><span class="w"> </span><span class="p">...</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001188163600__p35873872152236">First, <strong id="EN-US_TOPIC_0000001188163600__b8896252175710">store_sales</strong> and <strong id="EN-US_TOPIC_0000001188163600__b1740215557570">store</strong> are joined and <strong id="EN-US_TOPIC_0000001188163600__b05841425135819">store_sales</strong> is the inner table. Then, The result is joined with <strong id="EN-US_TOPIC_0000001188163600__b43835418582">promotion</strong>, <strong id="EN-US_TOPIC_0000001188163600__b125222195912">item</strong>, <strong id="EN-US_TOPIC_0000001188163600__b18832658597">customer</strong>, <strong id="EN-US_TOPIC_0000001188163600__b11941785594">ad2</strong>, and <strong id="EN-US_TOPIC_0000001188163600__b56931112175915">store_returns</strong> in sequence. The optimized plan is as follows:</p>
<p id="EN-US_TOPIC_0000001188163600__p15822422163618"><span><img id="EN-US_TOPIC_0000001188163600__image68221322183614" src="figure/en-us_image_0000001188642242.png"></span></p>
<p id="EN-US_TOPIC_0000001188163600__p50454036152424">For details about the warning at the top of the plan, see <a href="dws_04_0464.html">Hint Errors, Conflicts, and Other Warnings</a>.</p>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_0454.html">Hint-based Tuning</a></div>
</div>
</div>