数据库环境
openGauss:2.0.0 - 数据库实训平台
学习目标
学习openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint
学习笔记
- 查看系统表中表的统计信息
omm=# analyze VERBOSE tpcds.customer_address;
INFO: analyzing "tpcds.customer_address"(gaussdb pid=1)
INFO: ANALYZE INFO : "customer_address": scanned 55 of 55 pages, containing 9994 live rows and 0 dead rows; 9994 rows in sample, 9994 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# select relname, relpages, reltuples from pg_class where relname = 'customer_address';
relname | relpages | reltuples
------------------+----------+-----------
customer_address | 55 | 9994
(1 row)
- explain_perf_mode为normal时, 以JSON格式输出的执行计划
SET explain_perf_mode=normal;
EXPLAIN(FORMAT JSON) SELECT * FROM tpcds.customer_address;
课后作业
1.创建分区表,并用generate_series(1,N)函数对表插入数据
omm=# create schema my_schema;
CREATE SCHEMA
omm=# create table my_schema.product
omm-# (
omm(# product_id integer,
omm(# product_name char(30)
omm(# )
omm-# partition by range(product_id)
omm-# (
omm(# partition p0 values less than (500),
omm(# partition p1 values less than (1000),
omm(# partition p2 values less than (2000),
omm(# partition p3 values less than (maxvalue)
omm(# );
CREATE TABLE
omm=# insert into my_schema.product values(generate_series(1,10000));
INSERT 0 10000
omm=# select * from my_schema.product limit 10;
product_id | product_name
------------+--------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
(10 rows)
2.收集表统计信息
omm=# analyze VERBOSE my_schema.product;
INFO: analyzing "my_schema.product"(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 3 of 3 pages, containing 499 live rows and 0 dead rows; 499 rows in sample, 499 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 3 of 3 pages, containing 500 live rows and 0 dead rows; 500 rows in sample, 500 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 5 of 5 pages, containing 1000 live rows and 0 dead rows; 1000 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 36 of 36 pages, containing 8001 live rows and 0 dead rows; 8001 rows in sample, 8001 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# select relname, relpages, reltuples from pg_class where relname = 'product';
relname | relpages | reltuples
---------+----------+-----------
product | 47 | 10000
(1 row)
3.显示简单查询的执行计划;建立索引并显示有索引条件的执行计划
omm=# SET explain_perf_mode=normal;
SET
omm=# explain select * from my_schema.product;
QUERY PLAN
---------------------------------------------------------------------------------
omm=# Partition Iterator (cost=0.00..147.00 rows=10000 width=128)
Iterations: 4
-> Partitioned Seq Scan on product (cost=0.00..147.00 rows=10000 width=128)
Selected Partitions: 1..4
(4 rows)
omm=# create index product_id_index on my_schema.product(product_id);
CREATE INDEX
omm=# explain select * from my_schema.product where product_id > 5000;
QUERY PLAN
---------------------------------------------------------------------------------------
Index Scan using product_id_index on product (cost=0.00..112.75 rows=5000 width=128)
Index Cond: (product_id > 5000)
(2 rows)
4.更新表数据,并做垃圾收集
omm=# update my_schema.product set product_id = product_id - 100 where product_id > 3000;
UPDATE 7000
omm=# VACUUM (VERBOSE, ANALYZE) my_schema.product;
INFO: vacuuming "my_schema.product"(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: index "product_id_index" now contains 499 row versions in 68 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: "product": found 0 removable, 499 nonremovable row versions in 3 out of 3 pages(gaussdb pid=1)INFO: vacuuming "my_schema.product"(gaussdb pid=1)
INFO: index "product_id_index" now contains 500 row versions in 68 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: "product": found 0 removable, 500 nonremovable row versions in 3 out of 3 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 "my_schema.product"(gaussdb pid=1)
INFO: index "product_id_index" now contains 1000 row versions in 68 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: "product": found 0 removable, 1000 nonremovable row versions in 5 out of 5 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 "my_schema.product"(gaussdb pid=1)
INFO: index "product_id_index" now contains 15001 row versions in 68 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: "product": found 0 removable, 15001 nonremovable row versions in 67 out of 67 pages(gaussdb pid=1)
DETAIL: 7000 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: scanned index "product_id_index" to remove 0.000000 invisible rows(gaussdb pid=1)
DETAIL: CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: analyzing "my_schema.product"(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 3 of 3 pages, containing 499 live rows and 0 dead rows; 499 rows in sample, 499 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 3 of 3 pages, containing 500 live rows and 0 dead rows; 500 rows in sample, 500 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 5 of 5 pages, containing 1000 live rows and 0 dead rows; 1000 rows in sample, 1000 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "product": scanned 67 of 67 pages, containing 8001 live rows and 7000 dead rows; 8001 rows in sample, 8001 estimated total rows(gaussdb pid=1)
VACUUM
omm=# CHECKPOINT;
CHECKPOINT
5.清理数据
omm=# drop schema my_schema cascade;
NOTICE: drop cascades to table my_schema.product
DROP SCHEMA
学习资源
- openGauss SQL学习参考资料
- 每日一练:openGauss数据库在线实训课程
- openGauss每日一练 | 21期养成好习惯,提升技术能力!
- 墨天轮Markdown编辑器使用介绍
- 墨天轮数据库在线实训平台V1.0操作手册
- 墨天轮数据社区
欢迎各位同学一起来交流学习心得!
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




