Ama*_*rus 2 postgresql optimization import timestamp copy
PostgreSQL 的COPY命令对于快速导入大量数据非常有用,并且数据必须采用数据类型的文本表示形式。
我正在导入大量数据,包括一timestamp列,但它存储为“unix time”,即自纪元以来的秒数。我可以将其转换为ISO 8601(例如2010-01-01 00:00:00,并且 PostgreSQL 接受 a 的转换timestamp。它不接受原始纪元值整数。
是否可以让 postgres 接受纪元整数值并将其解释/转换为时间戳?这将使我的代码更简单(也许更快)。
这适用于psql:
create temporary table test1 ( v1 timestamp );
copy test1 from stdin ;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 2010-01-01 00:00:00
>> \.
COPY 1
Run Code Online (Sandbox Code Playgroud)
但这些没有:
copy test1 from stdin ;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 946684800
>> \.
ERROR: date/time field value out of range: "946684800"
HINT: Perhaps you need a different "datestyle" setting.
CONTEXT: COPY test1, line 1, column v1: "946684800"
copy test1 from stdin ;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> epoch + 946684800
>> \.
ERROR: time zone displacement out of range: "epoch + 946684800"
CONTEXT: COPY test1, line 1, column v1: "epoch + 946684800"
Run Code Online (Sandbox Code Playgroud)
该to_timestamp函数能够接受“整数”或该整数的字符串文本。DateStyle我看不到这样的设置选项。我尝试过将列作为一种timestamp with time zone类型,得到相同的结果。
我目前在 Ubuntu Linux 上运行 PostgreSQL 10,但如果需要的话可以升级到 11(或很快升级到 12)。
独自一人是没有办法做到这一点的COPY。您必须对数据进行预处理或后处理。
也许UNLOGGED表格可以让这个过程更快一些:
/* no WAL logging for better performance */
CREATE UNLOGGED TABLE test1 (v1 double precision);
COPY test1 FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 946684800
>> \.
/* this will rewrite the table */
ALTER TABLE test1
ALTER v1 TYPE timestamp without time zone
USING (to_timestamp(v1) AT TIME ZONE 'UTC');
/* this will dump the table to the transaction log */
ALTER TABLE test1 SET LOGGED;
Run Code Online (Sandbox Code Playgroud)
这将使您能够将中间操作排除在事务日志之外,这对性能有好处。
| 归档时间: |
|
| 查看次数: |
1958 次 |
| 最近记录: |