聊一聊数据库的行存与列存
原文:my.oschina.net/gaussdb/blog/5544252
存储方式比较
优缺点比较
行存 |
列存 |
|
优点 |
数据被保存在一起。INSERT/UPDATE 容易。 |
|
缺点 |
选择 (Selection) 时即使只涉及某几列,所有数据也都会被读取。 |
|
适用场景 |
|
|
行存与列存实验
创建行存表 custom1 和列存表 custom2 ,插入 50 万条记录。
openGauss=# create table custom1 (id integer,name varchar2(20)); CREATE TABLE openGauss=# create table custom2 (id integer,name varchar2(20)) with (orientation = column); CREATE TABLE openGauss=# insert into custom1 select n,'testtt'||n from generate_series(1,500000) n; INSERT 500000 openGauss=# insert into custom2 select * from custom1; INSERT 500000openGauss=# \d+ List of relations Schema | Name | Type | Owner | Size | Storage | Description --------+------------+-------+-------+------------+--------------------------------------+------------- public | custom1 | table | omm | 24 MB | {orientation=row,compression=no} | public | custom2 | table | omm | 3104 kB | {orientation=column,compression=low} |openGauss=# explain analyze insert into custom1 values(1,'zhang3'); QUERY PLAN ----------------------------------------------------------------------------------------------- [Bypass] Insert on custom1 (cost=0.00..0.01 rows=1 width=) (actual time=0.059..0.060 rows=1 loops=1) -> Result (cost=0.00...01 rows=1 width=) (actual time=0.001...001 rows=1 loops=1) Total runtime: .135 ms (4 rows) openGauss=# explain analyze insert into custom2 values(1,'zhang3'); QUERY PLAN ----------------------------------------------------------------------------------------------- Insert on custom2 (cost=0.00..0.01 rows=1 width=) (actual time=0.119..0.120 rows=1 loops=1) -> Result (cost=0.00...01 rows=1 width=) (actual time=0.001...002 rows=1 loops=1) Total runtime: .207 ms (3 rows)后删除测试表。
openGauss=# drop table custom1; DROP TABLE openGauss=#drop table custom2; DROP TABLE选择建议
更新频繁程度:数据如果频繁更新,选择行存表。
插入频繁程度:频繁的少量插入,选择行存表。一次插入大批量数据,选择列存表。
表的列数:一般情况下,如果表的字段比较多即列数多(大宽表),查询中涉及到的列不多的情况下,适合列存储。如果表的字段个数比较少,查询大部分字段,那么选择行存储比较好。
查询的列数:如果每次查询时,只涉及了表的少数(<50% 总列数)几个列,选择列存表。(不要问剩下的列干啥用,甲方说有用就是有用。)
压缩率:列存表比行存表压缩率高。但高压缩率会消耗更多的 CPU 资源。
注意事项
相关文章