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

99 lines
22 KiB
HTML

<a name="EN-US_TOPIC_0000001460722632"></a><a name="EN-US_TOPIC_0000001460722632"></a>
<h1 class="topictitle1">Join Order Hints</h1>
<div id="body1534471018690"><div class="section" id="EN-US_TOPIC_0000001460722632__section97491741123412"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001460722632__p1899334819349">Theses hints specify the join order and outer/inner tables.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001460722632__section128191729143517"><h4 class="sectiontitle">Syntax</h4><ul id="EN-US_TOPIC_0000001460722632__ul188666143526"><li id="EN-US_TOPIC_0000001460722632__li386691417528">Single-layer parentheses (), specifying only the join order. The order of internal and foreign tables is not specified.</li></ul>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722632__screen1897710421122"><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">leading</span><span class="p">(</span><span class="n">join_table_list</span><span class="p">)</span><span class="w"> </span>
<span class="k">leading</span><span class="p">(</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </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_0000001460722632__ul8659131819528"><li id="EN-US_TOPIC_0000001460722632__li1465921812522">Double parentheses (()), specifying 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_0000001460722632__screen18528112721316"><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">leading</span><span class="p">((</span><span class="n">join_table_list</span><span class="p">))</span><span class="w"> </span>
<span class="k">leading</span><span class="p">(</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </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>
<ul id="EN-US_TOPIC_0000001460722632__ul1594253214548"><li id="EN-US_TOPIC_0000001460722632__li1591424142814">Single-layer square brackets [], specifying the join order of [] and the sequence of internal and foreign tables.</li></ul>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722632__screen456433632810"><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">leading</span><span class="p">[</span><span class="n">join_table_list</span><span class="p">]</span>
<span class="k">leading</span><span class="p">[</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </span><span class="n">join_table_list</span><span class="p">]</span>
</pre></div></td></tr></table></div>
</div>
<ul id="EN-US_TOPIC_0000001460722632__ul8106123911284"><li id="EN-US_TOPIC_0000001460722632__li1210613396285">Combination of single-layer parentheses () and single-layer square brackets [], specifying the join order and the sequence of internal and foreign tables at any layer. The parentheses () specify only the join order, but not the sequence of internal and foreign tables. The square brackets [] specify both the join order and the sequence of internal and foreign tables.</li></ul>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722632__screen18942132105410"><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>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</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_list1</span><span class="w"> </span><span class="p">[</span><span class="n">join_table_list2</span><span class="p">])</span>
<span class="k">leading</span><span class="p">[</span><span class="n">join_table_list1</span><span class="w"> </span><span class="p">[</span><span class="n">join_table_list2</span><span class="p">]]</span>
<span class="k">leading</span><span class="p">[</span><span class="n">join_table_list1</span><span class="w"> </span><span class="p">(</span><span class="n">join_table_list2</span><span class="p">)]</span>
<span class="k">leading</span><span class="p">(</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </span><span class="n">join_table_list1</span><span class="w"> </span><span class="p">[</span><span class="n">join_table_list2</span><span class="p">])</span>
<span class="k">leading</span><span class="p">[</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </span><span class="n">join_table_list1</span><span class="w"> </span><span class="p">[</span><span class="n">join_table_list2</span><span class="p">]]</span>
<span class="k">leading</span><span class="p">[</span><span class="o">@</span><span class="n">block_name</span><span class="w"> </span><span class="n">join_table_list1</span><span class="w"> </span><span class="p">(</span><span class="n">join_table_list2</span><span class="p">)]</span>
</pre></div></td></tr></table></div>
</div>
<div class="notice" id="EN-US_TOPIC_0000001460722632__note335513341650"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001460722632__p153551234852">Single-layer square brackets [] can be used together with single-layer parentheses () to specify the sequence of internal and foreign tables of any layer. Single-layer [] and double-layer () cannot be used together.</p>
</div></div>
<div class="section" id="EN-US_TOPIC_0000001460722632__section1280444714345"><a name="EN-US_TOPIC_0000001460722632__section1280444714345"></a><a name="section1280444714345"></a><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001460722632__ul120216444473"><li id="EN-US_TOPIC_0000001460722632__li764414914812"><strong id="EN-US_TOPIC_0000001460722632__b1464069144812">join_table_list</strong><p id="EN-US_TOPIC_0000001460722632__p1398881074819">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="p" id="EN-US_TOPIC_0000001460722632__p8223163212110">To prevent semantic errors, tables in the list must meet the following requirements:<ul id="EN-US_TOPIC_0000001460722632__ul16629528316"><li id="EN-US_TOPIC_0000001460722632__li106291828919">The tables must exist in the query or its subquery to be pulled up.</li><li id="EN-US_TOPIC_0000001460722632__li56295281015">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_0000001460722632__li1629128816">A table appears only once in the list.</li><li id="EN-US_TOPIC_0000001460722632__li7629102810111">An alias (if any) is used to represent a table.</li></ul>
</div>
<div class="note" id="EN-US_TOPIC_0000001460722632__note91032059408"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="EN-US_TOPIC_0000001460722632__ul114831571316"><li id="EN-US_TOPIC_0000001460722632__li134831971819">The syntax format of the table is as follows:<p id="EN-US_TOPIC_0000001460722632__p6483117519"><a name="EN-US_TOPIC_0000001460722632__li134831971819"></a><a name="li134831971819"></a>[schema.]table[@block_name]</p>
<p id="EN-US_TOPIC_0000001460722632__p204832718119">The table name can contain the schema name or block name before the subquery statement block is promoted. If the subquery statement block is optimized and rewritten by the optimizer, the value of <strong id="EN-US_TOPIC_0000001460722632__b1442317392810">block_name</strong> is different from that of <strong id="EN-US_TOPIC_0000001460722632__b152968102815">block_name</strong> in leading.</p>
</li><li id="EN-US_TOPIC_0000001460722632__li3483157118">If a table has an alias, the alias is preferentially used to represent the table.</li></ul>
</div></div>
</li></ul>
</div>
<ul id="EN-US_TOPIC_0000001460722632__ul1090294415514"><li id="EN-US_TOPIC_0000001460722632__li99021444551"><a name="EN-US_TOPIC_0000001460722632__li99021444551"></a><a name="li99021444551"></a><strong id="EN-US_TOPIC_0000001460722632__b1018411361478">block_name</strong><p id="EN-US_TOPIC_0000001460722632__p676542935611">Specifies the block name of the statement block. It indicates that the hint takes effect in the subquery statement block corresponding to the block name.</p>
<div class="notice" id="EN-US_TOPIC_0000001460722632__note545385518562"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><ul id="EN-US_TOPIC_0000001460722632__ul181631227122612"><li id="EN-US_TOPIC_0000001460722632__li1016352792620">By default, a block name is generated for a statement.</li></ul>
<ul id="EN-US_TOPIC_0000001460722632__ul205384161254"><li id="EN-US_TOPIC_0000001460722632__li1153821617259">CN lightweight statements do not generate block names.</li></ul>
<ul id="EN-US_TOPIC_0000001460722632__ul20757161802515"><li id="EN-US_TOPIC_0000001460722632__li1675771862513">Block names can be generated for the CREATE TABLE AS SELECT, SELECT INTO, SELECT, INSERT, UPDATE, DELETE, and MERGE statements.</li></ul>
<ul id="EN-US_TOPIC_0000001460722632__ul112511122712"><li id="EN-US_TOPIC_0000001460722632__li725110152711">The naming rule of a block name is as follows:<ul id="EN-US_TOPIC_0000001460722632__ul61581227112712"><li id="EN-US_TOPIC_0000001460722632__li115802752716">A block name is automatically generated for the SELECT, INSERT, UPDATE, DELETE, and MERGE statements. The naming format of a block name for these statements is sel$n, ins$n, upd$n, del$n, and mer$n, respectively, where n starts from 1. The number of statements of different types is not accumulated, but the number of statements of the same type is accumulated.<p id="EN-US_TOPIC_0000001460722632__p1339018141233"><a name="EN-US_TOPIC_0000001460722632__li115802752716"></a><a name="li115802752716"></a>Example:</p>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722632__screen423517594204"><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">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">t</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">t1</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">a1</span><span class="w"> </span><span class="k">IN</span><span class="w"> </span><span class="p">(</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">t2</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001460722632__p1639021412312">--------sel$2-------</p>
<p id="EN-US_TOPIC_0000001460722632__p1939061419316">-----------------------sel$1----------------------</p>
<p id="EN-US_TOPIC_0000001460722632__p208893218315">--------------------------------ins$1---------------------------</p>
</li><li id="EN-US_TOPIC_0000001460722632__li151591627112714">Recursively assigns a block name to each statement block before the optimizer is used.<div class="p" id="EN-US_TOPIC_0000001460722632__p69301451257"><a name="EN-US_TOPIC_0000001460722632__li151591627112714"></a><a name="li151591627112714"></a>First, assign block names to the existing statements block based on the statement type, then traverse the statement blocks in the following sequence, and assign block name to the statement blocks in the statement blocks:<ol id="EN-US_TOPIC_0000001460722632__ol1726033520510"><li id="EN-US_TOPIC_0000001460722632__li1860123411514">Traverse the target column.</li><li id="EN-US_TOPIC_0000001460722632__li3975639610">Traverse the target column in the source table of the MERGE statement.</li><li id="EN-US_TOPIC_0000001460722632__li6172129060">Traverse actions (update or insert) in the MERGE statement.</li><li id="EN-US_TOPIC_0000001460722632__li75360397614">Traverse the returning clause.</li><li id="EN-US_TOPIC_0000001460722632__li6566947461">Traverse the Join and Where conditions in From. (The join condition takes precedence over the Where condition.)</li><li id="EN-US_TOPIC_0000001460722632__li443817553616">For a set operation, traverse each branch of the set (UNION, INTERSECT, and EXCEPT).</li><li id="EN-US_TOPIC_0000001460722632__li1788216415715">Traverse the HAVING clause.</li><li id="EN-US_TOPIC_0000001460722632__li10654121217719">Traverse the LIMIT OFFSET clause.</li><li id="EN-US_TOPIC_0000001460722632__li56912202070">Traverse the LIMIT COUNT clause.</li><li id="EN-US_TOPIC_0000001460722632__li16330112811716">Traverse CTE</li><li id="EN-US_TOPIC_0000001460722632__li1649013718713">Traverse the table after From.</li><li id="EN-US_TOPIC_0000001460722632__li4431644277">Traverse the UPSERT clause.</li></ol>
</div>
</li><li id="EN-US_TOPIC_0000001460722632__li0159172752720">In the rewriting phase of the optimizer, rewriting optimization is performed due to FUL LJOIN, cte inline, materialized view rewriting, INLIST2JOIN, OR conversion, multi count(distinct), Magic Set, lazyagg, and subquery/sublink promotion, a new subquery is constructed. In this case, the recursive processing during block name assignment is also applied to the newly constructed subquery. The number of block names is accumulated.</li><li id="EN-US_TOPIC_0000001460722632__li815914277279">In the optimizer rewriting phase, when a subquery is promoted, the table in the inner subquery is promoted to the outer query, and the inner subquery is eliminated. In this case, the promoted table may have the same name as the table in the outer queries. Therefore, the block name to which the promoted table belongs is recorded in the table to distinguish two tables with the same name but are from different query blocks.</li></ul>
</li></ul>
</div></div>
</li></ul>
<p id="EN-US_TOPIC_0000001460722632__p1006858011834">For example:</p>
<p id="EN-US_TOPIC_0000001460722632__p10791356174841"><strong id="EN-US_TOPIC_0000001460722632__b14429516152918">leading(t1 t2 t3 t4 t5)</strong>: <strong id="EN-US_TOPIC_0000001460722632__b182774813011">t1</strong>, <strong id="EN-US_TOPIC_0000001460722632__b121731511123013">t2</strong>, <strong id="EN-US_TOPIC_0000001460722632__b6629191411304">t3</strong>, <strong id="EN-US_TOPIC_0000001460722632__b19741131812305">t4</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b081313226302">t5</strong> are joined. The join order and outer/inner tables are not specified.</p>
<p id="EN-US_TOPIC_0000001460722632__p2350836311834"><strong id="EN-US_TOPIC_0000001460722632__b966212011290">leading(t1 t2 t3 t4 t5)</strong>: <strong id="EN-US_TOPIC_0000001460722632__b1970416000">t1</strong>, <strong id="EN-US_TOPIC_0000001460722632__b798346837">t2</strong>, <strong id="EN-US_TOPIC_0000001460722632__b1428035963">t3</strong>, <strong id="EN-US_TOPIC_0000001460722632__b325646328">t4</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b1403928940">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_0000001460722632__p62137248175715"><strong id="EN-US_TOPIC_0000001460722632__b4386735102916">leading(t1 (t2 t3 t4) t5)</strong>: First, <strong id="EN-US_TOPIC_0000001460722632__b9534104613396">t2</strong>, <strong id="EN-US_TOPIC_0000001460722632__b1597204920391">t3</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b6725125117395">t4</strong> are joined and the outer/inner tables are not specified. Then, the result is joined with <strong id="EN-US_TOPIC_0000001460722632__b41786115411">t1</strong> and <strong id="EN-US_TOPIC_0000001460722632__b8183716419">t5</strong>, and the outer/inner tables are not specified.</p>
<p id="EN-US_TOPIC_0000001460722632__p760856193620"><strong id="EN-US_TOPIC_0000001460722632__b217774017295">leading(t1 (t2 t3 t4) t5)</strong>: First, <strong id="EN-US_TOPIC_0000001460722632__b88729586418">t2</strong>, <strong id="EN-US_TOPIC_0000001460722632__b14959111104211">t3</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b1493115464219">t4</strong> are joined and the outer/inner tables are not specified. Then, the result is joined with <strong id="EN-US_TOPIC_0000001460722632__b1416212914431">t1</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b115386240443">(t2 t3 t4)</strong> is used as the inner table. Finally, the result is joined with <strong id="EN-US_TOPIC_0000001460722632__b1111345814454">t5</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b1482351094614">t5</strong> is used as the inner table.</p>
<p id="EN-US_TOPIC_0000001460722632__p4896571818053"><strong id="EN-US_TOPIC_0000001460722632__b174814213515">leading((t1 (t2 t3) t4 t5)) leading((t3 t2))</strong>: First, <strong id="EN-US_TOPIC_0000001460722632__b7768174419461">t2</strong> and <strong id="EN-US_TOPIC_0000001460722632__b1996173017478">t3</strong> are joined and <strong id="EN-US_TOPIC_0000001460722632__b143101537154719">t2</strong> is used as the inner table. Then, the result is joined with <strong id="EN-US_TOPIC_0000001460722632__b28041036184816">t1</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b179121440144812">(t2 t3)</strong> is used as the inner table. Finally, the result is joined with <strong id="EN-US_TOPIC_0000001460722632__b15590164135017">t4</strong> and then <strong id="EN-US_TOPIC_0000001460722632__b183521177503">t5</strong>, and the table on the right in each join is used as the inner table.</p>
<p id="EN-US_TOPIC_0000001460722632__p194713154211">leading[t1 [t2 t3]] is equivalent to leading((t1 (t2 t3))) leading((t2 t3)).</p>
<p id="EN-US_TOPIC_0000001460722632__p8815533144313">leading(t1 [t2 t3]) is equivalent to leading(t1 t2 t3) leading((t2 t3)).</p>
<p id="EN-US_TOPIC_0000001460722632__p385312212087">leading[@sel$1 t1@sel$1 [t2@sel$2 t3@sel$2]] indicates that t2 and t3 are located in the subquery. After the subquery is promoted, t2 and t3 are joined, and then the join table is joined to t1. Where t2 is a foreign table, t3 is an internal table, t1 is a foreign table. The join table of t2 and t3 is an internal table.</p>
<div class="section" id="EN-US_TOPIC_0000001460722632__section1127715590585"><h4 class="sectiontitle">Examples</h4><p id="EN-US_TOPIC_0000001460722632__p1698632723710">Hint the query plan in <a href="dws_04_0455.html#EN-US_TOPIC_0000001460562888__section671421102912">Examples</a> as follows:</p>
</div>
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001460722632__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_0000001460722632__p35873872152236">First, <strong id="EN-US_TOPIC_0000001460722632__b8896252175710">store_sales</strong> and <strong id="EN-US_TOPIC_0000001460722632__b1740215557570">store</strong> are joined and <strong id="EN-US_TOPIC_0000001460722632__b05841425135819">store_sales</strong> is the inner table. Then, The result is joined with <strong id="EN-US_TOPIC_0000001460722632__b43835418582">promotion</strong>, <strong id="EN-US_TOPIC_0000001460722632__b125222195912">item</strong>, <strong id="EN-US_TOPIC_0000001460722632__b18832658597">customer</strong>, <strong id="EN-US_TOPIC_0000001460722632__b11941785594">ad2</strong>, and <strong id="EN-US_TOPIC_0000001460722632__b56931112175915">store_returns</strong> in sequence. The optimized plan is as follows:</p>
<p id="EN-US_TOPIC_0000001460722632__p15822422163618"><span><img id="EN-US_TOPIC_0000001460722632__image68221322183614" src="figure/en-us_image_0000001510402845.png" height="231.42000000000002" width="523.6875" title="Click to enlarge" class="imgResize"></span></p>
<p id="EN-US_TOPIC_0000001460722632__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>
<script language="JavaScript">
<!--
image_size('.imgResize');
var msg_imageMax = "view original image";
var msg_imageClose = "close";
//--></script>