<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % CustomDTD SYSTEM "../../../../../../docbook/custom.dtd">
%CustomDTD;
]>
<chapter id="codetable">
	<title>Code Table Caching</title>
	<para>Teiid provides a short cut to creating an internal materialized view table via the lookup function.</para>
	<para>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.</para>
	<para>This caching solution is appropriate for integration of
		"reference data" 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.</para>
	<section>
		<title>User Interaction</title>
		<para>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, key element, and returned
			element (the first 3 arguments to the function), the Teiid
			System caches the entire contents of the table being accessed.
			 Subsequent lookup function uses with the same combination of
			parameters uses the cached table data.</para>
		<para>See the Reference for more information on use of the lookup function.</para>
		<example>
			<title>Country Code Lookup</title>
			<programlisting>lookup('ISOCountryCodes', 'CountryName', 'CountryCode', 'US')</programlisting>
		</example>
	</section>
	<section>
		<title>Limitations</title>
		<itemizedlist>
			<listitem>
				<para>The use of the lookup function automatically performs caching;
					there is no option to use the lookup function and not perform
					caching.</para>
			</listitem>
			<listitem>
				<para>No mechanism is provided to refresh code tables.</para>
			</listitem>
		</itemizedlist>
	</section>
	<section>
		<title>Materialized View Alternative</title>
		<para>The lookup 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.</para>
		<example>
			<title>Country Code Lookup Against A Mat View</title>
			<programlisting>SELECT (SELECT CountryCode From MatISOCountryCodes WHERE CountryName = tbl.CountryName) as cc FROM tbl</programlisting>
			<para>Here MatISOCountryCodes is a view selecting from ISOCountryCodes that has been marked as materialized and has a primary key or index on CountryName.  The scalar subquery will use the index to lookup the country code for each country name in tbl.</para>
		</example>
		<itemizedlist>
			<title>Reasons to use a materialized view:</title>
			<listitem>
				<para>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.</para>
			</listitem>
			<listitem>
				<para>Proper materialized views have built-in system procedure/table support.</para>
			</listitem>
			<listitem>
				<para>More control of the cache hint.</para>
			</listitem>
			<listitem>
				<para>The ability to use <link linkend="nocache">OPTION NOCACHE</link>.</para>
			</listitem>
			<listitem>
				<para>There is almost no performance difference.</para>
			</listitem>
		</itemizedlist>
		<orderedlist>
			<title>Steps to create a materialized view:</title>
			<listitem>
				<para>Create a view selecting the appropriate columns from the desired table.  In general, this view may have an arbitrarily complicated transformation query.</para>
			</listitem>
			<listitem>
				<para>Designate the appropriate column(s) as the primary key.  Additional indexes can be added if needed.</para>
				</listitem>
			<listitem>
				<para>Set the materialized property to true.</para>
				</listitem>
			<listitem>
				<para>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 <link linkend="cache-hint">cache hint</link> <code>/*+ cache(pref_mem) */</code> to indicate that the table data pages should prefer to remain in memory.</para>
			</listitem>
		</orderedlist>
		<para>Just as with the lookup function, the materialized view table will be created on first use and reused subsequently.  See the <link linkend="matviews">Materialized View Chapter</link> for more on materialized views.</para>
	</section>
</chapter>
