이 콘텐츠는 선택한 언어로 제공되지 않습니다.
3.3. External Materialization
3.3.1. Use External Materialization 링크 복사링크가 클립보드에 복사되었습니다!
Procedure 3.2. Use External Materialization
- Build a VDB using the Teiid Designer for your use case.
- Identify all the "Virtual Tables", that you think can use caching,
- Click on the table, then in the Properties panel, switch the Materialized property to "true".
- Right click on each materialized table, then choose- .
- Click on Materialization Model input box.button on the
- Select a "physical model" that already exists or create a new name for "physical model".
- Click.This will create the new model (if applicable) and a table with exact schema as your selected virtual table.
- Verify that the "Materialization Table" property is now updated with name of table that has just been created.
- Navigate to the new materialized table that has been created, and click on "Name In Source" property and change it from "MV1000001" to "mv_{your_table_name}". Typically this could be same name as your virtual table name, (for example, you might name it "mv_UpdateProduct".)
- Save your model.
Note
The data source this materialized view physical model represents will be the data source for storing the materialized tables. You can select different "physical models" for different materialized tables, creating multiple places to store your materialized tables. - Once you are have finished creating all materialized tables, right click on each model (in most cases this will be a single physical model used for all the materialized views), then use- - to generate the DDL for the physical model.
- Select the type of the database and DDL file name and click.A DDL file that contains all of the "create table" commands is generated..
- Use your favorite "client" tool for your database and create the database using the DDL file created.
- Go back to your VDB and configure the data source and translator for the "materialized" physical model to the database you just created.
- Once finished, deploy the VDB to Teiid Server and make sure that it is correctly configured and active.
Important
3.3.2. External Materialization Usage Steps 링크 복사링크가 클립보드에 복사되었습니다!
insert into target_table select * from matview option nocache matview
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, SYSADMIN Schema model in your VDB defines three stored procedures (loadMatView, updateMatView, matViewStatus) in its schema. Based on the defined metadata on the view, and these SYSADMIN procedures a simple scheduler automatically starts during the VDB deployment and loads and keeps the cache fresh.
Note
Property | Description | Optional | Default |
---|---|---|---|
teiid_rel:ALLOW_MATVIEW_MANAGEMENT |
Allow Teiid based management
|
False
|
False
|
teiid_rel:MATVIEW_STATUS_TABLE |
fully qualified Status Table Name defined above
|
False
|
NA
|
teiid_rel:MATVIEW_BEFORE_LOAD_SCRIPT |
semi-colon(;) separated DDL/DML commands to run before the actual load of the cache, typically used to truncate staging table
|
True
|
When not defined, no script will be run
|
teiid_rel:MATVIEW_LOAD_SCRIPT |
semi-colon(;) separated DDL/DML commands to run for loading of the cache
|
True
|
will be determined based on view transformation
|
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 is not defined in order to copy data from the teiid_rel:MATVIEW_STAGE_TABLE the MATVIEW table.
|
True
|
When not defined, no script will be run
|
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
|
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
|
NA
|
teiid_rel:ON_VDB_START_SCRIPT |
DML commands to run start of vdb
|
True
|
NA
|
teiid_rel:ON_VDB_DROP_SCRIPT |
DML commands to run at VDB un-deploy; typically used for cleaning the cache/status tables
|
True
|
NA
|
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
|
teiid_rel:MATVIEW_TTL |
time to live in milliseconds. Provide property or cache hint on view transformation - property takes precedence.
|
True
|
2^63 milliseconds - effectively the table will not refresh, but will be loaded a single time initially
|
Note
3.3.3. External Materialization Options 링크 복사링크가 클립보드에 복사되었습니다!
INSERT INTO mv_view.mv_UpdateProduct SELECT * FROM Portfolio.UpdateProduct OPTION NOCACHE
sql=connect(${url}, ${user}, ${password}); sql.execute("DELETE FROM mv_view.mv_UpdateProduct"); sql.execute("INSERT INTO mv_view.mv_UpdateProduct SELECT * FROM Portfolio.UpdateProduct OPTION NOCACHE"); sql.close();
sql=connect(${url}, ${user}, ${password});
sql.execute("DELETE FROM mv_view.mv_UpdateProduct");
sql.execute("INSERT INTO mv_view.mv_UpdateProduct SELECT * FROM Portfolio.UpdateProduct OPTION NOCACHE");
sql.close();
adminshell.sh . load.groovy
Note
- If you want to set up a job to run this script frequently at regular intervals, then on Red Hat Enterprise Linux use "cron tab" or on Microsoft Windows use "Windows Scheduler" to refresh the rows in the materialized table. Every time the script runs it will refresh the contents.
- This job needs to be run only when user access is restricted.
Important
- If it is updating all the rows in the materialized table, and you only need to update only few rows to avoid long refresh time.
- If it takes an hour for your reload your materialized table, queries executed during that time will fail to povide correct results.
- Also ensure that you create indexes on your materialization table after the data is loaded, as having indexes during the load process slows down the loading of data, especially when you are dealing with a large number of rows.
3.3.4. Set Up a Staging Table 링크 복사링크가 클립보드에 복사되었습니다!
Procedure 3.3. Set Up a Staging Table
- Build a VDB using the Teiid Designer for your use case.
- Identify all the "Virtual Tables", that you think can use caching,
- Click on the table, then in the Properties panel, switch the Materialized property to "true".
- Right click on each materialized table, then choose- .
- Click on Materialization Model input box.button on the
- Select a "physical model" that already exists or create a new name for "physical model".
- Click.This will create the new model (if applicable) and a table with exact schema as your selected virtual table.
- Verify that the "Materialization Table" property is now updated with name of table that has just been created.
- Navigate to the new table materialized table that has been created, and click on "Name In Source" property.
- Create two identical tables, one with "mv_" prefix and another with "st_" prefix. ("mv_" will stand for materialized view and "st_" will stand for the staging table.)
- Make sure that the "Materialized Table" property is set to the table with "mv_" prefix. Also make sure that both these tables have their "Name in Source" edited and renamed with respective prefixed name.
- Create the DDL file and the database. (The difference now is there are two identical tables for each materialized view.)
- Load the contents into staging table using this query (substitute with the names of your own tables):
INSERT INTO mv_view.ST_UpdateProduct SELECT * FROM Portfolio.UpdateProduct OPTION NOCACHE
- Once the table is loaded, use the database's "RENAME" table command to rename the staging table ( st_xxx ) to the materialized table ( mv_xxx ). Here is the syntax to do this with MySQL:
ALTER TABLE ST_UpdateProduct RENAME MV_UpdateProduct
The reason you need to run the "rename" command is that the staging table can be used for loading the data (except for the very first time you use it). Meanwhile, materialized table will serve the user queries. Once the staging is loaded, rename will switch the identity of staged with the materialized such that any future queries will be against newly loaded contents. You can keep repeating this cycle in regular intervals and never serve empty results or wait for results to load. To populate the results via an ETL tool, you can do so too in this step by disregarding the above SQL command.Note
You can script this entire process so that it runs at a regular interval. This example code shows you how to script it if you are using the MySQL database. (Make sure you provide the MySQL jdbc driver in the AdminShell classpath before running the script):Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the script to your cron tab or Windows Scheduler and set it to run at a regular interval to keep your data fresh.