1、汉匙、環(huán)境
OS:centos7
DB:postgresql10.11.3 + timescaleDB1.4.1
2拱烁、介紹
timescaledb-parallel-copy
是一個(gè)命令行程序,用于并行化 PostgreSQL 的內(nèi)置COPY
功能噩翠,用于將數(shù)據(jù)批量插入TimescaleDB戏自。
該工具的目的是通過(guò) COPY 同時(shí)運(yùn)行多個(gè)文件來(lái)加快大型數(shù)據(jù)的遷移。除了并行化工作負(fù)載之外伤锚,該工具還提供了一些標(biāo)記來(lái)改善復(fù)制體驗(yàn)擅笔。
3、下載安裝
[root@panghu /]# yum install golang
[root@panghu /]# su - postgres
# 臨時(shí)啟用Go Modules 功能
[postgres@panghu ~]$ export GO111MODULE=on
# 臨時(shí)設(shè)置 環(huán)境變量
[postgres@panghu ~]$ export GOPROXY=https://goproxy.io,direct
# 還可以設(shè)置不走 proxy 的私有倉(cāng)庫(kù)或組,多個(gè)用逗號(hào)相隔
[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剂娄、使用
將數(shù)據(jù)從名為data.csv的文件批量插入到名為sample的數(shù)據(jù)庫(kù)中的(超)表test中:
#單線程
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv
# 2 個(gè)線程
[postgres@panghu ~]$ timescaledb-parallel-copy --db-name test --table sample --file data.csv --workers 2
# 2 個(gè)線程蠢涝,每30秒輸出一次進(jìn)度
[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 "
屬性說(shuō)明
- batch-size int
每次插入的行數(shù) (默認(rèn)5000)- columns string
CSV 中使用的逗號(hào)分隔列- connection string
PostgreSQL連接參數(shù) (默認(rèn)使用本地連接"host=localhost user=postgres sslmode=disable")- copy-options string
傳遞給 COPY 的其他選項(xiàng) (例如 NULL 'NULL') (默認(rèn)"CSV")- db-name string
目標(biāo)表所在的數(shù)據(jù)庫(kù)- file string
要讀取的文件- header-line-count int
標(biāo)題行數(shù) (默認(rèn)是1)- limit int
要插入的總行數(shù), 0 表示全部插入- log-batches
是否對(duì)單個(gè)批次計(jì)時(shí)- reporting-period duration
報(bào)告插入統(tǒng)計(jì)數(shù)據(jù)的時(shí)間間隔阅懦, 如果為 0和二,則不會(huì)報(bào)告中間結(jié)果- schema string
目標(biāo)表的 schema (默認(rèn)是"public")- skip-header
跳過(guò)輸入的第一行- split string
要拆分的字符(默認(rèn)為“,”)- table string
插入的目標(biāo)表(默認(rèn)為“test_table”)- token-size int
令牌的最大大小。 默認(rèn)情況下耳胎,是 64KB惯吕,因此任何小于該值的值都將被忽略(默認(rèn)值 65536)- truncate
在插入之前截?cái)嗄繕?biāo)表- verbose
打印有關(guān)復(fù)印統(tǒng)計(jì)信息的更多信息- version
顯示此工具的版本- workers int
要發(fā)出的并行請(qǐng)求數(shù)(默認(rèn)為 1)
真實(shí)測(cè)試
[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)