1、、环境
OS:centos7
DB:postgresql10.11.3 + timescaleDB1.4.1
2、介绍
timescaledb-parallel-copy
是一个命令行程序,用于并行化 PostgreSQL 的内置COPY
功能,用于将数据批量插入TimescaleDB。
该工具的目的是通过 COPY 同时运行多个文件来加快大型数据的迁移。除了并行化工作负载之外,该工具还提供了一些标记来改善复制体验。
3、下载安装
[root@panghu /]# yum install golang
[root@panghu /]# su - postgres
# 临时启用Go Modules 功能
[postgres@panghu ~]$ export GO111MODULE=on
# 临时设置 环境变量
[postgres@panghu ~]$ export GOPROXY=https://goproxy.io,direct
# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔
[postgres@panghu ~]$ export GOPRIVATE=git.mycompany.com,github.com/my/private
[postgres@panghu ~]$ go get github.com/timescale/timescaledb-parallel-copy/cmd/timescaledb-parallel-copy
[postgres@panghu ~]$ cd ./go/bin/
4、使用
将数据从名为data.csv的文件批量插入到名为sample的数据库中的(超)表test中:
#单线程
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv
# 2 个线程
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv --workers 2
# 2 个线程,每30秒输出一次进度
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv --workers 2 --reporting-period 30s
#将文字字符串 'NULL' 视为 NULL:
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv --copy-options " NULL 'NULL' CSV "
属性说明
- batch-size int
每次插入的行数 (默认5000)- columns string
CSV 中使用的逗号分隔列- connection string
PostgreSQL连接参数 (默认使用本地连接"host=localhost user=postgres sslmode=disable")- copy-options string
传递给 COPY 的其他选项 (例如 NULL 'NULL') (默认"CSV")- db-name string
目标表所在的数据库- file string
要读取的文件- header-line-count int
标题行数 (默认是1)- limit int
要插入的总行数, 0 表示全部插入- log-batches
是否对单个批次计时- reporting-period duration
报告插入统计数据的时间间隔, 如果为 0,则不会报告中间结果- schema string
目标表的 schema (默认是"public")- skip-header
跳过输入的第一行- split string
要拆分的字符(默认为“,”)- table string
插入的目标表(默认为“test_table”)- token-size int
令牌的最大大小。 默认情况下,是 64KB,因此任何小于该值的值都将被忽略(默认值 65536)- truncate
在插入之前截断目标表- verbose
打印有关复印统计信息的更多信息- version
显示此工具的版本- workers int
要发出的并行请求数(默认为 1)
真实测试
[postgres@panghu bin]$ ./timescaledb-parallel-copy --db-name=bridge --table=tb_hypertable --verbose --reporting-period=1s --file=/tmp/data_small.csv --connection='host=localhost user=postgres password=yourpwd sslmode=disable' --workers=2
at 1s, row rate 124929.23/sec (period), row rate 124929.23/sec (overall), 1.250000E+05 total rows
at 2s, row rate 139986.70/sec (period), row rate 132456.19/sec (overall), 2.650000E+05 total rows
at 3s, row rate 105004.61/sec (period), row rate 123307.95/sec (overall), 3.700000E+05 total rows
at 4s, row rate 139997.44/sec (period), row rate 127479.74/sec (overall), 5.100000E+05 total rows
at 5s, row rate 130013.91/sec (period), row rate 127986.46/sec (overall), 6.400000E+05 total rows
at 6s, row rate 119749.71/sec (period), row rate 126611.40/sec (overall), 7.600000E+05 total rows
at 7s, row rate 140278.32/sec (period), row rate 128559.77/sec (overall), 9.000000E+05 total rows
COPY 1000000, took 7.603663623s with 2 worker(s) (mean rate 131515.549554/sec)