After you create materialized views, you can manage all materialized views in your AnalyticDB for MySQL cluster. For example, you can query the refresh records of materialized views, query the definitions of materialized views, change the elastic resource groups used by materialized views, enable or disable the query rewrite feature, and delete materialized views.
Query the definition of a materialized view
You can query the DDL statement that is used to create a materialized view.
Required permissions
You must have the SELECT permission on the materialized view. To obtain this permission, a privileged account or a standard account with the GRANT permission can grant the SELECT permission to your account on the Account Management page in the console or by executing the GRANT SELECT ON database.mv_name TO username; statement.
Syntax
SHOW CREATE MATERIALIZED VIEW <mv_name>Example
Query the DDL statement that was used to create the materialized view compl_mv1.
SHOW CREATE MATERIALIZED VIEW demo.compl_mv1;Query the metadata of materialized views
Field descriptions
The `information_schema.mv_info` table contains the metadata of all materialized views. If you want to query the definition of a single materialized view, we recommend that you execute the SHOW CREATE MATERIALIZED VIEW statement.
The following table describes the common fields in the information_schema.mv_info table.
Field | Description |
MV_SCHEMA | The name of the database where the materialized view resides. |
MV_NAME | The name of the materialized view. |
MV_DEFINITION | The DDL statement that is used to create the materialized view. |
MV_QUERY | The original SQL query of the materialized view. |
FIRST_REFRESH_TIME | The first refresh time if the materialized view is configured to automatically refresh. |
NEXT_REFRESH_TIME_FUNC | The function that deduces the next refresh time if the materialized view is configured to automatically refresh. |
OWNER | The creator of the materialized view. |
QUERY_SCHEMA | The name of the database where the materialized view creation statement is executed. |
REFRESH_CONDITION | Trigger mechanism for refreshes
|
REFRESH_STATE | The refresh policy of the materialized view.
|
UPDATE_TIME | The time when the metadata of the materialized view was last updated. |
Examples
Count all materialized views in the cluster.
SELECT count(*) FROM information_schema.mv_info;Query metadata for all materialized views.
SELECT * FROM information_schema.mv_info;
Query all materialized views
You can query all materialized views on which you have the SELECT permission in a database. If you specify an SQL pattern, all materialized views that match the pattern are returned.
Required 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 can grant it to your account on the Accounts page in the console or using the 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. You can specify a database other than the current database.
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 whose names start with compl in the current database.
SHOW MATERIALIZED VIEWS LIKE 'compl%';NoteFor more information about how to use `LIKE 'pattern'`, see String functions.
Query all materialized views in the demo database.
SELECT * FROM information_schema.mv_info WHERE mv_schema='demo';
Query the refresh records of a materialized view
Query the automatic refresh records
Syntax
You can execute the following statement to query the automatic refresh records of a materialized view, including the start time, end time, status, and query ID:
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 table contains up to 1,024 automatic refresh records of materialized views. Only materialized views that are configured to automatically refresh at scheduled intervals using the START WITH or NEXT parameter are included in the table.
The `information_schema.mv_auto_refresh_jobs` table does not include the first refresh record that occurs after creation.
Examples
Query all automatic refresh records of the compl_mv1 materialized view in the demo database.
SELECT * FROM information_schema.mv_auto_refresh_jobs WHERE mv_schema = 'demo' AND mv_name = 'compl_mv1';Query the most recent pending automatic refresh job for the `compl_mv1` materialized view in the `demo` database.
SELECT * FROM information_schema.mv_auto_refresh_jobs WHERE mv_schema = 'demo' AND mv_name = 'compl_mv1' AND state = 'PENDING';
Field descriptions
Common field descriptions:
Field | Description |
MV_SCHEMA | The name of the database where the materialized view resides. |
MV_NAME | The name of the materialized view. |
PROCESS_ID | The Process ID of the refresh task. |
SCHEDULED_START_TIME | The scheduled start time of the refresh job. |
START_TIME | The actual start time of the refresh job. |
END_TIME | The actual end time of the refresh job. |
STATE | Refresh the status.
|
Query the manual refresh records
To query manual refresh records from the past 30 days, use the SQL Audit feature. Enter
REFRESH MATERIALIZED VIEW mv_namein the Keyword field on the SQL Audit tab. You can then view the time, duration, IP address, and username for each manual refresh.NoteYou must enable the SQL Audit feature. SQL operations that occurred before you enabled this feature are not recorded in audit logs.

To query manual and automatic refresh records from the past 14 days, use the SQL diagnostics and optimization feature. Enter the name of a materialized view, such as
compl_mv1, on the SQL Diagnostics and Optimization tab. You can then view the start time, username, duration, and query ID for all related SQL queries, including creation, manual refresh, automatic refresh, and modification.
Modify a materialized view
You can change a standard materialized view to an elastic materialized view, change its resource group, enable or disable its query rewrite feature, or rename it.
You cannot change other properties, such as the definition, refresh policy, or refresh trigger mechanism.
Required 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 the ALTER permission to your account. This can be done on the Account Management page in the console or by executing the 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>]MV_PROPERTIES: The properties of the materialized view, including the `mv_resource_group` and `mv_refresh_hints` parameters.mv_resource_group: the resource group that is used by the materialized view. If the specified resource group does not exist, an error occurs when you create the materialized view.
mv_refresh_hints: the hint parameter of the materialized view. For more information, see the "Common hint parameters" section of the Config and hint configuration parameters topic.
ImportantThe value of the
<MV_PROPERTIES>parameter specified in the `ALTER` statement overwrites the value of the<MV_PROPERTIES>parameter specified when you created the materialized view.For example, assume you configure the
mv_resource_groupandmv_refresh_hintsparameters when you create a materialized view. If you then configure only themv_resource_groupparameter in the `ALTER` statement, themv_resource_groupparameter is modified and themv_refresh_hintsparameter is purged after you execute the statement.ENABLE|DISABLE QUERY REWRITE: Enables or disables the query rewrite feature.RENAME <new_mv_name>: Renames the materialized view.
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"} }';Change the standard materialized view
compl_mv1to 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 a materialized view
If you physically delete a materialized view, the materialized view and the corresponding data are deleted.
If you run `DROP TABLE` to delete a materialized view, an error is returned. For example: demo.myview11 is materialized view, use DROP MATERIALIZED VIEW.
Required 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 the DROP permission to your account. This can be done on the Accounts page in the console or by executing the 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;