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

openGauss每日一练第21天|学习心得

原创 Alex 2021-12-21
210

今天最后一天学习,学习openGauss存储模型-行存和列存

1.背景

   行存储是指将表按行存储到硬盘分区上,列存储是指将表按列存储到硬盘分区上。默认情况下,创建的表为行存储。
   列存储模型各有优劣,通常用于TP场景的数据库,默认使用行存储,仅对执行复杂查询且数据量大的AP场景时,才使用列存储

2.连接数据库
#第一次进入等待15秒
#数据库启动中...
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.

3.创建行表

omm=# CREATE TABLE test_t1
omm-# (
omm(# omm(# col1 CHAR(2),
omm(# col3 NUMBER
omm(# );col2 VARCHAR2(40),

omm=# CREATE TABLE

查看表信息

omm=# \d+ test_t1
Table "public.test_t1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
col1 | character(2) | | extended | |
col2 | character varying(40) | | extended | |
col3 | numeric | | main | |
Has OIDs: no
Options: orientation=row, compression=no

插入测试数据10W行
omm=# insert into test_t1 select col1, col2, col3 from (select generate_series(1, 100000) as key, repeat(chr(int4(random() * 26) + 65), 2) as col1, repeat(chr(int4(random() * 26) + 65), 30) as col2, (random() * (10^4))::integer as col3);


INSERT 0 100000


omm=# CREATE TABLE test_t2
omm-# (
omm(# col1 CHAR(2),
omm(# col2 VARCHAR2(40),
omm(# col3 NUMBER
omm(# )
omm-# WITH (ORIENTATION = COLUMN);
CREATE TABLE

查看表信息
omm=# \d+ test_t2;
--------+-----------------------+-----------+----------+--------------+-------------
col1 | character(2) | | extended | |
col2 | character varying(40) | | extended | |
col3 | numeric | | main | |
Has OIDs: no
Options: orientation=column, compression=low

omm=# Table "public.test_t2"
Column | Type | Modifiers | Storage | Stats target | Description


插入测试数据
omm=# insert into test_t2 select * from test_t1;
INSERT 0 100000


4.查看一下占用空间对比
omm=# \d+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+---------+-------+-------+---------+--------------------------------------+-------------
public | test_t1 | table | omm | 6760 kB | {orientation=row,compression=no} |
public | test_t2 | table | omm | 1112 kB | {orientation=column,compression=low} |
(2 rows)

5.对比读取一列的速度
omm=# analyze VERBOSE test_t1;
INFO: analyzing "public.test_t1"(gaussdb pid=1)
INFO: ANALYZE INFO : "test_t1": scanned 841 of 841 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows(gaussdb pid=1)
ANALYZE

omm=# analyze VERBOSE test_t2;
INFO: analyzing "public.test_t2"(gaussdb pid=1)
INFO: ANALYZE INFO : estimate total rows of "pg_delta_16395": scanned 0 pages of total 0 pages with 1 retry times, containing 0 live rows and 0 dead rows, estimated 0 total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "test_t2": scanned 2 of 2 cus, sample 30000 rows, estimated total 100000 rows(gaussdb pid=1)
ANALYZE

6.列存表时间少于行存表

omm=# explain analyze select distinct col1 from test_t1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=2091.00..2091.27 rows=27 width=3) (actual time=51.823..51.827 rows=27 loops=1)
Group By Key: col1
-> Seq Scan on test_t1 (cost=0.00..1841.00 rows=100000 width=3) (actual time=0.013..24.748 rows=100000 loops=1)
Total runtime: 51.888 ms
(4 rows)

omm=# explain analyze select distinct col1 from test_t2;
Row Adapter (cost=1008.27..1008.27 rows=27 width=3) (actual time=4.218..4.222 rows=27 loops=1)
-> Vector Sonic Hash Aggregate (cost=1008.00..1008.27 rows=27 width=3) (actual time=4.215..4.215 rows=27 loops=1)
Group By Key: col1
-> CStore Scan on test_t2 (cost=0.00..758.00 rows=100000 width=3) (actual time=0.030..0.281 rows=100000 loops=1)
Total runtime: 4.325 ms
(5 rows)

omm=# QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------


7.对比插入一行的速度

行存表时间少于列存表
omm=# explain analyze insert into test_t1 values('x', 'xxxx', '123');
Insert on test_t1 (cost=0.00..0.01 rows=1 width=0) (actual time=0.063..0.063 rows=1 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
Total runtime: 0.158 ms
(4 rows)

omm=# QUERY PLAN
-----------------------------------------------------------------------------------------------
[Bypass]
explain analyze insert into test_t2 values('x', 'xxxx', '123');
QUERY PLAN
-----------------------------------------------------------------------------------------------
Insert on test_t2 (cost=0.00..0.01 rows=1 width=0) (actual time=2.858..2.860 rows=1 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
Total runtime: 2.942 ms
(3 rows)


8.清理数据

omm=# drop table test_t1;
DROP TABLE
omm=# drop table test_t2;
omm=# DROP TABLE



课程作业

1.创建行存表和列存表,并批量插入10万条数据(行存表和列存表数据相同)

行存表
omm=# CREATE TABLE test_rows
omm-# (
omm(# col1 CHAR(2),
omm(# omm(# col3 NUMBER
omm(# );
col2 VARCHAR2(40),
CREATE TABLE

omm=# insert into test_rows select col1, col2, col3 from (select generate_series(1, 100000) as key, repeat(chr(int4(random() * 26) + 65), 2) as col1, repeat(chr(int4(random() * 26) + 65), 30) as col2, (random() * (10^4))::integer as col3);
INSERT 0 100000


CREATE TABLE test_columns
omm=# col1 CHAR(2),
omm(# omm-# (
omm(# col2 VARCHAR2(40),
omm(# omm(# )
omm-# col3 NUMBER
WITH (ORIENTATION = COLUMN);
CREATE TABLE
omm=# insert into test_columns select col1, col2, col3 from (select generate_series(1, 100000) as key, repeat(chr(int4(random() * 26) + 65), 2) as col1, repeat(chr(int4(random() * 26) + 65), 30) as col2, (random() * (10^4))::integer as col3);


INSERT 0 100000




2.对比行存表和列存表空间大小

omm=# \d+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+--------------+-------+-------+---------+--------------------------------------+-------------
public | test_columns | table | omm | 1112 kB | {orientation=column,compression=low} |
public | test_rows | table | omm | 6760 kB | {orientation=row,compression=no} |
(2 rows)



3.对比查询一列和插入一行的速度
omm=# analyze VERBOSE test_rows;
INFO: analyzing "public.test_rows"(gaussdb pid=1)
INFO: ANALYZE INFO : "test_rows": scanned 841 of 841 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows(gaussdb pid=1)
ANALYZE
omm=# analyze VERBOSE test_columns;
INFO: analyzing "public.test_columns"(gaussdb pid=1)
INFO: ANALYZE INFO : estimate total rows of "pg_delta_16417": scanned 0 pages of total 0 pages with 1 retry times, containing 0 live rows and 0 dead rows, estimated 0 total rows(gaussdb pid=1)
INFO: ANALYZE INFO : "test_columns": scanned 2 of 2 cus, sample 30000 rows, estimated total 100000 rows(gaussdb pid=1)
ANALYZE
–列存表时间少于行存表
omm=# explain analyze select distinct col1 from test_rows;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=2091.00..2091.27 rows=27 width=3) (actual time=64.621..64.626 rows=27 loops=1)
Group By Key: col1
-> Seq Scan on test_rows (cost=0.00..1841.00 rows=100000 width=3) (actual time=0.028..37.557 rows=100000 loops=1)
Total runtime: 64.690 ms
(4 rows)

omm=# explain analyze select distinct col1 from test_columns;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Row Adapter (cost=1008.27..1008.27 rows=27 width=3) (actual time=4.240..4.244 rows=27 loops=1)
-> Vector Sonic Hash Aggregate (cost=1008.00..1008.27 rows=27 width=3) (actual time=4.238..4.238 rows=27 loops=1)
Group By Key: col1
-> CStore Scan on test_columns (cost=0.00..758.00 rows=100000 width=3) (actual time=0.035..0.300 rows=100000 loops=1)
omm=# Total runtime: 4.345 ms
(5 rows)





–行存表时间少于列存表
omm=# explain analyze insert into test_rows values('x', 'xxxx', '123');
QUERY PLAN
-------------------------------------------------------------------------------------------------
omm=# [Bypass]
Insert on test_rows (cost=0.00..0.01 rows=1 width=0) (actual time=0.087..0.089 rows=1 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
Total runtime: 0.177 ms
(4 rows)

explain analyze insert into test_columns values('x', 'xxxx', '123');
QUERY PLAN
----------------------------------------------------------------------------------------------------
omm=# Insert on test_columns (cost=0.00..0.01 rows=1 width=0) (actual time=2.738..2.740 rows=1 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)
Total runtime: 2.824 ms
(3 rows)





4.清理数据

drop table test_rows;
drop table test_columns;

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

评论