<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/Code+Table+Caching">Code Table Caching</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</a>
    </h4>
        <br/>
                         <h4>Changes (8)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >Teiid provides a short cut to creating an internal materialized view table via the _lookup_ function. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The _lookup_ function provides a way to get a value out of a table when a key value is provided.  The function automatically caches all the values in the referenced table for the specified key/value pairs.  The cache is created the first time it is used in a particular Teiid process.  Subsequent lookups against the same table using the same key and value columns will use the cached information. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The _lookup_ function provides a way to accelerate getting a value out of a table when a key value is provided.  The function automatically caches all of the key/return pairs for the referenced table.  This caching is performed on demand, but will proactively load the results to other members in a cluster.  Subsequent lookups against the same table using the same key and return columns will use the cached information. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">This caching solution is appropriate for integration of &quot;reference data&quot; with transactional or operational data.  Reference data are static data sets – typically small – which are used very frequently in most enterprise applications.  Examples are ISO country codes, state codes, and different types of financial instrument identifiers. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">This caching solution is appropriate for integration of &quot;reference data&quot; with transactional or operational data.  Reference data is usually static and small data sets that are used frequently.  Examples are ISO country codes, state codes, and different types of financial instrument identifiers. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Usage <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >This caching mechanism is automatically invoked when the lookup scalar function is used.  The lookup function returns a scalar value, so it may be used anywhere an expression is expected.  Each time this function is called with a unique combination of referenced table, return column, and key column \(the first 3 arguments to the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">function), the Teiid System caches the entire contents of the table being accessed.  Subsequent lookup function calls with the same combination of parameters will use the cached table data.</span> <span class="diff-added-words"style="background-color: #dfd;">function).</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>See the [Lookup Function] in the Reference Guide for more information on use of the _lookup_ function. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* No mechanism is provided to refresh code tables <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">* Only a single key/return column is cached - values will not be session/user specific. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h2. Materialized View Alternative <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Here MatISOCountryCodes is a view selecting from ISOCountryCodes that has been marked as materialized and has a primary key <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">or</span> <span class="diff-added-words"style="background-color: #dfd;">and</span> index on CountryName.  The scalar subquery will use the index to lookup the country code for each country name in tbl. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Reasons to use a materialized view: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* Proper materialized views have built\-in system procedure/table support. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >* More control <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">of</span> <span class="diff-added-words"style="background-color: #dfd;">via</span> the cache hint. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>* The ability to use OPTION NOCACHE. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Teiid provides a short cut to creating an internal materialized view table via the <em>lookup</em> function.</p>

<p>The <em>lookup</em> function provides a way to accelerate getting a value out of a table when a key value is provided.  The function automatically caches all of the key/return pairs for the referenced table.  This caching is performed on demand, but will proactively load the results to other members in a cluster.  Subsequent lookups against the same table using the same key and return columns will use the cached information.</p>

<p>This caching solution is appropriate for integration of "reference data" with transactional or operational data.  Reference data is usually static and small data sets that are used frequently.  Examples are ISO country codes, state codes, and different types of financial instrument identifiers.</p>

<h2><a name="CodeTableCaching-Usage"></a>Usage</h2>

<p>This caching mechanism is automatically invoked when the lookup scalar function is used.  The lookup function returns a scalar value, so it may be used anywhere an expression is expected.  Each time this function is called with a unique combination of referenced table, return column, and key column &#40;the first 3 arguments to the function).</p>

<p>See the <a href="/author/display/TEIID/Lookup+Function" title="Lookup Function">Lookup Function</a> in the Reference Guide for more information on use of the <em>lookup</em> function.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Country Code Lookup</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
lookup('ISOCountryCodes', 'CountryCode', 'CountryName', 'United States')
</pre>
</div></div>

<h2><a name="CodeTableCaching-Limitations"></a>Limitations</h2>

<ul>
        <li>The use of the lookup function automatically performs caching; there is no option to use the lookup function and not perform caching.</li>
</ul>


<ul>
        <li>No mechanism is provided to refresh code tables</li>
</ul>


<ul>
        <li>Only a single key/return column is cached - values will not be session/user specific.</li>
</ul>


<h2><a name="CodeTableCaching-MaterializedViewAlternative"></a>Materialized View Alternative</h2>

<p>The <em>lookup</em> function is a shortcut to create an internal materialized view with an appropriate primary key.  In many situations, it may be better to directly create the analogous materialized view rather than to use a code table.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Country Code Lookup Against A Mat View</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
SELECT (SELECT CountryCode From MatISOCountryCodes WHERE CountryName = tbl.CountryName) as cc FROM tbl
</pre>
</div></div>

<p>Here MatISOCountryCodes is a view selecting from ISOCountryCodes that has been marked as materialized and has a primary key and index on CountryName.  The scalar subquery will use the index to lookup the country code for each country name in tbl.</p>

<p>Reasons to use a materialized view:</p>

<ul>
        <li>More control of the possible return columns. Code tables will create a materialized view for each key/value pair. If there are multiple return columns it would be better to have a single materialized view.</li>
</ul>


<ul>
        <li>Proper materialized views have built&#45;in system procedure/table support.</li>
</ul>


<ul>
        <li>More control via the cache hint.</li>
</ul>


<ul>
        <li>The ability to use OPTION NOCACHE.</li>
</ul>


<ul>
        <li>There is almost no performance difference.</li>
</ul>


<p>Steps to create a materialized view:</p>

<ol>
        <li>Create a view selecting the appropriate columns from the desired table.  In general, this view may have an arbitrarily complicated transformation query.</li>
        <li>Designate the appropriate column&#40;s) as the primary key.  Additional indexes can be added if needed.</li>
        <li>Set the materialized property to true.</li>
        <li>Add a cache hint to the transformation query.  To mimic the behavior of the implicit internal materialized view created by the lookup function, use the <a href="/author/display/TEIID/Hints+and+Options#HintsandOptions-CacheHint">Hints and Options</a> <tt>/&#42;+ cache&#40;pref&#95;mem) &#42;/</tt> to indicate that the table data pages should prefer to remain in memory.</li>
</ol>


<p>Just as with the lookup function, the materialized view table will be created on first use and reused subsequently.  See the <a href="/author/display/TEIID/Materialized+Views" title="Materialized Views">Materialized Views</a> for more.</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/Code+Table+Caching">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=18646104&revisedVersion=5&originalVersion=4">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/Code+Table+Caching?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>