前提・実現したいこと
・originalテーブルにある150万件のデータを、categoryテーブルに
移そうとしているんですが、遅すぎるので改善したいです。
・現在、10件あたりの処理に5秒ほど掛かってしまうので改善したいです。
・PREPAREやCOPY等も調べましたが、重複処理をどうすればいいのか分からなかったです。
発生している問題・エラーメッセージ
・テーブルからテーブルにデータを移す処理は正常に動いています。
・テーブル1行ごとにinsertの処理をしてしまっているので
速度が遅くなってしまっています。
該当のソースコード
言語:PostgreSQL
ソースコード
lang
1 2create or replace function insert_into_category( 3 Count INTEGER default null 4) 5RETURNS integer AS $$ 6DECLARE 7 --Forを回す回数. 8 loop_max integer; 9 --ForのLoop回数. 10 loop_count 11 INTEGER; 12 --カテゴリー名. 13 category_word character varying(255); 14 rec RECORD; 15 16BEGIN 17 --originalテーブルのカラムの大きさを調べる. 18 select into loop_max count(id) from original; 19 --loop_countの初期化 20 loop_count:= 0; 21 --Countを初期化 22 Count:=null; 23 24 FOR rec IN 0..loop_max LOOP 25 26 --category_wordに1つ目のカテゴリーを入れる。 27 select into category_word 28 (select split_part(category_name,E'/',1):: character varying(255) as category_name) 29 from original 30 where id =loop_count; 31 32 --1つ目のカテゴリーをinsert(同じカラムにすでに同じカテゴリーが存在する場合はinsertしない) 33 insert into 34 category (name) 35 select category_word 36 from original 37 where NOT EXISTS (SELECT name FROM Category WHERE name = category_word) LIMIT 1; 38 39 --categoryテーブルのname_allへoriginalテーブルのnameを上書き 40 update category 41 set name_all = (select name from original where id = loop_count) 42 where 43 EXISTS(select name from category where name = category_word) and name = category_word; 44 45 loop_count:=loop_count+1; 46 47 END LOOP; 48 return 0; 49END; 50$$ LANGUAGE plpgsql; 51 52select insert_into_category(); 53select * from category order by id asc;
試したこと
COPY文を使って1度csvで出力して、csvで読み込もうとしました。
しかし、重複チェックをCOPY文でどうやればいいのか分からなくて止まっています。
補足情報(FW/ツールのバージョンなど)
<環境>
PostgreSql9.6,pgAdmin3
テーブルのSQL文も記載します。
lang
1 2--category 3create table category ( 4 name_all character varying(255) 5 , id serial not null 6 , parent integer 7 , name character varying(255) 8 , constraint category_PKC primary key (id) 9) ; 10 11create unique index category_pki 12 on category(id); 13 14create index parent_id_index 15 on category(parent); 16 17-- original 18create table original ( 19 id integer not null 20 , name character varying(255) 21 , condition_id integer 22 , category_name character varying(255) 23 , brand character varying(255) 24 , price double precision 25 , shipping integer 26 , description text 27 , constraint original_PKC primary key (id) 28) ; 29 30create index brand_index 31 on original(brand); 32 33create unique index original_pki 34 on original(id); 35 36comment on table category is 'category'; 37comment on column category.name_all is 'name_all'; 38comment on column category.id is 'id'; 39comment on column category.parent is 'parent'; 40comment on column category.name is 'name'; 41 42comment on table original is 'original'; 43comment on column original.id is 'id'; 44comment on column original.name is 'name'; 45comment on column original.condition_id is 'condition_id'; 46comment on column original.category_name is 'category_name'; 47comment on column original.brand is 'brand'; 48comment on column original.price is 'price'; 49comment on column original.shipping is 'shipping'; 50comment on column original.description is 'description';
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/27 10:10