Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

SQL Statement Example of Materialized Views

Updated on 2024-11-29 GMT+08:00

For details about the SQL statements for materialized views, see Table 1.

Table 1 Operations on materialized views

Operation

Function

SQL Statement Example of Materialized View

Remarks

Creating a materialized view

(When a materialized view is created, only the definition of the materialized view is created. To fill in data, run the refresh materialized view name command.)

Create a materialized view that never expires.

create materialized view mv.default.mv1 with(storage_table='hive.default.mv11') AS select id from hive.mvschema.t1;

  • storage_table specifies the location where the materialized view data is materialized into a physical table.
  • When creating a materialized view, you must specify mv for the catalog. You can also create a schema.
  • For the AS SELECT clause, pay attention to the items listed in Creating the AS SELECT Clause for a Materialized View.

Create a materialized view that is valid for one day and cannot automatically refresh.

create materialized view mv.default.mv1 with(storage_table='hive.default.mv11', mv_validity = '24h') AS select id from hive.mvschema.t1;

mv_validity specifies the validity of a materialized view.

Create a materialized view that automatically refreshes data every hour.

create materialized view mv.default.mv1 with(storage_table='hive.default.mv1', need_auto_refresh = true, mv_validity = '1h', start_refresh_ahead_of_expiry = 0.2, refresh_priority = 3, refresh_duration = '5m') AS select id from hive.mvschema.t1;

  • need_auto_refresh: indicates whether to enable automatic refresh.
  • start_refresh_ahead_of_expiry: a refresh task is triggered for the materialized view at the time specified by mv_validity* (1-start_refresh_ahead_of_expiry) so that the task status is changed to Refreshable.
  • refresh_priority specifies the priority of refreshing tasks.
  • refresh_duration specifies the maximum duration of a refreshing task.

Showing materialized views

Show all MVs whose catalog name is mv and schema name is mvschema.

show materialized views from mvschema;

mvschema indicates the schema name. The value of catalog is fixed to mv.

Use the LIKE clause to filter the materialized views whose names meet the rule expression.

show MATERIALIZED VIEWs in mvschema tables like '*mvtb_0001';

mvschema indicates the schema name.

Querying the statement for creating a materialized view

Query the statement for creating the the materialized view of mv.default.mv1.

show create materialized view mv.default.mv1;

mv1 indicates the name of the materialized view.

Querying a materialized view

Query data in mv.default.mv1.

select * from mv.default.mv1;

mv1 indicates the name of the materialized view.

Refreshing a materialized view

Refresh the materialized view of mv.default.mv1.

refresh materialized view mv.default.mv1;

-

Modifying the properties of materialized views

Modifying the properties of the mv.default.mv1 materialized view

Alter materialized view mv.mvtestprop.pepa_ss set PROPERTIES refresh_priority = 2;

refresh_priority = 2 is the property of the materialized view.

Changing the status of materialized views

Changing the status of the mv.default.mv1 materialized view

alter materialized view mv.default.mv1 set status SUSPEND;

SUSPEND is the status of the materialized view. The status can be:

  • SUSPEND: The materialized view is suspended. The suspended materialized view is not rewritten.
  • ENABLE: The materialized view is available.
  • REFRESHING: The materialized view data is being refreshed and cannot be rewritten.
  • DISABLE: The materialized view is disabled.

You can only convert the status between ENABLE and SUSPEND, and chang the DISABLE state to SUSPEND or ENABLE.

Deleting a materialized view

Delete the materialized view of mv.default.mv1.

drop materialized view mv.default.mv1;

-

Enabling materialized view rewriting capability to optimize SQL statements

Enabling materialized view rewriting capability at the session level to optimize SQL statements

set session materialized_view_rewrite_enabled=true;

-

Verifying whether SQL statements can be optimized by rewriting a query to a materialized view

Verify whether the SELECT statement can be rewritten and optimized by mv.default.mv1.

verify materialized view mvname(mv.default.mv1) originalsql select id from hive.mvschema.t1;

-

Enabling the specified materialized view at the SQL level to optimize the SQL statements

Forcibly use mv.default.mv1 for SQL statement optimization in queries.

/*+ REWRITE(mv.default.mv1) */ select id from hive.mvschema.t1;

-

Disabling materialized views at the SQL level to optimize the SQL statements

Do not use materialized views for SQL statement optimization in queries.

/*+ NOREWRITE */ select id from hive.mvschema.t1;

-

Refreshing the metadata cache of materialized views

Synchronize the metadata cache of materialized views between tenants.

refresh catalog mv;

-

Creating the AS SELECT Clause for a Materialized View

The AS SELECT clause for creating materialized views cannot contain reserved keywords in Calcite SQL parsing and rewriting functions, such as default. To use reserved keywords in the AS SELECT clause, use either of the following solutions:
  • When creating MVs and executing original queries, you need to add double quotes to the default schema name.

    The following uses reserved keyword default in the AS SELECT clause as an example:

    Creating a materialized view

    CREATE MATERIALIZED VIEW mv.default.mv1 WITH(storage_table='hive.default.mv11') AS SELECT id FROM hive."default".t1;

    SELECT query

    SELECT id FROM hive."default".t1;
  • Set the corresponding catalog and schema at the Session level, rather than passing fully qualified names in the query.

    For example, set catalogname to hive and schemaname to default.

    USE hive.default; 

    Creating a materialized view

    CREATE MATERIALIZED VIEW mv.default.mv1 WITH(storage_table='hive.default.mv11') AS SELECT id FROM t1;

    SELECT query

    SELECT id FROM t1;

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback