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

openGauss每日一练第19天|学习openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint

原创 赵敬星 2021-12-19
523

坚持学习openGauss数据库,坚持每天打卡。第十九天学习openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint。

连接openGauss

root@modb:~# su - omm omm@modb:~$ gsql -r gsql ((opengauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr ) non-ssl connection (ssl connection is recommended when requiring high-security) type "help" for help. omm=#
复制

1.创建分区表,并用generate_series(1,N)函数对表插入数据

omm=# create table tab(id int not null,name varchar(20)) omm-# partition by range(id) omm-# ( omm(# partition store_p0 values less than (50), omm(# partition store_p1 values less than (100), omm(# partition store_p2 values less than (150), omm(# partition store_p3 values less than (200) omm(# ); CREATE TABLE omm=# insert into tab values(1,'a'),(51,'b'),(101,'c'),(151,'d'); INSERT 0 4 omm=# insert into tab select generate_series(1,199),'test'; INSERT 0 199
复制

2.收集表统计信息

--查看系统表中表的统计信息 omm=# select relname, relpages, reltuples from pg_class where relname = 'tab'; relname | relpages | reltuples ---------+----------+----------- tab | 0 | 0 (1 row) --使用ANALYZE VERBOSE语句更新统计信息,并输出表的相关信息 omm=# analyze verbose tab; INFO: analyzing "public.tab"(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 50 live rows and 0 dead rows; 50 rows in sample, 50 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 51 live rows and 0 dead rows; 51 rows in sample, 51 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 51 live rows and 0 dead rows; 51 rows in sample, 51 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 51 live rows and 0 dead rows; 51 rows in sample, 51 estimated total rows(gaussdb pid=1) ANALYZE --查看系统表中表的统计信息 omm=# select relname, relpages, reltuples from pg_class where relname = 'tab'; relname | relpages | reltuples ---------+----------+----------- tab | 4 | 203 (1 row)
复制

3.显示简单查询的执行计划;建立索引并显示有索引条件的执行计划

--使用默认的打印格式 SET explain_perf_mode=normal; --显示表简单查询的执行计划 omm=# explain select * from tab; QUERY PLAN ----------------------------------------------------------------------- Partition Iterator (cost=0.00..6.03 rows=203 width=8) Iterations: 4 -> Partitioned Seq Scan on tab (cost=0.00..6.03 rows=203 width=8) Selected Partitions: 1..4 (4 rows) --有索引条件的执行计划 omm=# create index store_index1 on tab(id) local; CREATE INDEX omm=# explain select * from tab where id<100; QUERY PLAN ----------------------------------------------------------------------- Partition Iterator (cost=0.00..4.54 rows=102 width=8) Iterations: 2 -> Partitioned Seq Scan on tab (cost=0.00..4.54 rows=102 width=8) Filter: (id < 100) Selected Partitions: 1..2 (5 rows)
复制

4.更新表数据,并做垃圾收集

omm=# update tab set id = id + 1 where id < 100; UPDATE 101 omm=# VACUUM (VERBOSE, ANALYZE) tab; INFO: vacuuming "public.tab"(gaussdb pid=1) DETAIL: 50 dead row versions cannot be removed yet. There were 0 unused item pointers. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: index "store_index1" now contains 99 row versions in 2 pages(gaussdb pid=1) DETAIL: 0 index row versions were removed. 0 index pages have been deleted, 0 are currently reusable. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: "tab": found 0 removable, 99 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1)INFO: vacuuming "public.tab"(gaussdb pid=1) INFO: index "store_index1" now contains 102 row versions in 2 pages(gaussdb pid=1) DETAIL: 0 index row versions were removed. 0 index pages have been deleted, 0 are currently reusable. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: "tab": found 0 removable, 102 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1) DETAIL: 51 dead row versions cannot be removed yet. There were 0 unused item pointers. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: vacuuming "public.tab"(gaussdb pid=1) INFO: index "store_index1" now contains 52 row versions in 2 pages(gaussdb pid=1) DETAIL: 0 index row versions were removed. 0 index pages have been deleted, 0 are currently reusable. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: "tab": found 0 removable, 52 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1) DETAIL: 0 dead row versions cannot be removed yet. There were 0 unused item pointers. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: vacuuming "public.tab"(gaussdb pid=1) INFO: index "store_index1" now contains 51 row versions in 2 pages(gaussdb pid=1) DETAIL: 0 index row versions were removed. 0 index pages have been deleted, 0 are currently reusable. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: "tab": found 0 removable, 51 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1) DETAIL: 0 dead row versions cannot be removed yet. There were 0 unused item pointers. 0 pages are entirely empty. CPU 0.00s/0.00u sec elapsed 0.00 sec. INFO: analyzing "public.tab"(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 49 live rows and 50 dead rows; 49 rows in sample, 49 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 51 live rows and 51 dead rows; 51 rows in sample, 51 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 52 live rows and 0 dead rows; 52 rows in sample, 52 estimated total rows(gaussdb pid=1) INFO: ANALYZE INFO : "tab": scanned 1 of 1 pages, containing 51 live rows and 0 dead rows; 51 rows in sample, 51 estimated total rows(gaussdb pid=1) VACUUM
复制

5.清理数据

drop table tab;
复制

通过学习openGauss的收集统计信息、打印执行计划、垃圾收集和checkpoint。了解到VACUUM回收表或B-Tree索引中已经删除的行所占据的存储空间。
检查点(CHECKPOINT)是一个事务日志中的点,所有数据文件都在该点被更新以反映日志中的信息,所有数据文件都将被刷新到磁盘。

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

文章被以下合辑收录

评论