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

openGauss每日一练第18天|视图管理

原创 吴杰克 2022-12-11
395

openGauss每日一练第18天

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

--创建模式、表和插入数据 su - omm sql -r Create schema mytpcds; CREATE TABLE mytpcds.mycustomer(c_mycustomer_sk integer,c_mycustomer_id char(5),c_first_name char(6),c_last_name char(8)); INSERT INTO mytpcds.mycustomer VALUES (6885, 1, 'Joes', 'Hunter'),(4321, 2, 'Lily','Carter'),(9527, 3, 'James', 'Cook'),(9500, 4, 'Lucy', 'Baker'); --创建视图 CREATE VIEW mytpcds.mycustomer_details_view_v1 AS SELECT * FROM mytpcds.mycustomer WHERE c_mycustomer_sk > 5400; --查看视图内容 select * from mytpcds.mycustomer_details_view_v1;
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 mytpcds; CREATE SCHEMA omm=# CREATE TABLE mytpcds.mycustomer(c_mycustomer_sk integer,c_mycustomer_id char(5),c_first_name char(6),c_last_name char(8)); CREATE TABLE omm=# INSERT INTO mytpcds.mycustomer VALUES (6885, 1, 'Joes', 'Hunter'),(4321, 2, 'Lily','Carter'),(9527, 3, 'James', 'Cook'),(9500, 4, 'Lucy', 'Baker'); INSERT 0 4 omm=# CREATE VIEW mytpcds.mycustomer_details_view_v1 AS SELECT * FROM mytpcds.mycustomer WHERE c_mycustomer_sk > 5400; CREATE VIEW omm=# select * from mytpcds.mycustomer_details_view_v1; c_mycustomer_sk | c_mycustomer_id | c_first_name | c_last_name -----------------+-----------------+--------------+------------- 6885 | 1 | Joes | Hunter 9527 | 3 | James | Cook 9500 | 4 | Lucy | Baker (3 rows) omm=#

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

create VIEW mytpcds.part_view as SELECT * FROM mytpcds.mycustomer_details_view_v1 where c_mycustomer_sk =9527; --查看新的视图 select * from mytpcds.part_view;
omm=# create VIEW mytpcds.part_view as SELECT * FROM mytpcds.mycustomer_details_view_v1 where c_mycustomer_sk =9527; CREATE VIEW omm=# select * from mytpcds.part_view; c_mycustomer_sk | c_mycustomer_id | c_first_name | c_last_name -----------------+-----------------+--------------+------------- 9527 | 3 | James | Cook (1 row) omm=#

3.创建物化视图。

--创建基表 drop materialized view if exists mv_mytest; drop table if exists mytest; create table mytest(id serial primary key,mytestnum serial); insert into mytest(mytestnum) values(generate_series(1,100000)); --创建物化视图: create materialized view mv_mytest as select * from mytest where mytestnum%2=0; --查看物化视图目前有多少行记录: select count(*) from mv_mytest;
omm=# drop materialized view if exists mv_mytest; NOTICE: materialized view "mv_mytest" does not exist, skipping DROP MATERIALIZED VIEW omm=# drop table if exists mytest; NOTICE: table "mytest" does not exist, skipping DROP TABLE omm=# create table mytest(id serial primary key,mytestnum serial); NOTICE: CREATE TABLE will create implicit sequence "mytest_id_seq" for serial column "mytest.id" NOTICE: CREATE TABLE will create implicit sequence "mytest_mytestnum_seq" for serial column "mytest.mytestnum" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "mytest_pkey" for table "mytest" CREATE TABLE omm=# insert into mytest(mytestnum) values(generate_series(1,100000)); INSERT 0 100000 omm=# create materialized view mv_mytest as select * from mytest where mytestnum%2=0; CREATE MATERIALIZED VIEW omm=# select count(*) from mv_mytest; count ------- 50000 (1 row) omm=#

4.手动更新物化视图。

--向基表mytest插入数据,查看物化视图有多少行记录: insert into mytest(mytestnum) values(generate_series(1,100000)); select count(*) from mv_mytest; --手动更新物化视图,并查看更新物化视图后,物化视图有多少行记录: refresh materialized view mv_mytest; select count(*) from mv_mytest;
omm=# insert into mytest(mytestnum) values(generate_series(1,100000)); INSERT 0 100000 omm=# select count(*) from mv_mytest; count ------- 50000 (1 row) omm=# refresh materialized view mv_mytest; REFRESH MATERIALIZED VIEW omm=# select count(*) from mv_mytest; count -------- 100000 (1 row) omm=#

5.删除创建的视图。

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

评论