JBoss.orgCommunity Documentation

Chapter 4. Materialized Views

4.1. Support Summary
4.2. User Interaction
4.3. Materialized View Definition
4.4. External Materialization
4.5. Internal Materialization
4.5.1. Loading And Refreshing
4.5.2. Limitations

Teiid supports materialized views.  Materialized views are just like other views, but their transformations are pre-computed and stored just like a regular table. When queries are issued against the views through the Teiid Server, the cached results are used.  This saves the cost of accessing all the underlying data sources and re-computing the view transforms each time a query is executed.

Materialized views are appropriate when the underlying data does not change rapidly, or when it is acceptable to retrieve data that is "stale" within some period of time, or when it is preferred for end-user queries to access staged data rather than placing additional query load on operational sources.

When client applications issue queries against a Relational table or view that has been defined as a materialized view, the Teiid query engine automatically routes that query to obtain the results from the cache database.

Individual queries may override the use of materialized views by specifying OPTION NOCACHE on the query.  This parameter may specify one or more virtual groups to override (separated by commas, spaces optional).  If no virtual groups are specified, materialized views tables will not be used transitively.



Option NOCACHE may be specified in virtual group transformation queries.  In that way, transformations can specify to always use real-time data obtained directly from a source.  The use of caching and non-caching can be mixed in transformation definitions, just as with user queries.

Materialized views are defined in Teiid Designer by setting the materialized property on a table or view in a virtual (view) relational model.  Setting this property's value to true (the default is false) allows the data generated for this virtual table to be treated as a materialized view.

The target materialized table may also be set in the properties. If the value is left blank, the default, then internal materialization will be used. Otherwise for external materialization, the value should reference the fully qualified name of a table (or possibly view) with the same columns as the materialized view. For most basic scenarios the simplicity of internal materialization makes it the more appealing option. Other considerations for chosing between internal and external materialization are:

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.

Since the actual physical cache for materialized views is maintained external to the Teiid system, there is no pre-defined policy for clearing and managing the cache.  These policies will be defined and enforced by administrators of the Teiid system.

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.

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 SYS.refreshMatView. 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 controled through the SYS.refreshMatView procedure.


While the initial load may trigger a transitive loading of dependent materialized views, subsequent refreshes performed with refreshMatView 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 SYS.refreshMatView procedure is called, it will return -1 immediately rather than preempting the current load.

The cache hint may be used to automatically trigger a full snapshot refresh after a specified time to live.


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 SYS.refreshMatViewRow procedure to update a single row in the materialized table. To be updatable the materialized view must have a single column primary key. Composite keys are not yet supported by SYS.refreshMatViewRow.


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. This may require the use of depedent join hints. 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.