暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第 18 天 | openGauss 3.0.0数据库在线实训课程

原创 shunwah 2022-12-11
949

作者:马顺华

从事运维管理工作多年,目前就职于某科技有限公司,熟悉运维自动化、OceanBase部署运维、MySQL 运维以及各种云平台技术和产品。并已获得OceanBase认证OBCA、OBCP证书。

第18天 | openGauss逻辑结构:视图管理

学习目标

掌握openGauss视图的管理:创建视图、删除视图、查询视图的信息、修改视图的信息。

课程学习

openGauss逻辑结构:视图管理

1.创建视图

1)-- 创建模式、表和插入数据
root@modb:~# 
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# Create schema tpcds;
CREATE SCHEMA
omm=# CREATE TABLE tpcds.customer
omm-# (  c_customer_sk             integer,   
omm(#   c_customer_id             char(5),    
omm(#   c_first_name              char(6),    
omm(#   c_last_name               char(8) 
omm(# ) ;
CREATE TABLE
omm=# INSERT INTO tpcds.customer VALUES    
omm-# (6885, 1, 'Joes', 'Hunter'),    
omm-# (4321, 2, 'Lily','Carter'),    
omm-# (9527, 3, 'James', 'Cook'),
omm-# (9500, 4, 'Lucy', 'Baker');
INSERT 0 4
omm=# 

image.png

2)-- 创建视图
omm=# 
omm=# CREATE VIEW tpcds.customer_details_view_v1 AS
omm-# SELECT * FROM tpcds.customer
omm-# WHERE c_customer_sk > 5400;
CREATE VIEW
omm=# 

image.png

3)-- 查看视图内容
omm=# select * from tpcds.customer_details_view_v1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          6885 | 1             | Joes         | Hunter  
          9527 | 3             | James        | Cook    
          9500 | 4             | Lucy         | Baker   
(3 rows)

omm=# 

image.png

2.基于视图建立新的视图

omm=# create  VIEW tpcds.part_view as  SELECT * FROM  tpcds.customer_details_view_v1 where  c_customer_sk =9527;
CREATE VIEW
omm=# 

image.png

1)-- 查看新的视图
omm=# 
omm=# select * from tpcds.part_view;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          9527 | 3             | James        | Cook    
(1 row)

omm=# 

image.png

3.创建物化视图

普通视图在查询中是实时进行计算的。如果建立视图的基表数据很多,使用视图的时候,进行实时计算视图表示的结果集,将消耗很大的计算机资源,并且费时很长。
物化视图提前计算出视图的结果集,并将该结果集保存在数据库里。
如果更新了基表,物化视图将过期。也就是说,基表更新后,物化视图不能反映最新的数据情况。因此在基表发生变化的时候,需要对物化视图进行更新。

1)-- 创建基表
omm=# 
omm=# drop materialized view if exists mv_test;
NOTICE:  materialized view "mv_test" does not exist, skipping
DROP MATERIALIZED VIEW
omm=# drop table if exists test;
NOTICE:  table "test" does not exist, skipping
DROP TABLE
omm=# create table test(id serial primary key,testnum serial);
NOTICE:  CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
NOTICE:  CREATE TABLE will create implicit sequence "test_testnum_seq" for serial column "test.testnum"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
omm=# insert into test(testnum) values(generate_series(1,100000));
INSERT 0 100000
omm=# 

image.png

2)-- 创建物化视图:
omm=# create materialized view mv_test as 
omm-# select * from test where testnum%2=0;
CREATE MATERIALIZED VIEW
omm=# 

image.png

3)-- 查看物化视图目前有多少行记录:
omm=# 
omm=# select count(*) from mv_test;
 count 
-------
 50000
(1 row)

omm=# 

image.png

4.普通视图相关的系统表pg_views

1)-- 系统视图pg_views
omm=# 
omm=# select * from pg_views where schemaname = 'tpcds' or schemaname = 'public';
 schemaname |         viewname         | viewowner |                                              definition                                         
     
------------+--------------------------+-----------+-------------------------------------------------------------------------------------------------
-----
 tpcds      | customer_details_view_v1 | omm       | SELECT  * FROM tpcds.customer WHERE (customer.c_customer_sk > 5400);
 tpcds      | part_view                | omm       | SELECT  * FROM tpcds.customer_details_view_v1 WHERE (customer_details_view_v1.c_customer_sk = 95
27);
(2 rows)

omm=# 

image.png

5.物化视图相关的系统表GS_MATVIEW

1)-- GS_MATVIEW系统表提供了关于数据库中每一个物化视图的信息。
omm=# 
omm=# select * from GS_MATVIEW;
 matviewid | mapid | ivm | needrefresh | refreshtime 
-----------+-------+-----+-------------+-------------
     16416 |     0 | f   |             | 
(1 row)

omm=# 

image.png

6.手动更新物化视图

1)-- 向基表test插入数据,查看物化视图有多少行记录:
omm=# 
omm=# insert into test(testnum) values(generate_series(1,100000));
INSERT 0 100000
omm=# select count(*) from mv_test;
 count 
-------
 50000
(1 row)

omm=# 
2)-- 手动更新物化视图,并查看更新物化视图后,物化视图有多少行记录:
omm=# 
omm=# refresh materialized view mv_test;
REFRESH MATERIALIZED VIEW
omm=# select count(*) from mv_test;
 count  
--------
 100000
(1 row)

omm=# 

image.png

7.删除视图

1)-- 删除普通视图

omm=# 
omm=# DROP view tpcds.part_view;
DROP VIEW
omm=# DROP view TPCDS.customer_details_view_v1;
DROP VIEW
omm=# 

image.png

2)-- 删除物化视图
omm=# 
omm=# DROP MATERIALIZED VIEW mv_test;
DROP MATERIALIZED VIEW
omm=# 

课程作业

1.创建表,创建普通视图

2.使用视图创建新的视图

3.创建物化视图

4.手动更新物化视图

5.删除创建的视图

1.创建表,创建普通视图

root@modb:~# 
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# Create schema tpopen;
CREATE SCHEMA
omm=# CREATE TABLE tpopen.customer
omm-# (  c_customer_sk             integer,   
omm(#   c_customer_id             char(5),    
omm(#   c_first_name              char(6),    
omm(#   c_last_name               char(8) 
omm(# ) ;
CREATE TABLE
omm=# INSERT INTO tpopen.customer VALUES    
omm-# (6885, 1, 'Joes', 'Hunter'),    
omm-# (4321, 2, 'Lily','Carter'),    
omm-# omm-# (9527, 3, 'James', 'Cook'),
(9500, 4, 'Lucy', 'Baker');
omm=# INSERT 0 4

omm=# CREATE VIEW tpopen.customer_details_view_v1 AS
omm-# SELECT * FROM tpopen.customer
omm-# WHERE c_customer_sk > 5400;
CREATE VIEW
omm=# select * from tpopen.customer_details_view_v1;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          6885 | 1             | Joes         | Hunter  
          9527 | 3             | James        | Cook    
          9500 | 4             | Lucy         | Baker   
(3 rows)
omm=# 

image.png

2.使用视图创建新的视图

omm=# 
omm=# create  VIEW tpopen.part_view as  SELECT * FROM  tpopen.customer_details_view_v1 where  c_customer_sk =9527;
CREATE VIEW
omm=# select * from tpopen.part_view;
 c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
          9527 | 3             | James        | Cook    
(1 row)

omm=# 

image.png

3.创建物化视图

omm=# 
omm=# drop materialized view if exists mv_open;
NOTICE:  materialized view "mv_open" does not exist, skipping
DROP MATERIALIZED VIEW
omm=# drop table if exists open;
NOTICE:  table "open" does not exist, skipping
DROP TABLE
omm=# create table open(id serial primary key,opennum serial);
NOTICE:  CREATE TABLE will create implicit sequence "open_id_seq" for serial column "open.id"
NOTICE:  CREATE TABLE will create implicit sequence "open_opennum_seq" for serial column "open.opennum"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "open_pkey" for table "open"
CREATE TABLE
omm=# insert into open(opennum) values(generate_series(1,100000));
INSERT 0 100000
omm=# 

image.png

omm=# 
omm=# create materialized view mv_open as 
omm-# select * from open where opennum%2=0;
CREATE MATERIALIZED VIEW
omm=# select count(*) from mv_open;
 count 
-------
 50000
(1 row)

omm=# select * from pg_views where schemaname = 'tpopen' or schemaname = 'public';
 schemaname |         viewname         | viewowner |                                              definition                              
                 
------------+--------------------------+-----------+--------------------------------------------------------------------------------------
-----------------
 tpopen     | customer_details_view_v1 | omm       | SELECT  * FROM tpopen.customer WHERE (customer.c_customer_sk > 5400);
 tpopen     | part_view                | omm       | SELECT  * FROM tpopen.customer_details_view_v1 WHERE (customer_details_view_v1.c_cust
omer_sk = 9527);
(2 rows)

omm=# 

image.png

4.手动更新物化视图

omm=# 
omm=# insert into open(opennum) values(generate_series(1,100000));
INSERT 0 100000
omm=# select count(*) from mv_open;
 count 
-------
 50000
(1 row)

omm=# refresh materialized view mv_open;
REFRESH MATERIALIZED VIEW
omm=# select count(*) from mv_open;
 count  
--------
 100000
(1 row)

omm=# 

image.png

5.删除创建的视图

omm=# 
omm=# DROP view tpopen.part_view;
DROP VIEW
omm=# DROP view TPOPEN.customer_details_view_v1;
DROP VIEW
omm=# DROP MATERIALIZED VIEW mv_open;
DROP MATERIALIZED VIEW
omm=# 

image.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论