PostgreSQL COPY To STDOUT需要大量时间来处理大量数据

时间:2017-10-30 08:01:01

标签: node.js postgresql

我有4个表,每个表中有近770K行,我使用COPY TO STDOUT命令进行数据备份。我使用pg模块进行数据库连接。以下代码显示了数据库客户端连接:

  var client = new pg.Client({user: 'dbuser', database: 'dbname'});
  client.connect();

以下是用于备份的代码

  var stream1 = client.copyTo("COPY table_name TO STDOUT WITH CSV");
  stream1.on('data', function (chunk) {
    rows = Buffer.concat([rows, chunk]);
  });

  stream1.on('end', function () {
    myStream._write(rows, 'hex', function(){
      rows = new Buffer(0);
      return cb(null, 1);
    });
  });

  stream1.on('error', function (error) {
    debug('Error: ' + error);
    return cb(error, null);
  });

myStream是继承stream.Writable的类的对象。 myStream._write()将连接缓冲区中的流,最后缓冲区中的数据存储在文件中。

它适用于少量数据,但对于大数据需要大量时间

我正在使用 PostgreSQL 9.3.4 NodeJS v0.10.33

create table声明是:

CREATE TABLE table_name
(
  id serial NOT NULL,
  date_time timestamp without time zone,
  dev_id integer,
  type integer,
  value1 double precision,
  value2 double precision,
  value3 double precision,
  value4 double precision,
  message character varying(255),
  created_at timestamp without time zone NOT NULL,
  updated_at timestamp without time zone NOT NULL,
)

这是执行计划:

dbname=# explain (analyze, verbose, buffers) select * from table_name;
                                                         QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
 Seq Scan on public.table_name  (cost=0.00..18022.19 rows=769819 width=105) (actual time=0.047..324.202 rows=769819 loops=1)
 Output: id, date_time, dev_id, type, value1, value2, value3, value4, message, created_at, updated_at
 Buffers: shared hit=10324
 Total runtime: 364.909 ms
(4 rows)

0 个答案:

没有答案