博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL DBA(82) - PG 12 Improving COPY
阅读量:2503 次
发布时间:2019-05-11

本文共 3540 字,大约阅读时间需要 11 分钟。

Copy命令在PG 12有所增强,在COPY FROM时可添加WHERE条件过滤.

PG 11

Copy命令

testdb=# \help copyCommand:     COPYDescription: copy data between a file and a tableSyntax:COPY table_name [ ( column_name [, ...] ) ]    FROM { 'filename' | PROGRAM 'command' | STDIN }    [ [ WITH ] ( option [, ...] ) ]COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }    TO { 'filename' | PROGRAM 'command' | STDOUT }    [ [ WITH ] ( option [, ...] ) ]where option can be one of:    FORMAT format_name    OIDS [ boolean ]    FREEZE [ boolean ]    DELIMITER 'delimiter_character'    NULL 'null_string'    HEADER [ boolean ]    QUOTE 'quote_character'    ESCAPE 'escape_character'    FORCE_QUOTE { ( column_name [, ...] ) | * }    FORCE_NOT_NULL ( column_name [, ...] )    FORCE_NULL ( column_name [, ...] )    ENCODING 'encoding_name'

简单使用

testdb=# drop table if exists t_copy;DROP TABLEtestdb=# CREATE TABLE t_copy(id int,c1 varchar(20));CREATE TABLEtestdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;INSERT 0 1000testdb=# testdb=# COPY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';COPY 1000testdb=# drop table if exists t_import;DROP TABLEtestdb=# CREATE TABLE t_import(id int,c1 varchar(20));CREATE TABLEtestdb=# COPY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|';COPY 1000testdb=# select * from t_import limit 10; id |  c1   ----+-------  1 | c1-1  2 | c1-2  3 | c1-3  4 | c1-4  5 | c1-5  6 | c1-6  7 | c1-7  8 | c1-8  9 | c1-9 10 | c1-10(10 rows)

不支持WHERE条件过滤

testdb=# COPY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' where id < 5;ERROR:  syntax error at or near "where"LINE 1: ...t FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' where id <...

PG 12

COPY命令语法

[local]:5432 pg12@testdb=# \help copyCommand:     COPYDescription: copy data between a file and a tableSyntax:COPY table_name [ ( column_name [, ...] ) ]    FROM { 'filename' | PROGRAM 'command' | STDIN }    [ [ WITH ] ( option [, ...] ) ]    [ WHERE condition ]COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }    TO { 'filename' | PROGRAM 'command' | STDOUT }    [ [ WITH ] ( option [, ...] ) ]where option can be one of:    FORMAT format_name    FREEZE [ boolean ]    DELIMITER 'delimiter_character'    NULL 'null_string'    HEADER [ boolean ]    QUOTE 'quote_character'    ESCAPE 'escape_character'    FORCE_QUOTE { ( column_name [, ...] ) | * }    FORCE_NOT_NULL ( column_name [, ...] )    FORCE_NULL ( column_name [, ...] )    ENCODING 'encoding_name'URL: https://www.postgresql.org/docs/12/sql-copy.html

支持WHERE条件过滤

[local]:5432 pg12@testdb=# drop table if exists t_copy;DROP TABLETime: 50.327 ms[local]:5432 pg12@testdb=# CREATE TABLE t_copy(id int,c1 varchar(20));CREATE TABLETime: 5.038 ms[local]:5432 pg12@testdb=# insert into t_copy SELECT x,'c1-'||x FROM generate_series(1, 1000) AS x;INSERT 0 1000Time: 16.422 ms[local]:5432 pg12@testdb=# [local]:5432 pg12@testdb=# COPY t_copy TO '/tmp/data/t_copy.txt' with DELIMITER '|';COPY 1000Time: 4.795 ms[local]:5432 pg12@testdb=# drop table if exists t_import;DROP TABLETime: 4.798 ms[local]:5432 pg12@testdb=# CREATE TABLE t_import(id int,c1 varchar(20));CREATE TABLETime: 2.462 ms[local]:5432 pg12@testdb=# COPY t_import FROM '/tmp/data/t_copy.txt'  with DELIMITER '|' WHERE id < 5;COPY 4Time: 4.842 ms[local]:5432 pg12@testdb=# select * from t_import; id |  c1  ----+------  1 | c1-1  2 | c1-2  3 | c1-3  4 | c1-4(4 rows)Time: 6.103 ms

参考资料

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2654243/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-2654243/

你可能感兴趣的文章
shell 获得后台进程返回值
查看>>
python 多线程_thread
查看>>
SpringBoot2.0 + SpringCloud Eureka搭建高可用注册中心(Eureka之三)
查看>>
vue.js 组件-全局组件和局部组件
查看>>
svn 分支与合并的使用
查看>>
intellj idea
查看>>
浮动(clear)
查看>>
HierarchicalDataTemplate
查看>>
关于 keybd_event (vb篇)
查看>>
记公司的原型设计软件培训课程
查看>>
C语言知识点
查看>>
猴子吃桃
查看>>
tesseract-ocr图像识别技术(一)
查看>>
vs下dll与exe调试
查看>>
排列——递归
查看>>
cocos2d lua的cclog 在logcat中显示
查看>>
雅礼学习10.4
查看>>
Dota2 荒神罪 破解
查看>>
TP控制器的操作
查看>>
Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
查看>>