<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/Internal+Materialization">Internal Materialization</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</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" >When the updatable option is not specified, accessing the materialized view table is more efficient because modifications do not need to be considered. Therefore, only specify the updatable option if row based incremental updates are needed.  Even when performing row updates, full snapshot refreshes may be needed to ensure consistency. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The {{EventDistributor}} also <span class="diff-changed-words">expose<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">d</span><span class="diff-added-chars"style="background-color: #dfd;">s</span></span> the updateMatViewRow as a lower level API <span class="diff-added-words"style="background-color: #dfd;">for [Programmatic Control]</span> - care should be taken when using this update method. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h1. Secondary Indexes <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. Clustering Considerations <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Internal materialization creates Teiid temporary tables to hold the materialized table.  While these tables are not fully durable, they perform well in most circumstances and the data is present at each Teiid instance which removes the single point of failure and network overhead of an external database. Internal materialization also provides more built-in facilities for refreshing and monitoring.</p>

<p>The <a href="/author/display/TEIID/Hints+and+Options#HintsandOptions-CacheHint">Cache Hint</a>, when used in the context of an internal materialized view transformation query, provides the ability to fine tune the materialized table. The pref_mem option also applies to internal materialized views.  Internal table index pages already have a memory preference, so the perf_mem option indicates that the data pages should prefer memory as well.</p>

<p>All internal materialized view refresh and updates happen atomically.  Internal materialized views support READ_COMMITTED (used also for READ_UNCOMMITED) and SERIALIZABLE (used also for REPEATABLE_READ) transaction isolation levels.</p>


<h1><a name="InternalMaterialization-LoadingAndRefreshing"></a>Loading And Refreshing</h1>

<h3><a name="InternalMaterialization-UsingSystemProcedure"></a>Using System Procedure</h3>

<p>An internal materialized view table is initially in an invalid state (there is no data).  The first user query will trigger an implicit loading of the data. All other queries against the materialized view will block until the load completes. In some situations administrators may wish to better control when the cache is loaded with a call to <tt>SYSADMIN.refreshMatView</tt>.  The initial load may itself trigger the initial load of dependent materialized views.  After the initial load user queries against the materialized view table will only block if it is in an invalid state. The valid state may also be controlled through the <tt>SYSADMIN.refreshMatView</tt> procedure. <div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Invalidating Refresh</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">CALL SYSADMIN.refreshMatView(viewname=&gt;'schema.matview', invalidate=&gt;true)</pre>
</div></div><br/>
matview will be refreshed and user queries will block until the refresh is complete (or fails).</p>

<p>While the initial load may trigger a transitive loading of dependent materialized views, subsequent refreshes performed with <tt>refreshMatView</tt> will use dependent materialized view tables if they exist.  Only one load may occur at a time.  If a load is already in progress when the <tt>SYSADMIN.refreshMatView</tt> procedure is called, it will return &#45;1 immediately rather than preempting the current load.</p>


<h3><a name="InternalMaterialization-UsingTTLSnapshotRefresh"></a>Using TTL Snapshot Refresh</h3>

<p>The <a href="/author/display/TEIID/Hints+and+Options#HintsandOptions-CacheHint">Cache Hint</a> may be used to automatically trigger a full snapshot refresh after a specified time to live (ttl). The ttl starts from the time the table is finished loading. The refresh is equivalent to <tt>CALL SYSADMIN.refreshMatView('view name', false)</tt>, but performed asynchronously so that user queries do not block on the load.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Auto-refresh Transformation Query</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
/*+ cache(ttl:3600000) */ select t.col, t1.col from t, t1 where t.id = t1.id
</pre>
</div></div>

<p>The resulting materialized view will be reloaded every hour (3600000 milliseconds).</p>


<h4><a name="InternalMaterialization-Limitations"></a>Limitations</h4>

<ul>
        <li>The automatic ttl refresh is not intended for complex loading scenarios, as nested materialized views will be used by the refresh query.</li>
</ul>


<ul>
        <li>The automatic ttl refresh is performed lazily, that is it is only trigger by using the table after the ttl has expired. For infrequently used tables with long load times, this means that data may be used well past the intended ttl.</li>
</ul>


<h2><a name="InternalMaterialization-Updatable"></a>Updatable</h2>

<p>In advanced use-cases the cache hint may also be used to mark an internal materialized view as updatable. An updatable internal materialized view may use the <tt>SYSADMIN.refreshMatViewRow</tt> procedure to update a single row in the materialized table. If the source row exists, the materialized view table row will be updated.  If the source row does not exist, the correpsonding materialized row will be deleted. To be updatable the materialized view must have a single column primary key.  Composite keys are not yet supported by <tt>SYSADMIN.refreshMatViewRow</tt>. Transformation Query: <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">/*+ cache(updatable) */ select t.col, t1.col from t, t1 where t.id = t1.id</pre>
</div></div></p>

<p>Update SQL:</p>

<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;">
CALL SYSADMIN.updateMatViewRow(viewname=&gt;'schema.matview', key=&gt;5)
</pre>
</div></div>

<p>Given that the schema.matview defines an integer column col as its primary key, the update will check the live source(s) for the row values.</p>

<p>The update query will not use dependent materialized view tables, so care should be taken to ensure that getting a single row from this transformation query performs well.  See the Reference Guide for information on controlling dependent joins, which may be applicable to increasing the performance of retrieving a single row. The refresh query does use nested caches, so this refresh method should be used with caution.</p>

<p>When the updatable option is not specified, accessing the materialized view table is more efficient because modifications do not need to be considered. Therefore, only specify the updatable option if row based incremental updates are needed.  Even when performing row updates, full snapshot refreshes may be needed to ensure consistency.</p>

<p>The <tt>EventDistributor</tt> also exposes the updateMatViewRow as a lower level API for <a href="/author/display/TEIID/Programmatic+Control" title="Programmatic Control">Programmatic Control</a> - care should be taken when using this update method.</p>

<h1><a name="InternalMaterialization-SecondaryIndexes"></a>Secondary Indexes</h1>

<p>Internal materialized view tables will automatically create non-unique indexes for each unique constraint and index defined on the materialized view. These indexes are created as non-unique even for unique constraints since the materialized table is not intended as an enforcement point for data integrity and when updatable the table may not be consistent with underlying values and thus unable to satisfy constraints.  The primary key (if it exists) of the view will automatically be part of the covered columns for the index.</p>

<p>The secondary indexes are always created as trees - bitmap or hash indexes are not supported.  Teiid's metadata for indexes is currently limited. We are not currently able to capture additional information, sort direction, additional columns to cover, etc.  You may workaround some of these limitations though.</p>

<ul>
        <li>Function based index are supported, but can only be specified through DDL metadata.&nbsp; If you are not using DDL metadata, consider adding another column to the view that projects the function expression, then place an index on that new column. Queries to the view will need to be modified as appropriate though to make use of the new column/index.</li>
</ul>


<ul>
        <li>If additional covered columns are needed, they may simply be added to the index columns.  This however is only applicable to comparable types. Adding additional columns will increase the amount of space used by the index, but may allow its usage to result in higher performance when only the covered columns are used and the main table is not consulted.</li>
</ul>



<h1><a name="InternalMaterialization-ClusteringConsiderations"></a>Clustering Considerations</h1>

<p>Each member in a cluster maintains its own copy of each materialized table and associated indexes. An attempt is made to ensure each member receives the same full refresh events as the others. Full consistency for updatable materialized views however is not guaranteed. Periodic full refreshes of updatable materialized view tables helps ensure consistency among members.</p>
    </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/Internal+Materialization">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646103&revisedVersion=8&originalVersion=7">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/Internal+Materialization?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>