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

openGauss每日一练第19天 | 收集统计信息、打印执行计划等

原创 olabll1 2021-12-19
483

学习openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint
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=# Create schema tpcds;
CREATE SCHEMA

CREATE TABLE tpcds.customer_address
(
ca_address_sk integer NOT NULL ,
ca_address_id character(16),
ca_street_number character(10) ,
ca_street_name character varying(60) ,
ca_street_type character(15) ,
ca_suite_number character(10) ,
ca_city character varying(60) ,
ca_county character varying(30) ,
ca_state character(2) ,
ca_zip character(10) ,
ca_country character varying(20) ,
ca_gmt_offset numeric(5,2) ,
ca_location_type character(20)
);
CREATE TABLE
insert into tpcds.customer_address values
(1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'),
(2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
(3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family');
INSERT 0 3
–使用序列的generate_series(1,N)函数对表插入数据
omm=# insert into tpcds.customer_address values(generate_series(10, 10000));
INSERT 0 9991
收集统计信息
–查看系统表中表的统计信息
omm=# select relname, relpages, reltuples from pg_class where relname = 'customer_address';
relname | relpages | reltuples
------------------+----------+-----------
customer_address | 0 | 0
(1 row)
—使用ANALYZE VERBOSE语句更新统计信息,并输出表的相关信息
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)
打印执行计划
–使用默认的打印格式
omm=# SET explain_perf_mode=normal;
SET
–显示表简单查询的执行计划
omm=# EXPLAIN SELECT * FROM tpcds.customer_address;
QUERY PLAN
-----------------------------------------------------------------------
Seq Scan on customer_address (cost=0.00..154.94 rows=9994 width=151)
(1 row)
–以JSON格式输出的执行计划(explain_perf_mode为normal时)
omm=# EXPLAIN(FORMAT JSON) SELECT * FROM tpcds.customer_address;
QUERY PLAN
--------------------------------------------
[ +
{ +
"Plan": { +
"Node Type": "Seq Scan", +
"Relation Name": "customer_address",+
"Alias": "customer_address", +
"Startup Cost": 0.00, +
"Total Cost": 154.94, +
"Plan Rows": 9994, +
"Plan Width": 151 +
} +
} +
]
(1 row)
–禁止开销估计的执行计划
omm=# EXPLAIN(COSTS FALSE)SELECT * FROM tpcds.customer_address;

omm=# QUERY PLAN
------------------------------
Seq Scan on customer_address
(1 row)
–带有聚集函数查询的执行计划
omm=# EXPLAIN SELECT SUM(ca_address_sk) FROM tpcds.customer_address WHERE ca_address_sk<100;
QUERY PLAN
-------------------------------------------------------------------------
Aggregate (cost=180.16..180.17 rows=1 width=12)
-> Seq Scan on customer_address (cost=0.00..179.93 rows=94 width=4)
Filter: (ca_address_sk < 100)
(3 rows)
–有索引条件的执行计划
omm=# create index customer_address_idx on tpcds.customer_address(ca_address_sk);
CREATE INDEX
omm=# EXPLAIN SELECT * FROM tpcds.customer_address WHERE ca_address_sk<100;
QUERY PLAN
------------------------------------------------------------------------------------------------
[Bypass]
Index Scan using customer_address_idx on customer_address (cost=0.00..9.90 rows=94 width=151)
Index Cond: (ca_address_sk < 100)
(3 rows)
垃圾收集
–VACUUM回收表或B-Tree索引中已经删除的行所占据的存储空间
omm=# update tpcds.customer_address set ca_address_sk = ca_address_sk + 1 where ca_address_sk <100;
UPDATE 93
omm=# VACUUM (VERBOSE, ANALYZE) tpcds.customer_address;
INFO: vacuuming "tpcds.customer_address"(gaussdb pid=1)
INFO: index "customer_address_idx" now contains 10087 row versions in 31 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: "customer_address": found 0 removable, 10087 nonremovable row versions in 55 out of 55 pages(gaussdb pid=1)
DETAIL: 93 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 "tpcds.customer_address"(gaussdb pid=1)
INFO: ANALYZE INFO : "customer_address": scanned 55 of 55 pages, containing 9994 live rows and 93 dead rows; 9994 rows in sample, 9994 estimated total rows(gaussdb pid=1)
VACUUM
事务日志检查点
–检查点(CHECKPOINT)是一个事务日志中的点,所有数据文件都在该点被更新以反映日志中的信息,所有数据文件都将被刷新到磁盘
omm=# CHECKPOINT;
CHECKPOINT
omm=# drop schema tpcds cascade;
NOTICE: drop cascades to table tpcds.customer_address
DROP SCHEMA
omm=# create schema olab;
CREATE SCHEMA
omm=# create table olab.olab_par_t1 (c1 number(20),c2 char(10))
omm-# partition by range(c1)
omm-# ( partition p1 values less than (30),
omm(# partition p2 values less than (60),
omm(# omm(# partition p3 values less than (90),
partition p5 values less than (maxvalue));
CREATE TABLE
omm=# insert into olab.olab_par_t1 values (1,'a'),(2,'b');
INSERT 0 2
omm=# insert into olab.olab_par_t1 values(generate_series(10, 100));
INSERT 0 91
omm=# select * from olab.olab_par_t1 partition(p5);
c1 | c2
-----+----
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
(11 rows)

omm=# select relname, relpages, reltuples from pg_class where relname = 'olab_par_t1';
relname | relpages | reltuples
-------------+----------+-----------
olab_par_t1 | 0 | 0
(1 row)

omm=# analyze VERBOSE olab.olab_par_t1;
INFO: analyzing "olab.olab_par_t1"(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 22 live rows and 0 dead rows; 22 rows in sample, 22 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 30 live rows and 0 dead rows; 30 rows in sample, 30 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 30 live rows and 0 dead rows; 30 rows in sample, 30 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 11 live rows and 0 dead rows; 11 rows in sample, 11 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# select relname, relpages, reltuples from pg_class where relname = 'olab_par_t1';
relname | relpages | reltuples
-------------+----------+-----------
olab_par_t1 | 4 | 93
(1 row)

omm=# SET explain_perf_mode=normal;
SET
omm=# EXPLAIN SELECT * FROM olab.olab_par_t1;
QUERY PLAN
-------------------------------------------------------------------------------
Partition Iterator (cost=0.00..4.93 rows=93 width=16)
Iterations: 4
-> Partitioned Seq Scan on olab_par_t1 (cost=0.00..4.93 rows=93 width=16)
Selected Partitions: 1..4
(4 rows)

omm=# create index olab.olab_par_t1_c1_inx on olab.olab_par_t1(c1);
CREATE INDEX
未走索引
omm=# EXPLAIN SELECT c1 FROM olab.olab_par_t1 WHERE c1<6;
QUERY PLAN
-----------------------------------------------------------------------------
Partition Iterator (cost=0.00..2.16 rows=2 width=5)
Iterations: 1
-> Partitioned Seq Scan on olab_par_t1 (cost=0.00..2.16 rows=2 width=5)
Filter: (c1 < 6::numeric)
Selected Partitions: 1
(5 rows)

omm=# update olab.olab_par_t1 set c1 = c1 + 10 where c1 <88;
UPDATE 80
omm=# VACUUM (VERBOSE, ANALYZE) olab.olab_par_t1;
INFO: vacuuming "olab.olab_par_t1"(gaussdb pid=1)
INFO: index "olab_par_t1_c1_inx" now contains 34 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: "olab_par_t1": found 0 removable, 34 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1)
DETAIL: 22 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 "olab.olab_par_t1"(gaussdb pid=1)
INFO: index "olab_par_t1_c1_inx" now contains 60 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: "olab_par_t1": found 0 removable, 60 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1)
DETAIL: 30 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 "olab.olab_par_t1"(gaussdb pid=1)
INFO: index "olab_par_t1_c1_inx" now contains 60 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: "olab_par_t1": found 0 removable, 60 nonremovable row versions in 1 out of 1 pages(gaussdb pid=1)
DETAIL: 28 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 "olab.olab_par_t1"(gaussdb pid=1)
INFO: index "olab_par_t1_c1_inx" now contains 19 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: "olab_par_t1": found 0 removable, 19 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: scanned index "olab_par_t1_c1_inx" to remove 0.000000 invisible rows(gaussdb pid=1)
DETAIL: CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: analyzing "olab.olab_par_t1"(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 12 live rows and 22 dead rows; 12 rows in sample, 12 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 30 live rows and 30 dead rows; 30 rows in sample, 30 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 32 live rows and 28 dead rows; 32 rows in sample, 32 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 19 live rows and 0 dead rows; 19 rows in sample, 19 estimated total rows(gaussdb pid=1)
VACUUM
omm=# drop index olab.olab_par_t1_c1_inx;
DROP INDEX
omm=# CREATE INDEX olab.olab_par_t1_c1_inx ON olab.olab_par_t1(c1) GLOBAL;
CREATE INDEX
omm=# EXPLAIN SELECT c1 FROM olab.olab_par_t1 WHERE c1<100;
QUERY PLAN
------------------------------------------------------------------------------
Partition Iterator (cost=0.00..5.16 rows=93 width=5)
Iterations: 4
-> Partitioned Seq Scan on olab_par_t1 (cost=0.00..5.16 rows=93 width=5)
Filter: (c1 < 100::numeric)
Selected Partitions: 1..4
(5 rows)

omm=# drop index olab.olab_par_t1_c1_inx;
DROP INDEX
omm=# CREATE INDEX olab.olab_par_t1_c1_inx ON olab.olab_par_t1(c1) local;
CREATE INDEX
omm=# EXPLAIN SELECT c1 FROM olab.olab_par_t1 WHERE c1<10;
QUERY PLAN
-----------------------------------------------------------------------------
Partition Iterator (cost=0.00..2.16 rows=1 width=5)
Iterations: 1
-> Partitioned Seq Scan on olab_par_t1 (cost=0.00..2.16 rows=1 width=5)
Filter: (c1 < 10::numeric)
Selected Partitions: 1
(5 rows)

omm=# analyze VERBOSE olab.olab_par_t1;
INFO: analyzing "olab.olab_par_t1"(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 12 live rows and 22 dead rows; 12 rows in sample, 12 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 30 live rows and 30 dead rows; 30 rows in sample, 30 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 32 live rows and 28 dead rows; 32 rows in sample, 32 estimated total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "olab_par_t1": scanned 1 of 1 pages, containing 19 live rows and 0 dead rows; 19 rows in sample, 19 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# EXPLAIN SELECT c1 FROM olab.olab_par_t1 WHERE c1<10;
-> Partitioned Seq Scan on olab_par_t1 (cost=0.00..2.16 rows=1 width=5)
Filter: (c1 < 10::numeric)
Selected Partitions: 1
(5 rows)

omm=# QUERY PLAN
-----------------------------------------------------------------------------
Partition Iterator (cost=0.00..2.16 rows=1 width=5)
Iterations: 1

数据量足够大优化器才选择索引方式
omm=# insert into olab.olab_par_t1 values(generate_series(120, 10000));
INSERT 0 9881
omm=# EXPLAIN SELECT c1 FROM olab.olab_par_t1 WHERE c1<10;
-> Partitioned Index Only Scan using olab_par_t1_c1_inx on olab_par_t1 (cost=0.00..8.27 rows=1 w
idth=5)
Index Cond: (c1 < 10::numeric)
Selected Partitions: 1
(5 rows)

QUERY PLAN

-----------------------------------------------------------------------------------------------------
--------
Partition Iterator (cost=0.00..8.27 rows=1 width=5)
Iterations: 1
omm=# drop schema olab cascade;
NOTICE: drop cascades to table olab.olab_par_t1
DROP SCHEMA

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

评论