Bulk insert hundreds of millions of records

What is the fastest way to insert 237 million records into a rules table (to distribute data among child tables)?

I tried:

The inserts are too slow (four days) and COPY FROMignore the rules (and have other problems).

Sample data:

station_id,taken,amount,category_id,flag
1,'1984-07-1',0,4,
1,'1984-07-2',0,4,
1,'1984-07-3',0,4,
1,'1984-07-4',0,4,T

Table structure (with one rule enabled):

CREATE TABLE climate.measurement
(
  id bigserial NOT NULL,
  station_id integer NOT NULL,
  taken date NOT NULL,
  amount numeric(8,2) NOT NULL,
  category_id smallint NOT NULL,
  flag character varying(1) NOT NULL DEFAULT ' '::character varying
)
WITH (
  OIDS=FALSE
);
ALTER TABLE climate.measurement OWNER TO postgres;

CREATE OR REPLACE RULE i_measurement_01_001 AS
    ON INSERT TO climate.measurement
   WHERE date_part('month'::text, new.taken)::integer = 1 AND new.category_id = 1 DO INSTEAD  INSERT INTO climate.measurement_01_001 (id, station_id, taken, amount, category_id, flag)
  VALUES (new.id, new.station_id, new.taken, new.amount, new.category_id, new.flag);

The data was originally in MySQL, but to improve performance it should be translated into PostgreSQL (and use the PL / R extension).

Thank!

+3
source share
3 answers

COPY, . , , - , . , , .

, . , , (, , ).

, date_part() , postgres , , . - , substr($date,5,2) ( ): , ?

, COPY . , COPY: , - "id" . ( " ... csv", , , , , ... "" ).

280e6 postgresql , , , . fsync = off; , , . checkpoint_segments = 40, . dev- - , , xlogs (.. ). shared_buffers 1Gb, checkpoint_target - 0.5. , , , , .

1,7e9 , ... , .

+9
  • , (create table some_data (c_1 int, c_2 varchar,....))
  • "like" ( some_data_X some_data)
  • some_data_X
  • , .. ( postgresql)
  • ! , 400000-500000 10 (2 xeon, 24 , 24 Gb , SSD).

: separete (some_data_X min X): !

+2

The PostgreSQL documentation contains a database fill page that can help you after you followed the araqnid advice to preprocess the input so you can use it COPY.

0
source

Source: https://habr.com/ru/post/1745606/


All Articles