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

openGauss每日一练第19天

原创 华军 2021-12-24
596

今天学习openGauss收集统计信息、打印执行计划、垃圾收集和checkpoint
1.准备数据

CREATE SCHEMA
omm=# 
omm=# omm(# omm(# omm(# omm(# omm(# omm(# omm(# 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_omm-# city character varying(60) ,
ca_county character varying(30) ,
ca_state character(2) ,
omm(# ca_zip character(10) ,
ca_country character varying(20) omm(# ,
ca_gmt_offset numeric(5,2) ,
omm(# ca_location_type character(20)omm(# 
);omm(# omm(# omm(# 
CREATE TABLE
omm=# 
omm=# 
omm=# 
omm=# 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 omm-# 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'),
(3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suomm-# ite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family'omm-# );
INSERT 0 3
omm=# 
omm=# insert into tpcds.customer_address values(generate_series(10, 10000));
INSERT 0 9991

复制

2.收集统计信息

omm=# select relname, relpages, reltuples from pg_class where relname = 'customer_address';

omm=#      relname      | relpages | reltuples 
------------------+----------+-----------
 customer_address |        0 |         0
(1 row)

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)


复制

3.打印执行计划

omm=# SET explain_perf_mode=normal;
SET
omm=# EXPLAIN SELECT * FROM tpcds.customer_address;
omm=#                               QUERY PLAN                               
-----------------------------------------------------------------------
 Seq Scan on customer_address  (cost=0.00..154.94 rows=9994 width=151)
(1 row)


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,               +
   }                                       +
 ]
(1 row)

omm=#        "Plan Rows": 9994,                  +
       "Plan Width": 151                   +
     }                                     +
 
omm=# EXPLAIN(COSTS FALSE)SELECT * FROM tpcds.customer_address;
          QUERY PLAN          
------------------------------
 Seq Scan on customer_address
(1 row)

omm=# 
omm=# EXPLAIN SELECT SUM(ca_address_sk) FROM tpcds.customer_address WHERE ca_address_sk<100;
   ->  Seq Scan on customer_address  (cost=0.00..179.93 rows=94 width=4)
         Filter: (ca_address_sk < 100)
(3 rows)

omm=#                                QUERY PLAN                                
-------------------------------------------------------------------------
 Aggregate  (cost=180.16..180.17 rows=1 width=12)

omm=# 
omm=# 
omm=# create index customer_address_idx on tpcds.customer_address(ca_address_sk);
CREATE INDEX
omm=# 
omm=# 
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)

omm=# 
omm=# omm=# 

复制

4.垃圾收集

omm=# update tpcds.customer_address set ca_address_sk = ca_address_sk + 1 where ca_address_sk <100;
UPDATE 93
omm=# 
omm=# 
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

复制

5.事务日志检查点

omm=# CHECKPOINT;
CHECKPOINT

复制

6.清理数据

omm=# drop schema tpcds cascade;
NOTICE:  drop cascades to table tpcds.customer_address
DROP SCHEMA

复制

课后作业
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

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

文章被以下合辑收录

评论