os: ubuntu 16.04
postgresql: 9.6.8
citus: postgresql-9.6-citus 8.0.0

ip规划如下:

192.168.0.92 pgsql1 --coordinator 节点

192.168.0.90 pgsql2 --worker 节点
192.168.0.88 pgsql3 --worker 节点

citus 有两种表:

  1. distributed table:分片表,rows会分布在 worker节点中。主要用于大量数据的事实表。
  2. reference table:广播表,每个 worker 节点都保存一模一样的数据。主要用于维度表。

登录 coordinator 创建分片表

$ psql -h 192.168.0.92 -U cituser citusdb
citusdb=# create table tmp_t0(c0 varchar(100),c1 varchar(100));
CREATE TABLE
citusdb=# select create_distributed_table('tmp_t0','c0');create_distributed_table
--------------------------(1 row)

这里简单说明下:
----表分片
select master_create_distributed_table(‘tmp_t0’,‘c0’,‘hash’);
–设定分片个数及每个分片副本数,默认是 citus.shard_count=32,citus.shard_replication_factor=1
select master_create_worker_shards(‘tmp_t0’,2,2);

citusdb=# \dList of relationsSchema |  Name  | Type  |  Owner
--------+--------+-------+---------public | tmp_t0 | table | cituser
(1 row)

pgsql2 节点上查看

citusdb=# \d+List of relationsSchema |     Name      | Type  |  Owner  |  Size   | Description
--------+---------------+-------+---------+---------+-------------public | tmp_t0_102009 | table | cituser | 0 bytes | public | tmp_t0_102011 | table | cituser | 0 bytes | public | tmp_t0_102013 | table | cituser | 0 bytes | public | tmp_t0_102015 | table | cituser | 0 bytes | public | tmp_t0_102017 | table | cituser | 0 bytes | public | tmp_t0_102019 | table | cituser | 0 bytes | public | tmp_t0_102021 | table | cituser | 0 bytes | public | tmp_t0_102023 | table | cituser | 0 bytes | public | tmp_t0_102025 | table | cituser | 0 bytes | public | tmp_t0_102027 | table | cituser | 0 bytes | public | tmp_t0_102029 | table | cituser | 0 bytes | public | tmp_t0_102031 | table | cituser | 0 bytes | public | tmp_t0_102033 | table | cituser | 0 bytes | public | tmp_t0_102035 | table | cituser | 0 bytes | public | tmp_t0_102037 | table | cituser | 0 bytes | public | tmp_t0_102039 | table | cituser | 0 bytes |
(16 rows)

pgsql3 节点上查看

citusdb=# \d+List of relationsSchema |     Name      | Type  |  Owner  |  Size   | Description
--------+---------------+-------+---------+---------+-------------public | tmp_t0_102008 | table | cituser | 0 bytes | public | tmp_t0_102010 | table | cituser | 0 bytes | public | tmp_t0_102012 | table | cituser | 0 bytes | public | tmp_t0_102014 | table | cituser | 0 bytes | public | tmp_t0_102016 | table | cituser | 0 bytes | public | tmp_t0_102018 | table | cituser | 0 bytes | public | tmp_t0_102020 | table | cituser | 0 bytes | public | tmp_t0_102022 | table | cituser | 0 bytes | public | tmp_t0_102024 | table | cituser | 0 bytes | public | tmp_t0_102026 | table | cituser | 0 bytes | public | tmp_t0_102028 | table | cituser | 0 bytes | public | tmp_t0_102030 | table | cituser | 0 bytes | public | tmp_t0_102032 | table | cituser | 0 bytes | public | tmp_t0_102034 | table | cituser | 0 bytes | public | tmp_t0_102036 | table | cituser | 0 bytes | public | tmp_t0_102038 | table | cituser | 0 bytes |
(16 rows)

插入数据

citusdb=# insert into tmp_t0(c0,c1)
select md5(md5((id)::varchar)),md5((id)::varchar) from generate_series(1,2000000) as id;INSERT 0 2000000

pgsql2 节点上查看

citusdb=# \d+List of relationsSchema |     Name      | Type  |  Owner  |  Size   | Description
--------+---------------+-------+---------+---------+-------------public | tmp_t0_102009 | table | cituser | 6216 kB | public | tmp_t0_102011 | table | cituser | 6216 kB | public | tmp_t0_102013 | table | cituser | 6200 kB | public | tmp_t0_102015 | table | cituser | 6200 kB | public | tmp_t0_102017 | table | cituser | 6240 kB | public | tmp_t0_102019 | table | cituser | 6208 kB | public | tmp_t0_102021 | table | cituser | 6160 kB | public | tmp_t0_102023 | table | cituser | 6240 kB | public | tmp_t0_102025 | table | cituser | 6184 kB | public | tmp_t0_102027 | table | cituser | 6160 kB | public | tmp_t0_102029 | table | cituser | 6224 kB | public | tmp_t0_102031 | table | cituser | 6192 kB | public | tmp_t0_102033 | table | cituser | 6176 kB | public | tmp_t0_102035 | table | cituser | 6184 kB | public | tmp_t0_102037 | table | cituser | 6256 kB | public | tmp_t0_102039 | table | cituser | 6216 kB |
(16 rows)

pgsql3 节点上查看

citusdb=# \d+List of relationsSchema |     Name      | Type  |  Owner  |  Size   | Description
--------+---------------+-------+---------+---------+-------------public | tmp_t0_102008 | table | cituser | 6200 kB | public | tmp_t0_102010 | table | cituser | 6184 kB | public | tmp_t0_102012 | table | cituser | 6232 kB | public | tmp_t0_102014 | table | cituser | 6200 kB | public | tmp_t0_102016 | table | cituser | 6168 kB | public | tmp_t0_102018 | table | cituser | 6264 kB | public | tmp_t0_102020 | table | cituser | 6184 kB | public | tmp_t0_102022 | table | cituser | 6200 kB | public | tmp_t0_102024 | table | cituser | 6208 kB | public | tmp_t0_102026 | table | cituser | 6216 kB | public | tmp_t0_102028 | table | cituser | 6176 kB | public | tmp_t0_102030 | table | cituser | 6152 kB | public | tmp_t0_102032 | table | cituser | 6208 kB | public | tmp_t0_102034 | table | cituser | 6184 kB | public | tmp_t0_102036 | table | cituser | 6192 kB | public | tmp_t0_102038 | table | cituser | 6184 kB |
(16 rows)

可以看到,在 worker 节点 pgsql2、pgsql3 上的数据基本是均匀分布的。
coordinator 节点 pgsql1 是不保存任何数据的。如下:

citusdb=# \d+List of relationsSchema |  Name  | Type  |  Owner  |  Size   | Description
--------+--------+-------+---------+---------+-------------public | tmp_t0 | table | cituser | 0 bytes |
(1 row)

执行计划

citusdb=# explain verbose select count(1) from tmp_t0;QUERY PLAN
----------------------------------------------------------------------------------------------------------Aggregate  (cost=0.00..0.00 rows=0 width=0)Output: COALESCE((pg_catalog.sum(remote_scan.count))::bigint, '0'::bigint)->  Custom Scan (Citus Real-Time)  (cost=0.00..0.00 rows=0 width=0)Output: remote_scan.countTask Count: 32Tasks Shown: One of 32->  TaskNode: host=192.168.0.88 port=5432 dbname=citusdb->  Aggregate  (cost=1552.94..1552.95 rows=1 width=8)Output: count(1)->  Seq Scan on public.tmp_t0_102008 tmp_t0  (cost=0.00..1396.75 rows=62475 width=0)Output: c0, c1
(12 rows)

有点看不太明白,为什么只有Node: host=192.168.0.88,刚接触citus,先记录下问题。

多表join

coordinator 节点上创建 tmp_t1

citusdb=# create table tmp_t1(c0 varchar(100),c1 varchar(100));
CREATE TABLE
citusdb=# select create_distributed_table('tmp_t1','c0');create_distributed_table
--------------------------(1 row)citusdb=# insert into tmp_t1(c0,c1)
select md5(md5((id)::varchar)),md5((id)::varchar) from generate_series(1,1000000) as id;
citusdb=#
citusdb=# select count(1) from tmp_t0 t0,tmp_t1 t1 where t0.c0=t1.c0;count
---------1000000
(1 row)
citusdb=# explain select count(1) from tmp_t0 t0,tmp_t1 t1 where t0.c0=t1.c0;QUERY PLAN
-----------------------------------------------------------------------------------------------------------Aggregate  (cost=0.00..0.00 rows=0 width=0)->  Custom Scan (Citus Real-Time)  (cost=0.00..0.00 rows=0 width=0)Task Count: 32Tasks Shown: One of 32->  TaskNode: host=192.168.0.88 port=5432 dbname=citusdb->  Aggregate  (cost=3114.55..3114.56 rows=1 width=8)->  Hash Join  (cost=1091.90..3036.22 rows=31329 width=0)Hash Cond: ((t0.c0)::text = (t1.c0)::text)->  Seq Scan on tmp_t0_102008 t0  (cost=0.00..1396.75 rows=62475 width=33)->  Hash  (cost=700.29..700.29 rows=31329 width=33)->  Seq Scan on tmp_t1_102040 t1  (cost=0.00..700.29 rows=31329 width=33)
(12 rows)citusdb=#

貌似还不错。

参考:
https://www.citusdata.com/
https://docs.citusdata.com/en/v8.0/
https://docs.citusdata.com/en/stable/index.html

citus 之二 distributed table相关推荐

  1. 从零实现Vue的组件库(十二)- Table 实现

    基于Table标签的展示数据组件. Table 组件主要特点在于: 组件 data 的解耦,减少重复代码: 良好的扩展性,可以通过自定义列模板来适应不同的业务场景. 1. 实例 代码 <fat- ...

  2. 天河二号上运行ZHT(a zero-hop distributed table)

    最近做的研究室是空间信息网络中分布式元数据的管理,基于ZHT实现,需要搭建一个集群环境,老师就提供了天河二号,以下是在天河二号上运行ZHT的步骤,作为记录: 使用ZHT首先需要安装protobuf以及 ...

  3. Oracle 学习笔记二 Create table

    1.创建用户 create user hfy identified by hfy; 2.在该用户下建表 建表必须满足权限和表空间: 首先连接sys账户,grant create session to ...

  4. iview table增加一行减少一行_iView学习笔记(二):Table行编辑操作

    1.前端准备工作 首先新建一个项目,然后引入iView插件,配置好router npm安装iView npm install iview --save cnpm install iview --sav ...

  5. Citus中的分片策略:Append Distribution追加分配

    翻译自<Citus官方文档> Append Distribution追加分配 Append distribution is a specialized technique which re ...

  6. Citus 分布式 PostgreSQL 集群 - SQL Reference(创建和修改分布式表 DDL)

    创建和分布表 要创建分布式表,您需要首先定义表 schema. 为此,您可以使用 CREATE TABLE 语句定义一个表,就像使用常规 PostgreSQL 表一样. CREATE TABLE ht ...

  7. 试驾 Citus 11.0 beta

    https://www.citusdata.com/blog/2022/03/26/test-drive-citus-11-beta-for-postgres/ Citus 11.0 beta 的最大 ...

  8. citus 之六 多表join

    os: ubuntu 16.04 postgresql: 9.6.8 citus: postgresql-9.6-citus 8.0.0 ip规划如下: 192.168.0.92 pgsql1 --c ...

  9. Citus 兼容性评估

    文章目录 Citus 兼容性评估 结论 1. 对于postgis的支持: 2. sql功能支持 集群环境下不支持的sql功能 查询: 更新: DDL: 3. 事务支持 执行步骤: 一致性模型:最终一致 ...

最新文章

  1. iOS15实现音乐播放器
  2. 我对于全栈工程师的理解
  3. C++多态的基本语法与原理剖析
  4. 数据 3 分钟 | 《数据安全法》即将施行;ACM SIGMOD在西安举办;艾瑞咨询发布数据库行业报告...
  5. MMO游戏数值框架概述(偏模拟方向)
  6. 设计模式---组件协作模式之观察者模式(Observer)
  7. Android Activity类详解
  8. paip. C#.NET循环获取不同随机数的方法根据时间
  9. 已知两点坐标和半径,求圆心
  10. 主机甲和乙已建立了 TCP 连接,甲始终以 MSS=1KB 大小的段发送数据,并一直有数据 发送;乙每收到一个数据段都会发出一个接收窗口为 10KB 的确认段。若甲在 t 时刻发生超 时时拥塞窗口为
  11. bugku 杂项 QAQ
  12. 对比性句子sentiment analysis
  13. Selenium中的EC模块
  14. 实例:用C#.NET手把手教你做微信公众号开发(13)--事件消息处理之取消关注
  15. GNU/Linux闲话
  16. 查看Windows凭据和普通凭据的密码(查看Windows中存储的密码)——mimikatz
  17. imshow 显示图像(Matlab)
  18. 基于tensorflow、CNN网络识别花卉的种类(图像识别)
  19. MySql各版本介绍
  20. SQL Dblink SQL

热门文章

  1. 计算机文档制作教程,计算机实操课:用word制作电子小报教程.doc
  2. 3dmax拾取路径怎么用(3DMAX路径工具使用教程)
  3. Android Banner(无限轮播图)控件的简单使用(网络数据OkHttp3)
  4. python数据透视表计数去除重复_excel透视表计数去重_Excel数据透视表中的唯一计数...
  5. SpringBoot之校园订餐系统全部源码,一键运行
  6. Adobe Audition 入门系列(合集)
  7. 硬盘结构损坏且无法读取怎么办
  8. 流量矿石:经历九死一生,曾获王欣亲自嘉奖,被5亿快播用户追捧
  9. 计算机毕业优秀作品展观后感,优秀作业展的观后感(精选6篇)
  10. 通信中隧道技术的解释