<html>
<head>
    <base href="https://docs.jboss.org/author">
            <link rel="stylesheet" href="/author/s/en/2172/19/5/_/styles/combined.css?spaceKey=TEIID&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://docs.jboss.org/author/display/TEIID/External+Materialization">External Materialization</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~kylin">Kylin Soong</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >| teiid_rel:MATVIEW_AFTER_LOAD_SCRIPT | semi-colon(\;) separated DDL/DML commands to run after the actual load of the cache. Typically used to rename staging table to actual cache table. Required when MATVIEW_LOAD_SCRIPT not defined to copy data from teiid_rel:MATVIEW_STAGE_TABLE to MATVIEW table | true | When not defined, no script will be run | <br>| teiid_rel:MATVIEW_SHARE_SCOPE | Allowed values are \{NONE, VDB, SCHEMA\}, which define if the cached contents are shared among different VDB versions and different VDBs as long as schema names match | true | NONE | <br></td></tr>
            <tr><td class="diff-unchanged" >| teiid_rel:MATERIALIZED_STAGE_TABLE | When MATVIEW_LOAD_SCRIPT property not defined, Teiid loads the cache contents into this table. Required when MATVIEW_LOAD_SCRIPT not defined | false \\ | n/a | <br></td></tr>
            <tr><td class="diff-unchanged" >| teiid_rel:ON_VDB_START_SCRIPT | DML commands to run start of vdb | true | n/a | <br>| teiid_rel:ON_VDB_DROP_SCRIPT | DML commands to run at VDB un-deploy; typically used for cleaning the cache/status tables | true | n/a | <br>| teiid_rel:MATVIEW_ONERROR_ACTION | Action to be taken when mat view contents are requested but cache is invalid. Allowed values are (THROW_EXCEPTION = throws an exception, IGNORE = ignores the warning and supplied invalidated data, WAIT = waits until the data is refreshed and valid then provides the updated data) | true | WAIT | <br></td></tr>
            <tr><td class="diff-changed-lines" >| teiid_rel:MATVIEW_TTL | time to live in milliseconds. Provide property or cache hint on view transformation - property takes precedence. | <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">true</span> <span class="diff-added-words"style="background-color: #dfd;">false</span> | The table will not refresh | <br></td></tr>
            <tr><td class="diff-unchanged" > <br>* Once the VDB with a model with above properties defined and deployed, the following sequence of events will take place <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>External materialized views cache their data in an external database system.  External materialized views give the administrator full control over the loading and refresh strategies.</p>

<p>Starting from Teiid version 8.5, Teiid gives the option to configure metadata on the view to control the simple load and refresh policies. Administrators of Teiid system are free to manage their external materialization table through other means based on their needs.</p>

<h2><a name="ExternalMaterialization-UserManagedMaterializationManagement"></a>User Managed Materialization Management</h2>

<p><b>Typical Usage Steps</b></p>

<ul>
        <li>Create materialized views and corresponding physical materialized target tables in Designer.  This can be done through setting the materialized and target table manually, or by selecting the desired views, right clicking, then selecting Modeling-&gt;"Create Materialized Views"</li>
</ul>


<ul>
        <li>Generate the DDL for your physical model materialization target tables.  This can be done by selecting the model, right clicking, then choosing Export-&gt;"Metadata Modeling"-&gt;"Data Definition Language (DDL) File". This script can be used to create the desired schema for your materialization target on whatever source you choose.</li>
</ul>


<ul>
        <li>Determine a load and refresh strategy.  With the schema created the most simplistic approach is to just load the data. The load can even be done through Teiid with</li>
</ul>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">insert into target_table select * from matview option nocache matview</pre>
</div></div>

<p>That however may be too simplistic because your index creation may be more performant if deferred until after the table has been created. Also full snapshot refreshes are best done to a staging table then swapping it for the existing physical table to ensure that the refresh does not impact user queries and to ensure that the table is valid prior to use.</p>

<h2><a name="ExternalMaterialization-MetadataBasedMaterializationManagement"></a>Metadata Based Materialization Management</h2>

<p>Users when they are designing their views, they can define additional metadata on their views to control the loading and refreshing of external materialization cache. This option provides a limited but a powerful way to manage the materialization views. For this purpose, <a href="https://docs.jboss.org/author/display/TEIID/System+Schema" class="external-link" rel="nofollow">SYSADMIN Schema</a> model in your VDB defines three stored procedures (loadMatView, updateMatView, matViewStatus) in its schema. Based on the defined metadata on the view, and these <a href="https://docs.jboss.org/author/display/TEIID/System+Procedures" class="external-link" rel="nofollow">SYSADMIN procedures</a> a simple scheduler automatically starts during the VDB deployment and loads and keeps the cache fresh.</p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/author/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td>The Designer tooling for this feature is lacking at this moment but this will be added in coming releases.</td></tr></table></div>

<p><b>Usage Steps</b></p>

<ul>
        <li>To manage and report the loading and refreshing activity of materialization view, Teiid expects the user to define "Status" table with following schema in any one of the source models. Create this table on the physical database, before you do the import of this physical source.</li>
</ul>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
CREATE TABLE status
(
  VDBName varchar(50) not null,
  VDBVersion integer not null,
  SchemaName varchar(50) not null,
  Name varchar(256) not null,
  TargetSchemaName varchar(50),
  TargetName varchar(256) not null,
  Valid boolean not null,
  LoadState varchar(25) not null,
  Cardinality long,
  Updated timestamp not null,
  LoadNumber long not null,
  PRIMARY KEY (VDBName, VDBVersion, SchemaName, Name)
);
</pre>
</div></div>

<ul>
        <li>Create Views and corresponding physical materialized target tables in Designer or using DDL in Dynamic VDB.  This can be done through setting the materialized and target table manually, or by selecting the desired views, right clicking, then selecting Modeling-&gt;"Create Materialized Views" in the Designer.</li>
</ul>


<ul>
        <li>Define the following extension properties on the view.</li>
</ul>


<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Property Name </th>
<th class='confluenceTh'> Description </th>
<th class='confluenceTh'> Optional </th>
<th class='confluenceTh'> Default Value </th>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:ALLOW_MATVIEW_MANAGEMENT </td>
<td class='confluenceTd'> Allow Teiid based management </td>
<td class='confluenceTd'> false </td>
<td class='confluenceTd'> false </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_STATUS_TABLE </td>
<td class='confluenceTd'> fully qualified Status Table Name defined above </td>
<td class='confluenceTd'> false </td>
<td class='confluenceTd'> n/a </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_BEFORE_LOAD_SCRIPT </td>
<td class='confluenceTd'> semi-colon(;) separated DDL/DML commands to run before the actual load of the cache, typically used to truncate staging table </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> When not defined, no script will be run </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_LOAD_SCRIPT </td>
<td class='confluenceTd'> semi-colon(;) separated DDL/DML commands to run for loading of the cache </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> will be determined based on view transformation </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_AFTER_LOAD_SCRIPT </td>
<td class='confluenceTd'> semi-colon(;) separated DDL/DML commands to run after the actual load of the cache. Typically used to rename staging table to actual cache table. Required when MATVIEW_LOAD_SCRIPT not defined to copy data from teiid_rel:MATVIEW_STAGE_TABLE to MATVIEW table </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> When not defined, no script will be run </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_SHARE_SCOPE </td>
<td class='confluenceTd'> Allowed values are {NONE, VDB, SCHEMA}, which define if the cached contents are shared among different VDB versions and different VDBs as long as schema names match </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> NONE </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATERIALIZED_STAGE_TABLE </td>
<td class='confluenceTd'> When MATVIEW_LOAD_SCRIPT property not defined, Teiid loads the cache contents into this table. Required when MATVIEW_LOAD_SCRIPT not defined </td>
<td class='confluenceTd'> false <br class="atl-forced-newline" /> </td>
<td class='confluenceTd'> n/a </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:ON_VDB_START_SCRIPT </td>
<td class='confluenceTd'> DML commands to run start of vdb </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> n/a </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:ON_VDB_DROP_SCRIPT </td>
<td class='confluenceTd'> DML commands to run at VDB un-deploy; typically used for cleaning the cache/status tables </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> n/a </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_ONERROR_ACTION </td>
<td class='confluenceTd'> Action to be taken when mat view contents are requested but cache is invalid. Allowed values are (THROW_EXCEPTION = throws an exception, IGNORE = ignores the warning and supplied invalidated data, WAIT = waits until the data is refreshed and valid then provides the updated data) </td>
<td class='confluenceTd'> true </td>
<td class='confluenceTd'> WAIT </td>
</tr>
<tr>
<td class='confluenceTd'> teiid_rel:MATVIEW_TTL </td>
<td class='confluenceTd'> time to live in milliseconds. Provide property or cache hint on view transformation - property takes precedence. </td>
<td class='confluenceTd'> false </td>
<td class='confluenceTd'> The table will not refresh </td>
</tr>
</tbody></table>
</div>


<ul>
        <li>Once the VDB with a model with above properties defined and deployed, the following sequence of events will take place</li>
</ul>


<ol>
        <li>Upon the VDB deployment, teiid_rel:ON_VDB_START_SCRIPT will be run on completion of the deployment.</li>
        <li>Based on the teiid_rel:MATVIEW_TTL defined a scheduler entry will be created to run SYSADMIN.loadMatView procedure, which loads the cache contents.</li>
        <li>This procedure, first inserts/updates a entry in teiid_rel:MATVIEW_STATUS_TABLE, which indicates that the cache is being loaded.</li>
        <li>In same procedure, then teiid_rel:MATVIEW_BEFORE_LOAD_SCRIPT will be run if defined</li>
        <li>In same procedure, then teiid_rel:MATVIEW_LOAD_SCRIPT will be run if defined, otherwise one will be automatically created based on the view's transformation logic.</li>
        <li>Then, teiid_rel:MATVIEW_AFTER_LOAD_SCRIPT will be run, to close out and create any indexes on the mat view table.</li>
        <li>The procedure, then will set teiid_rel:MATVIEW_STATUS_TABLE entry to "LOADED" and valid.</li>
        <li>Based on the teiid_rel:MATVIEW_TTL, the SYSADMIN.matViewStatus is ran by the Scheduler, to queue further cache re-loads.</li>
        <li>When VDB is un-deployed (not when server is restarted) the teiid_rel:ON_VDB_DROP_SCRIPT script will be run.</li>
</ol>


<p>User can any time run SYSADMIN.updateMatView procedure to partially update the cache contents rather than complete refresh of contents with SYSADMIN.loadMatview procedure. When partial update is run the cache expiration time is renewed for new term based on Cache Hint again.</p>

<p>A sample Dynamic VDB with these properties can be defined as below</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: xml; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;vdb name="sakila" version="1"&gt;
    &lt;description&gt;Shows how to call JPA entities&lt;/description&gt;

    &lt;model name="pg"&gt;
        &lt;source name="pg" translator-name="postgresql-override" connection-jndi-name="java:/sakila-ds"/&gt;
    &lt;/model&gt;

    &lt;model name="sakila" type="VIRTUAL"&gt;
    &lt;metadata type="DDL"&gt;&lt;![CDATA[
        CREATE VIEW actor (
           actor_id integer,
           first_name varchar(45) NOT NULL,
           last_name varchar(45) NOT NULL,
           last_update timestamp NOT NULL
        ) OPTIONS (MATERIALIZED 'TRUE', UPDATABLE 'TRUE',
                MATERIALIZED_TABLE 'pg.public.mat_actor',
                "teiid_rel:MATERIALIZED_STAGE_TABLE" 'pg.public.mat_actor_staging',
                "teiid_rel:ALLOW_MATVIEW_MANAGEMENT" 'true',
                "teiid_rel:MATVIEW_STATUS_TABLE" 'pg.public.status',
                "teiid_rel:MATVIEW_BEFORE_LOAD_SCRIPT" 'execute pg.native(''truncate table mat_actor_staging'');',
                "teiid_rel:MATVIEW_AFTER_LOAD_SCRIPT" 'execute pg.native(''ALTER TABLE mat_actor RENAME TO mat_actor_temp'');execute pg.native(''ALTER TABLE mat_actor_staging RENAME TO mat_actor'');execute pg.native(''ALTER TABLE mat_actor_temp RENAME TO mat_actor_staging;'')',
                "teiid_rel:MATVIEW_SHARE_SCOPE" 'NONE',
                "teiid_rel:MATVIEW_ONERROR_ACTION" 'THROW_EXCEPTION',
                "teiid_rel:MATVIEW_TTL" 300000,
                "teiid_rel:ON_VDB_DROP_SCRIPT" 'DELETE FROM pg.public.status WHERE Name=''actor'' AND schemaname = ''sakila''')
            AS SELECT actor_id, first_name, last_name, last_update from pg."public".actor;
     ]]&gt;
    &lt;/metadata&gt;
    &lt;/model&gt;
    &lt;translator name="postgresql-override" type="postgresql"&gt;
        &lt;property name="SupportsNativeQueries" value="true"/&gt;
    &lt;/translator&gt;
&lt;/vdb&gt;
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://docs.jboss.org/author/users/removespacenotification.action?spaceKey=TEIID">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://docs.jboss.org/author/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://docs.jboss.org/author/display/TEIID/External+Materialization">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646102&revisedVersion=18&originalVersion=17">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/External+Materialization?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>