Manage materialized views
After you create materialized views, you can manage them in your cluster. This includes querying their refresh records and definitions, changing the resource group they use, enabling or disabling the query rewrite feature, and deleting them.
Query a materialized view definition
You can query the DDL statement used to create a materialized view.
Permissions
You must have the SELECT permission on the materialized view. If you do not have this permission, a privileged account or a standard account with the GRANT permission must grant you the permission on the account management page in the console or by running the following SQL statement: GRANT SELECT ON database.mv_name TO username;
Syntax
SHOW CREATE MATERIALIZED VIEW <mv_name>
Example
Query the DDL statement for the materialized view compl_mv1.
SHOW CREATE MATERIALIZED VIEW demo.compl_mv1;
Query materialized view metadata
Fields
The information_schema.mv_info view contains metadata for all materialized views. If you only need to view the definition of a single materialized view, use the SHOW CREATE MATERIALIZED VIEW statement.
The following table describes the common fields in information_schema.mv_info.
|
Field |
Description |
|
MV_SCHEMA |
The database where the materialized view is located. |
|
MV_NAME |
The name of the materialized view. |
|
MV_DEFINITION |
The DDL for the table properties of the materialized view. |
|
MV_QUERY |
The materialized view's original SQL query (query_body). |
|
FIRST_REFRESH_TIME |
The time of the first refresh, if automatic refresh is configured. |
|
NEXT_REFRESH_TIME_FUNC |
The function that derives the next refresh time, if automatic refresh is configured. |
|
OWNER |
The creator of the materialized view. |
|
QUERY_SCHEMA |
The database where the |
|
REFRESH_CONDITION |
The refresh trigger mechanism.
|
|
REFRESH_STATE |
The refresh method for the materialized view.
|
|
UPDATE_TIME |
The last update time for this metadata row. |
Examples
-
Count the materialized views in the cluster.
SELECT count(*) FROM information_schema.mv_info; -
Query the metadata for all materialized views.
SELECT * FROM information_schema.mv_info;
List materialized views
Lists the materialized views in a specified database on which you have the SELECT permission. If you specify a pattern, only materialized views whose names match the pattern are returned.
Permissions
You must have the SELECT permission on all materialized views in the specified database. If you do not have this permission, a privileged account or a standard account with the GRANT permission must grant you the permission on the account management page in the console or by running the following SQL statement: GRANT SELECT ON database.* TO username;
Syntax
Query all materialized views in the current database:
SHOW MATERIALIZED VIEWS [LIKE 'pattern']
Query all materialized views in a specific database (which can be different from the current one):
SELECT * FROM information_schema.mv_info WHERE mv_schema='schema_name';
Examples
-
Query all materialized views in the current database.
SHOW MATERIALIZED VIEWS; -
Query all materialized views in the current database whose names start with
compl.SHOW MATERIALIZED VIEWS LIKE 'compl%';NoteFor more information about how to use
LIKE 'pattern', see String functions. -
Query all materialized views in the
demodatabase.SELECT * FROM information_schema.mv_info WHERE mv_schema='demo';
Query refresh records
Automatic refresh records
Syntax
Use the following SQL statement to query the automatic refresh records for a specified materialized view. The records include the start time (START_TIME), end time (END_TIME), status (STATE), and process ID (PROCESS_ID) of the refresh.
SELECT * FROM information_schema.mv_auto_refresh_jobs WHERE mv_schema='<schema_name>' AND mv_name = '<mv_name>';
The information_schema.mv_auto_refresh_jobs view stores the automatic refresh job records for materialized views. It retains a maximum of 1,024 records. This table lists only materialized views configured for scheduled automatic refresh (using the START WITH or NEXT parameters).
The information_schema.mv_auto_refresh_jobs view does not include the record for the initial refresh that occurs when the materialized view is created.
Examples
-
Query all refresh records for the materialized view
compl_mv1in thedemodatabase.SELECT * FROM information_schema.mv_auto_refresh_jobs WHERE mv_schema = 'demo' AND mv_name = 'compl_mv1'; -
Query the most recent pending refresh job for the materialized view
compl_mv1in thedemodatabase.SELECT * FROM information_schema.mv_auto_refresh_jobs WHERE mv_schema = 'demo' AND mv_name = 'compl_mv1' AND state = 'PENDING';
Fields
The following table describes the common fields.
|
Field |
Description |
|
MV_SCHEMA |
The name of the database where the materialized view is located. |
|
MV_NAME |
The name of the materialized view. |
|
PROCESS_ID |
The process ID of the refresh job. |
|
SCHEDULED_START_TIME |
The scheduled start time for the refresh job. |
|
START_TIME |
The actual start time of the refresh job. |
|
END_TIME |
The actual end time of the refresh job. |
|
STATE |
The refresh status.
|
Manual refresh records
-
You can use the SQL Audit feature to query manual refresh records for a materialized view from the last 30 days. When querying, enter the keyword
REFRESH MATERIALIZED VIEW mv_nameto find information such as the time, duration, IP address, and username for manual refreshes.NoteThe SQL Audit feature must be enabled separately. SQL operations performed before this feature was enabled are not recorded.
-
You can use the SQL Diagnostics and Optimization feature to query both manual and automatic refresh records from the last 14 days. When querying, enter the name of the materialized view, such as
compl_mv1. You can retrieve information about all related SQL queries (including creation, manual refresh, automatic refresh, and alterations), such as the start time, username, duration, and query ID.
Alter materialized views
You can convert a standard materialized view into an elastic materialized view, change the resource group it uses, enable or disable its query rewrite feature, or rename it.
You cannot alter other properties, such as the view definition, refresh policy, or refresh trigger.
Permissions
You must have the ALTER permission on the materialized view. If you do not have this permission, a privileged account or a standard account with the GRANT permission must grant you the permission on the account management page in the console or by running the following SQL statement: GRANT ALTER ON database.mv_name TO username;
Syntax
ALTER MATERIALIZED VIEW <mv_name>
[MV_PROPERTIES=<MV_PROPERTIES>]
[ENABLE|DISABLE QUERY REWRITE]
[RENAME <new_mv_name>]
[BINLOG={TRUE|FALSE}]
-
MV_PROPERTIES: Specifies the properties of the materialized view, including themv_resource_groupandmv_refresh_hintsparameters.mv_resource_group: Specifies the resource group that the materialized view uses. If the specified resource group does not exist, an error is returned when creating the materialized view.mv_refresh_hints: Sets parameters for the materialized view. For details on supported configuration parameters, see Common Hints.ImportantThe
<MV_PROPERTIES>properties specified in anALTERstatement completely overwrite the<MV_PROPERTIES>properties specified when the materialized view was created.For example, if you create a materialized view and configure both the
mv_resource_groupandmv_refresh_hintsparameters, and then run an ALTER statement that specifies only themv_resource_groupparameter, themv_resource_groupparameter is updated, and themv_refresh_hintsparameter is cleared. -
ENABLE|DISABLE QUERY REWRITE: Enables or disables the query rewrite feature. -
RENAME <new_mv_name>: Renames the materialized view. -
BINLOG={TRUE|FALSE}: Enables or disables the binlog feature for the materialized view. This is supported in cluster kernel versions 3.2.5.0 and later.
Examples
-
Enable the query rewrite feature for the materialized view
compl_mv1.ALTER MATERIALIZED VIEW compl_mv1 ENABLE QUERY REWRITE; -
Change the resource group used by the elastic materialized view
job_mv.ALTER MATERIALIZED VIEW job_mv MV_PROPERTIES='{ "mv_resource_group":"my_job_rg_2", "mv_refresh_hints":{"query_priority":"NORMAL"} }'; -
Convert the standard materialized view
compl_mv1into an elastic materialized view.ALTER MATERIALIZED VIEW compl_mv1 MV_PROPERTIES='{ "mv_resource_group":"my_job_rg_2" }'; -
Rename the materialized view mv1 to mv2.
ALTER MATERIALIZED VIEW mv1 RENAME mv2;
Delete materialized views
This operation physically deletes the materialized view and its data.
If you use DROP TABLE to delete a materialized view, an error is returned. The error message is similar to demo.myview11 is materialized view, use DROP MATERIALIZED VIEW.
Permissions
You must have the DROP permission on the materialized view. If you do not have this permission, a privileged account or a standard account with the GRANT permission must grant you the permission on the account management page in the console or by running the following SQL statement: GRANT DROP ON database.mv_name TO username;
Syntax
DROP MATERIALIZED VIEW <mv_name>
Example
Delete the materialized view compl_mv1.
DROP MATERIALIZED VIEW compl_mv1;