Yang, Tong 48706b7552 MRS COMP-LTS 320-lts.1 version
Reviewed-by: Kacur, Michal <michal.kacur@t-systems.com>
Co-authored-by: Yang, Tong <yangtong2@huawei.com>
Co-committed-by: Yang, Tong <yangtong2@huawei.com>
2024-04-12 12:51:10 +00:00

183 lines
24 KiB
HTML

<a name="mrs_01_24545"></a><a name="mrs_01_24545"></a>
<h1 class="topictitle1">SQL Statement Example of Materialized Views</h1>
<div id="body32001227"><p id="mrs_01_24545__en-us_topic_0000001521198793_p18480881394">For details about the SQL statements for materialized views, see <a href="#mrs_01_24545__en-us_topic_0000001521198793_table167251859399">Table 1</a>.</p>
<div class="tablenoborder"><a name="mrs_01_24545__en-us_topic_0000001521198793_table167251859399"></a><a name="en-us_topic_0000001521198793_table167251859399"></a><table cellpadding="4" cellspacing="0" summary="" id="mrs_01_24545__en-us_topic_0000001521198793_table167251859399" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Operations on materialized views</caption><thead align="left"><tr id="mrs_01_24545__en-us_topic_0000001521198793_row10725105143911"><th align="left" class="cellrowborder" valign="top" width="14.42%" id="mcps1.3.2.2.5.1.1"><p id="mrs_01_24545__en-us_topic_0000001521198793_p27253563911">Operation</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="16.49%" id="mcps1.3.2.2.5.1.2"><p id="mrs_01_24545__en-us_topic_0000001521198793_p197257563915">Function</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="37.96%" id="mcps1.3.2.2.5.1.3"><p id="mrs_01_24545__en-us_topic_0000001521198793_p1725165103918">SQL Statement Example of Materialized View</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="31.130000000000003%" id="mcps1.3.2.2.5.1.4"><p id="mrs_01_24545__en-us_topic_0000001521198793_p1172520513911">Remarks</p>
</th>
</tr>
</thead>
<tbody><tr id="mrs_01_24545__en-us_topic_0000001521198793_row13725856396"><td class="cellrowborder" rowspan="3" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p87257583916">Creating a materialized view</p>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p2413376242"></p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1772517519397">Creat a materialized view that never expires.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre1535413182249">create materialized view mv.default.mv1 with(mv_storage_table='hive.default.mv11') AS select id from hive.mvschema.t1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><ul id="mrs_01_24545__en-us_topic_0000001521198793_ul1190285543110"><li id="mrs_01_24545__en-us_topic_0000001521198793_li15902105553119"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b38017231212">mv_storage_table</strong> specifies the location where the materialized view data is materialized into a physical table.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li090245520315">When creating a materialized view, you must specify <strong id="mrs_01_24545__en-us_topic_0000001521198793_b15288132872115">mv</strong> for the catalog. You can also create a schema.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li1952416392403">For the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b32169515245">AS SELECT</strong> clause, pay attention to the items listed in <a href="#mrs_01_24545__en-us_topic_0000001521198793_section1215122434112">Creating the AS SELECT Clause for a Materialized View</a>.</li></ul>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row07256563917"><td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p57251655396">Create a materialized view that is valid for one day and cannot automatically refresh.</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.2 "><pre id="mrs_01_24545__pre320462313241">create materialized view mv.default.mv1 with(mv_storage_table='hive.default.mv11', mv_validity = '24h') AS select id from hive.mvschema.t1;</pre>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.3 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p37251853394"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b207818727102848">mv_validity</strong> specifies the validity of a materialized view.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row127256563910"><td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p11725145143917">Create a materialized view that automatically refreshes data every hour.</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.2 "><pre id="mrs_01_24545__pre19641162852419">create materialized view mv.default.mv1 with(mv_storage_table='hive.default.mv1', need_autorefresh = true, mv_validity = '1h', start_refresh_ahead_of_expiry = 0.2, refresh_priority = 3, refresh_duration = '5m') AS select id from hive.mvschema.t1;</pre>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.3 "><ul id="mrs_01_24545__en-us_topic_0000001521198793_ul18295839125116"><li id="mrs_01_24545__en-us_topic_0000001521198793_li16295163915113"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b329377319">need_autorefresh</strong>: indicates whether to enable automatic refresh.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li152951939165116"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b15758430902848">start_refresh_ahead_of_expiry</strong>: a refresh task is triggered for the materialized view at the time specified by <strong id="mrs_01_24545__en-us_topic_0000001521198793_b30572397602848">mv_validity*</strong> (<strong id="mrs_01_24545__en-us_topic_0000001521198793_b37581586602848">1-start_refresh_ahead_of_expiry</strong>) so that the task status is changed to <strong id="mrs_01_24545__en-us_topic_0000001521198793_b7223104018203">Refreshable</strong>.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li1229523995115"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b36359722102848">refresh_priority</strong> specifies the priority of refreshing tasks.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li1429513914511"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b111390396902848">refresh_duration</strong> specifies the maximum duration of a refreshing task.</li></ul>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row572513543912"><td class="cellrowborder" rowspan="2" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p77252513912">Showing materialized views</p>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p1859616166536"></p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p872514563912">Show all MVs whose catalog name is <strong id="mrs_01_24545__en-us_topic_0000001521198793_b20748779102848">mv</strong> and schema name is <strong id="mrs_01_24545__en-us_topic_0000001521198793_b105371242002848">mvschema</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre15101103492418">show materialized views from mvschema;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p799519349262"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b38382642702848">mvschema</strong> indicates the schema name. The value of <strong id="mrs_01_24545__en-us_topic_0000001521198793_b161743671302848">catalog</strong> is fixed to <strong id="mrs_01_24545__en-us_topic_0000001521198793_b17018032602848">mv</strong>.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row155964168533"><td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p2596161645314">Use the LIKE clause to filter the materialized views whose names meet the rule expression.</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.2 "><pre id="mrs_01_24545__pre176592654010"><span id="mrs_01_24545__ph14723818162512">show MATERIALIZED VIEWs in mvschema tables like '*mvtb_0001';</span></pre>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.2.5.1.3 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p55961416205312"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b66765731002848">mvschema</strong> indicates the schema name.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row372515515391"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p157251754397">Querying the statement for creating a materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p117257513918">Query the statement for creating the the materialized view of <strong id="mrs_01_24545__en-us_topic_0000001521198793_b12644636102848">mv.default.mv1</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre577083942416">show create materialized view mv.default.mv1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p172517593913"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b175093495802848">mv1</strong> indicates the name of the materialized view.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row634813511545"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1734965112541">Querying a materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1349105135416">Query data in <strong id="mrs_01_24545__en-us_topic_0000001521198793_b49312445002848">mv.default.mv1</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre15756174562415">select * from mv.default.mv1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p18349951165410"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b36157204402848">mv1</strong> indicates the name of the materialized view.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row147251752397"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p57251054390">Refreshing a materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p15725185143918">Refresh the materialized view of <strong id="mrs_01_24545__en-us_topic_0000001521198793_b117681090702848">mv.default.mv1</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre781285012418">refresh materialized view mv.default.mv1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1272515513398">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row1844553611715"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p7446193681717">Modifying the properties of materialized views</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p8446636151715">Modifying the properties of the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b10465191120462">mv.default.mv1</strong> materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre9725195418248">Alter materialized view mv.mvtestprop.pepa_ss set PROPERTIES(refresh_priority = 2);</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p744613671719"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b17213037164618">refresh_priority = 2</strong> is the property of the materialized view.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row2803241181719"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p13803141121712">Changing the status of materialized views</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p08035418176">Changing the status of the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b1148704519462">mv.default.mv1</strong> materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre2985205802416">alter materialized view mv.default.mv1 set status SUSPEND;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1580324171712"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b826722017478">SUSPEND</strong> is the status of the materialized view. The status can be:</p>
<ul id="mrs_01_24545__en-us_topic_0000001521198793_ul5504173293216"><li id="mrs_01_24545__en-us_topic_0000001521198793_li1893133793214"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b14987165515472">SUSPEND</strong>: The materialized view is suspended. The suspended materialized view is not rewritten.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li450493214329"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b1440191754812">ENABLED</strong>: The materialized view is available.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li7505332153213"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b11321173916487">Refreshing</strong>: The materialized view data is being refreshed and cannot be rewritten.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li2505133243218"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b13668134824820">DISABLED</strong>: The materialized view has been disabled.</li><li id="mrs_01_24545__en-us_topic_0000001521198793_li050573263212"><strong id="mrs_01_24545__en-us_topic_0000001521198793_b58431751498">UNKNOWN</strong>: The cache is inconsistent with the database. You are advised to run the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b17583138124920">refresh catalog mv</strong> command.</li></ul>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p2032114318563">Manual refresh supports only the conversion between <strong id="mrs_01_24545__en-us_topic_0000001521198793_b1166213711221">ENABLED</strong> and <strong id="mrs_01_24545__en-us_topic_0000001521198793_b06631437112219">SUSPEND</strong>.</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row472545113918"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1372513513395">Deleting a materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p107251456398">Delete the materialized view of <strong id="mrs_01_24545__en-us_topic_0000001521198793_b148171573102848">mv.default.mv1</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre131615572514">drop materialized view mv.default.mv1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p6725145173919">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row12690142184217"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p269024214217">Enabling materialized view rewriting capability to optimize SQL statements</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p469012427427">Enabling materialized view rewriting capability at the session level to optimize SQL statements</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre1263618712516">set session materialized_view_rewrite_enabled=true;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p3690742144217">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row472616511395"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1872615123912">Verifying whether SQL statements can be optimized by rewriting a query to a materialized view</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p117261516397">Verify whether the SELECT statement can be rewritten and optimized by <strong id="mrs_01_24545__en-us_topic_0000001521198793_b16726262302848">mv.default.mv1</strong>.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre462071015256">verify materialized view mvname(mv.default.mv1) originalsql select id from hive.mvschema.t1;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p147266563915">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row7946125293612"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p79465525367">Enabling the specified materialized view at the SQL level to optimize the SQL statements</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p10946125218362">Forcibly use <strong id="mrs_01_24545__en-us_topic_0000001521198793_b93676744402848">mv.default.mv1</strong> for SQL statement optimization in queries.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre059451574213"><span id="mrs_01_24545__ph272491816251">/*+ REWRITE(mv.default.mv1) */ select id from hive.mvschema.t1;</span></pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p8946165293619">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row146614499362"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p846618495364">Disabling materialized views at the SQL level to optimize the SQL statements</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p8466649143616">Do not use materialized views for SQL statement optimization in queries.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre10443141964212"><span id="mrs_01_24545__ph1372411892511">/*+ NOREWRITE */ select id from hive.mvschema.t1;</span></pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p1746615494363">-</p>
</td>
</tr>
<tr id="mrs_01_24545__en-us_topic_0000001521198793_row310811181514"><td class="cellrowborder" valign="top" width="14.42%" headers="mcps1.3.2.2.5.1.1 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p121091516150">Refreshing the metadata cache of materialized views</p>
</td>
<td class="cellrowborder" valign="top" width="16.49%" headers="mcps1.3.2.2.5.1.2 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p13109519154">Synchronize the metadata cache of materialized views between tenants.</p>
</td>
<td class="cellrowborder" valign="top" width="37.96%" headers="mcps1.3.2.2.5.1.3 "><pre id="mrs_01_24545__pre1420251520253">refresh catalog mv;</pre>
</td>
<td class="cellrowborder" valign="top" width="31.130000000000003%" headers="mcps1.3.2.2.5.1.4 "><p id="mrs_01_24545__en-us_topic_0000001521198793_p61091171514">-</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="mrs_01_24545__en-us_topic_0000001521198793_section1215122434112"><a name="mrs_01_24545__en-us_topic_0000001521198793_section1215122434112"></a><a name="en-us_topic_0000001521198793_section1215122434112"></a><h4 class="sectiontitle">Creating the AS SELECT Clause for a Materialized View</h4><div class="p" id="mrs_01_24545__en-us_topic_0000001521198793_p9340181621219">The <strong id="mrs_01_24545__en-us_topic_0000001521198793_b1223694017318">AS SELECT</strong> clause for creating materialized views cannot contain reserved keywords in Calcite SQL parsing and rewriting functions, such as <strong id="mrs_01_24545__en-us_topic_0000001521198793_b1322665620312">default</strong>. To use reserved keywords in the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b16857141479">AS SELECT</strong> clause, use either of the following solutions:<ul id="mrs_01_24545__en-us_topic_0000001521198793_ul1415445815206"><li id="mrs_01_24545__en-us_topic_0000001521198793_li926419332490">When creating MVs and executing original queries, you need to add double quotes to the default schema name.<p id="mrs_01_24545__en-us_topic_0000001521198793_p16311335104910"><a name="mrs_01_24545__en-us_topic_0000001521198793_li926419332490"></a><a name="en-us_topic_0000001521198793_li926419332490"></a>The following uses reserved keyword <strong id="mrs_01_24545__en-us_topic_0000001521198793_b1647661510916">default</strong> in the <strong id="mrs_01_24545__en-us_topic_0000001521198793_b691853810919">AS SELECT</strong> clause as an example:</p>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p122433329226">Creating a materialized view</p>
<pre class="screen" id="mrs_01_24545__en-us_topic_0000001521198793_screen1716693610230">CREATE MATERIALIZED VIEW mv.default.mv1 WITH(mv_storage_table='hive.default.mv11') AS SELECT id FROM hive."<strong id="mrs_01_24545__en-us_topic_0000001521198793_b154741014192718">default"</strong>.t1;</pre>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p4980029122218">SELECT query</p>
<pre class="screen" id="mrs_01_24545__en-us_topic_0000001521198793_screen35082123244">SELECT id FROM hive.<strong id="mrs_01_24545__en-us_topic_0000001521198793_b421625022610">"default"</strong>.t1;</pre>
</li></ul>
</div>
<ul id="mrs_01_24545__en-us_topic_0000001521198793_ul57401359142318"><li id="mrs_01_24545__en-us_topic_0000001521198793_li167401959132319">Set the corresponding catalog and schema at the Session level, rather than passing fully qualified names in the query.<p id="mrs_01_24545__en-us_topic_0000001521198793_p977714131280"><a name="mrs_01_24545__en-us_topic_0000001521198793_li167401959132319"></a><a name="en-us_topic_0000001521198793_li167401959132319"></a>For example, set <strong id="mrs_01_24545__en-us_topic_0000001521198793_b10296114551115">catalogname</strong> to <strong id="mrs_01_24545__en-us_topic_0000001521198793_b19734125181119">hive</strong> and <strong id="mrs_01_24545__en-us_topic_0000001521198793_b7750165441114">schemaname</strong> to <strong id="mrs_01_24545__en-us_topic_0000001521198793_b444705717118">default</strong>.</p>
<pre class="screen" id="mrs_01_24545__en-us_topic_0000001521198793_screen20130121133318">USE hive.default; </pre>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p19723186112817">Creating a materialized view</p>
<pre class="screen" id="mrs_01_24545__en-us_topic_0000001521198793_screen87072915331">CREATE MATERIALIZED VIEW mv.default.mv1 WITH(mv_storage_table='hive.default.mv11') AS SELECT id FROM t1;</pre>
<p id="mrs_01_24545__en-us_topic_0000001521198793_p121165123216">SELECT query</p>
<pre class="screen" id="mrs_01_24545__en-us_topic_0000001521198793_screen6274360332">SELECT id FROM t1;</pre>
</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_24533.html">Using HetuEngine Materialized Views</a></div>
</div>
</div>