PIP Caching
    • 15 Aug 2024
    • 6 Minutes to read
    • Dark
      Light
    • PDF

    PIP Caching

    • Dark
      Light
    • PDF

    Article summary

    Introduction

    The PIP Service provides several capabilities for caching data including materialized views. This is used to significantly improve performance in many situations.

    In-Memory Cache

    To create an in-memory cache, simply add the relevant options as part of the view definition.

    Example:

    CREATE View v_Person (
        id varchar,
        name varchar,
        dob date,
        PRIMARY KEY (id)
    ) OPTIONS (
        UPDATABLE 'false', materialized true,  
        "teiid_rel:ALLOW_MATVIEW_MANAGEMENT" 'true',
        "teiid_rel:MATVIEW_POLLING_QUERY" '(SELECT NOW() from SYSADMIN.MatViews where Name = ''v_aad_users'' AND LoadState = ''FAILED_LOAD'')
            UNION
            (SELECT Updated FROM SYSADMIN.MatViews where Name = ''v_aad_users'' AND LoadState = ''LOADED'')',
        "teiid_rel:MATVIEW_POLLING_INTERVAL" 30000
    ) as select * from <USER>.Users;
    
    Property NameDescriptionOptionalDefault
    materializedSet for the view to be materializedfalsetrue
    UPDATABLEAllow updating Materialized View through the DML UPDATE.truefalse
    teiid_rel:ALLOW_MATVIEW_MANAGEMENTAllow PIP based management of the TTL and initial load rather than the implicit behavior.truefalse
    teiid_rel:MATVIEW_PREFER_MEMEORYSame as the pref_mem cache hint option.truefalse
    teiid_rel:MATVIEW_TTLTrigger a Scheduled ExecutorService which execute refresh repeatedly with a specified time to live.truenull
    teiid_rel:MATVIEW_POLLING_QUERYThis property defines a query that must return a single timestamp value. If the value is greater than the last update time of the materialization table, it will be reloaded.*true--
    teiid_rel:MATVIEW_POLLING_INTERVALThis property defines the polling interval, in milliseconds, used with the polling query.*true--

    *Note: See Polling Query and Interval Properties for more information

    Polling Query and Interval Properties

    The Polling Query and Interval properties provide a mechanism to continuously attempt to refresh the cache until successful, avoiding data availability issues. A cache refresh helps ensure that the Materialized Views reflect the most current and accurate data, which is essential for maintaining data consistency and availability for the Authorization process. These properties allow users to handle memory cache failure scenarios like connectivity issues with the underlying data source.
    The PIP Service samples the materialized view status on a regular basis according to the predefined query and interval. The PIP then decides if triggering a refresh is required based on the timestamp returned from the polling query.

    Persistent Cache

    Persistent cache caches the view data to an external physical database system. When a new session issues a query against this view, the request will be redirected to the external physical database system and the cached results will be returned, rather than re-computing. This can prove timesaving and CPU-saving if your query sentences are complex and across multiple, heterogeneous data stores.

    Creating a Persistent Cache:

    1. Create a new Data Source for caching (RDBMS).
    2. Enable Is Transactional.
    3. Model Name: materialized.
    4. Create Status Table DDL.
    CREATE FOREIGN TABLE status
    (
       VDBName varchar(50) not null,
       VDBVersion varchar(50) not null,
       SchemaName varchar(50) not null,
       Name varchar(256) not null,
       TargetSchemaName varchar(50),
       TargetName varchar(256) not null,
       Valid boolean not null,
       LoadState varchar(25) not null,
       Cardinality long,
       Updated timestamp not null,
       LoadNumber long not null,
       NodeName varchar(25) not null,
       StaleCount long,
       PRIMARY KEY (VDBName, VDBVersion, SchemaName, Name)
    );
    
    1. Create a DDL for a Materialized Table

    Example model DDL for External Materialized View.

    CREATE FOREIGN TABLE PersonCached (
        id varchar,
        name varchar,
        dob date,
        LoadNumber long,
        PRIMARY KEY (id)
    ) OPTIONS (UPDATABLE true);
    

    The table should include exactly the same columns and an additional column LoadNumber with the type long. This step is required.

    1. Create a cached view. Go to the Views List and click Manage to add the following view definition.

    Example:

    CREATE View v_Person (
        id varchar,
        name varchar,
        dob date,
        PRIMARY KEY (id)
    ) OPTIONS (
        MATERIALIZED 'TRUE',
        UPDATABLE 'FALSE',
        MATERIALIZED_TABLE 'materialized.PersonCached',
        "teiid_rel:MATVIEW_TTL" 20000000,
        "teiid_rel:ALLOW_MATVIEW_MANAGEMENT" 'true',
        "teiid_rel:MATVIEW_LOADNUMBER_COLUMN" 'LoadNumber',
        "teiid_rel:MATVIEW_STATUS_TABLE" 'materialized.STATUS'
        "teiid_rel:MATVIEW_POLLING_INTERVAL" 30000,
        "teiid_rel:MATVIEW_POLLING_QUERY" '(SELECT NOW() from SYSADMIN.MatViews where       Name = ''v_USER'' AND LoadState = ''FAILED_LOAD'')
        UNION
        (SELECT Updated FROM SYSADMIN.MatViews where Name = ''v_USER'' AND                 LoadState = ''LOADED'')'
         ) as select * from <USER>.Users;
        );
    

    View Options

    The following View properties are extension properties that are used in the management of the Materialized View.

    Property NameDescriptionOptionalDefault
    MATERIALIZEDSet the value to 'TRUE' for the View to be materialized.falseNA
    MATERIALIZED_TABLEDefines the name of target table, this also hints the materialization is using external materialization. Omitting this property and setting the MATERIALIZED property true, invokes internal materialization.falseNA
    teiid_rel:ALLOW_MATVIEW_MANAGEMENTAllow PIP service based automatic management of load/refresh strategies of View.truefalse
    teiid_rel:MATVIEW_STATUS_TABLEFully qualified Status Table Name to manage the load/refresh of the materialized view. See below for table structure and DDL for it.falseNA
    teiid_rel:MATVIEW_LOADNUMBER_COLUMNName of column in the MATERIALIZED_TABLE that can hold status information about load/refresh load process. The column type MUST be long, and typically named as "LoadNumber".falseNONE
    teiid_rel:MATVIEW_BEFORE_LOAD_SCRIPTDDL/DML command to run before the actual load of the cache.trueWhen not defined, no script will be run
    teiid_rel:MATVIEW_AFTER_LOAD_SCRIPTDDL/DML command to run after the actual load of the cache. teiid_rel:MATVIEW_STAGE_TABLE to MATVIEW table.trueWhen not defined, no script will be run
    teiid_rel:ON_VDB_START_SCRIPTDDL/DML command to run start of VDB.trueNA
    teiid_rel:ON_VDB_DROP_SCRIPTDDL/DML command to run at VDB un-deploy; typically used for cleaning the cache/status tables. DO NOT use this script to delete the contents of Status table, when cache scope settings are configured for {FULL} scope, if another version of the VDB is still active. Deletion of this information will reload the materialization table.trueNA
    teiid_rel:MATVIEW_ONERROR_ACTIONAction 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).trueWAIT
    teiid_rel:MATVIEW_TTLtime to live in milliseconds. Provide property or cache hint on view transformation - property takes precedence.true2^63 milliseconds - effectively the table will not refresh, but will be loaded a single time initially
    teiid_rel:MATVIEW_MAX_STALENESS_PCTThis property defines the percentage max of staleness allowed before a refresh to the View is invoked. Any double value 0 to 100 is valid value. The StateCount column on Status table is used to keep track of the number of updates, and this value is checked against Cardinality column to calculate the amount of variance. The availability of this property, supercedes the MATVIEW_TTL property in terms of when a refresh job triggered to update the contents of the view.trueNA
    teiid_rel:MATVIEW_POLLING_INTERVALThis property defines the polling interval, in milliseconds, used with the polling query.true--
    teiid_rel:MATVIEW_POLLING_QUERYThis property defines a query that must return a single timestamp value. If the value is greater than the last update time of the materialization table, it will be reloaded.true--

    Multivalue support

    If multiple values are managed on the Asset or on the User, the required view structure should flatten the values into multi records as demonstrated in the example below:

    Accounts can be associated with multiple countries. To support this type of use case, the required view structure should be according to the following example:

    Account IDBalanceCountry
    166:905000IL
    166:9010,000IL
    167:502000UK
    165:4010,000US
    167:5015,000US

    Notes: When creating an Asset Template, you need to define which columns have multiple values.

    Multivalue attributes in Asset Templates are not supported in LDAP Data Sources.

    Views List

    A list of all available views is presented on the main page. Upon expanding, a list of all available columns within a view is shown.


    Was this article helpful?