学习内容:视图的管理
1.使用gsql -r登录opengauss数据库
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.2.创建一个schema并在该schema下创建一个表。插入部分数据
omm=# create schema tpcds;
CREATE SCHEMA
omm=# \dn
List of schemas
Name | Owner
-----------------+-------
blockchain | omm
cstore | omm
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
pkg_service | omm
public | omm
snapshot | omm
sqladvisor | omm
tpcds | omm
(11 rows)
omm=# CREATE TABLE tpcds.customer
omm-# ( c_customer_sk integer,
omm(# omm(# c_customer_id char(5),
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=#
omm=# select * from tpcds.customer;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------
6885 | 1 | Joes | Hunter
4321 | 2 | Lily | Carter
9527 | 3 | James | Cook
9500 | 4 | Lucy | Baker
(4 rows)3.在该表的基础上创建视图并查看视图内容
omm=# create view tpcds.view1 as select * from tpcds.customer where c_first_name like 'L%';
CREATE VIEWomm=# select * from tpcds.view1;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------
4321 | 2 | Lily | Carter
9500 | 4 | Lucy | Baker
(2 rows)4.在视图的基础上创建视图并查看其内容
omm=# create view tpcds.view2 as select * from tpcds.view1 where c_customer_sk>5000;
CREATE VIEW
omm=# select * from tpcds.view2;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------
9500 | 4 | Lucy | Baker
(1 row)5.创建一个表。生成一些数据
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 1000006.在刚才创建的表的基础上创建一个物化视图并查看其数据量。
omm=# create materialized view mv_test as select * from test where testnum>80000;
CREATE MATERIALIZED VIEWomm=# select count(*) from mv_test;
count
-------
20000
(1 row)7.查看物化视图相关系统表
omm=# select * from GS_MATVIEW;
matviewid | mapid | ivm | needrefresh | refreshtime
-----------+-------+-----+-------------+-------------
16412 | 0 | f | |
(1 row)8.利用refresh materialized命令手动更新物化视图
omm=# insert into test(testnum) values(generate_series(1,100000));
INSERT 0 100000
omm=# select count(*) from mv_test;
omm=# count
-------
20000
(1 row)
omm=# refresh materialized view mv_test;
REFRESH MATERIALIZED VIEW
omm=# select count(*) from mv_test;
(1 row)
omm=# count
-------
400009.删除视图。
omm=# drop view tpcds.view2;
DROP VIEW
omm=# drop view tpcds.view1;
DROP VIEW
omm=# drop materialized view mv_test;
DROP MATERIALIZED VIEW「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




