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

82 lines
14 KiB
HTML

<a name="EN-US_TOPIC_0000001809519836"></a><a name="EN-US_TOPIC_0000001809519836"></a>
<h1 class="topictitle1">UPSERT</h1>
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_sc38eadebc8e74e1e8200bca116ca55ef"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p151160440274">HStore is compatible with the <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b1575214485385">UPSERT</strong> syntax. You can add one or more rows to a table. When a row duplicates an existing primary key or unique key value, the row will be ignored or updated.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_s382c620cdede4a91ba5d429f091c14c9"><h4 class="sectiontitle">Precautions</h4><ul id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_u83811bebe7004526bcd5b0d5cb5278d2"><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li06242311283">The <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b17387542103810">UPSERT</strong> statement of updating data upon conflict can be executed only when the target table contains a primary key or unique index.</li><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li08361856172816">Similar to column storage, an update operation performed using <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b1192391516">UPSERT</strong> on an HStore table in the current version involves DELETE and INSERT.</li><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li1312519219294">In concurrent UPSERT scenarios, operations on the same CU will cause lock conflicts in traditional column-store tables and result in low performance. For HStore tables, the operations can be concurrently performed, and the upsert performance can be more than 100 times that of column-store tables.</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_s63e68d20fc8a4670b54b531fc5722bb9"><h4 class="sectiontitle">Syntax</h4>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_table663035101813" frame="border" border="1" rules="all"><caption><b>Table 1 </b>UPSERT syntax</caption><thead align="left"><tr id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_row261035171818"><th align="left" class="cellrowborder" valign="top" width="16.650000000000002%" id="mcps1.3.3.2.2.4.1.1"><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p9611235101812">Syntax</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="44.379999999999995%" id="mcps1.3.3.2.2.4.1.2"><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p15611935121816">Update Data Upon Conflict</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="38.97%" id="mcps1.3.3.2.2.4.1.3"><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p36123517186">Ignore Data Upon Conflict</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_row46213581815"><td class="cellrowborder" valign="top" width="16.650000000000002%" headers="mcps1.3.3.2.2.4.1.1 "><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p196113359183">Syntax 1: No index is specified.</p>
</td>
<td class="cellrowborder" valign="top" width="44.379999999999995%" headers="mcps1.3.3.2.2.4.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen10798943161918">INSERT INTO ON DUPLICATE KEY UPDATE</pre>
</td>
<td class="cellrowborder" valign="top" width="38.97%" headers="mcps1.3.3.2.2.4.1.3 "><pre class="screen" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen139871862019">INSERT IGNORE
INSERT INTO ON CONFLICT DO NOTHING</pre>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_row663163591811"><td class="cellrowborder" valign="top" width="16.650000000000002%" headers="mcps1.3.3.2.2.4.1.1 "><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p962163521814">Syntax 2: The unique key constraint can be inferred from the specified column name or constraint name.</p>
</td>
<td class="cellrowborder" valign="top" width="44.379999999999995%" headers="mcps1.3.3.2.2.4.1.2 "><pre class="screen" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen122585578195">INSERT INTO ON CONFLICT(...) DO UPDATE SET
INSERT INTO ON CONFLICT ON CONSTRAINT con_name DO UPDATE SET</pre>
</td>
<td class="cellrowborder" valign="top" width="38.97%" headers="mcps1.3.3.2.2.4.1.3 "><pre class="screen" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen95451842200">INSERT INTO ON CONFLICT(...) DO NOTHING
INSERT INTO ON CONFLICT ON CONSTRAINT con_name DO NOTHING</pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_s0568dfe900e948eaa67d0a335c26701c"><h4 class="sectiontitle">Parameters</h4><p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p127329159316">In syntax 1, no index is specified. The system checks for conflicts on all primary keys or unique indexes. If a conflict exists, the system ignores or updates the corresponding data.</p>
<p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p335817251039">In syntax 2, a specified index is used for conflict check. The primary key or unique index is inferred from the column name, the expression that contains column names, or the constraint name specified in the <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b812662553115910">ON CONFLICT</strong> clause.</p>
<ul id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_ul12899166151116"><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li8686111819410">Unique index inference<p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p152226442"><a name="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li8686111819410"></a><a name="en-us_topic_0000001492158353_li8686111819410"></a>Syntax 2 infers the primary key or unique index by specifying the column name or constraint name. You can specify a single column name or multiple column names by using an expression. Example: <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b84301347185113">column1, column2, column3</strong></p>
</li></ul>
<ul id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_ul15430192210115"><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li196101274116"><strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b116056475115910">UPDATE</strong> clause<p id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p44397717254">The <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b619394132115910">UPDATE</strong> clause can use <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b299253666115910">VALUES(colname)</strong> or <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b1588099717115910">EXCLUDED.colname</strong> to reference inserted data. <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b47629290115910">EXCLUDED</strong> indicates the rows that should be excluded due to conflicts.</p>
</li></ul>
<ul id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_ul174849128396"><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li20777357396"><strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b1860028802115910">WHERE</strong> clause<ul id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_ul14512164911391"><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li114842124399">The <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b48464534115910">WHERE</strong> clause is used to determine whether a specified condition is met when data conflict occurs. If yes, update the conflict data. Otherwise, ignore it.</li><li id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_li1894841402">Only syntax 2 of <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b1995945690115910">Update Data Upon Conflict</strong> can specify the <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b711345392115910">WHERE</strong> clause, that is, <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b852617851115910">INSERT INTO ON CONFLICT(...) DO UPDATE SET WHERE</strong>.</li></ul>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_sc62f0ffeacde4228a7e8c56b5a2b85f1"><h4 class="sectiontitle">Example</h4><div class="p" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p318719109517">Create table <strong id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_b15333175611527">reason_upsert</strong> and insert data into it.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen13318419544"><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>
<span class="normal">7</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">reason_upsert</span>
<span class="p">(</span>
<span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="nb">int</span><span class="w"> </span><span class="k">primary</span><span class="w"> </span><span class="k">key</span><span class="p">,</span>
<span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="nb">int</span><span class="p">,</span>
<span class="w"> </span><span class="k">c</span><span class="w"> </span><span class="nb">int</span>
<span class="p">)</span><span class="k">WITH</span><span class="p">(</span><span class="n">ORIENTATION</span><span class="o">=</span><span class="k">COLUMN</span><span class="p">,</span><span class="w"> </span><span class="n">ENABLE_HSTORE</span><span class="o">=</span><span class="k">ON</span><span class="p">);</span>
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">reason_upsert</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="p" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p93713814617">Ignore conflicting data.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen16375812618"><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">reason_upsert</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">),(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">CONFLICT</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="k">DO</span><span class="w"> </span><span class="k">NOTHING</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="p" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_p12607644268">Update conflicting data.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001809519836__en-us_topic_0000001492158353_screen196071844568"><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">reason_upsert</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">),(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">)</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">CONFLICT</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="k">DO</span><span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="k">SET</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">EXCLUDED</span><span class="p">.</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="k">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">EXCLUDED</span><span class="p">.</span><span class="k">c</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_04_1026.html">Hybrid Data Warehouse Syntax</a></div>
</div>
</div>