Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 57C8A1337BEB for ; Sun, 28 Nov 2010 15:09:44 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 55635-01-8 for ; Sun, 28 Nov 2010 19:09:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out.mail.amis.hr (out1.mail.amis.hr [188.129.0.4]) by mail.postgresql.org (Postfix) with ESMTP id D69451337C43 for ; Sun, 28 Nov 2010 15:08:23 -0400 (AST) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out.mail.amis.hr (Postfix) with ESMTP id 03D65203AA; Sun, 28 Nov 2010 20:08:23 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id oASIvZ5j006512; Sun, 28 Nov 2010 20:08:22 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id su1dd0hfp-1; Sun, 28 Nov 2010 20:08:22 +0100 A-Original-Client-UID: 188.129.74.222 splivalo Received: from [192.168.10.1] (cpe-188-129-74-222.dynamic.amis.hr [188.129.74.222]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id 73546261; Sun, 28 Nov 2010 20:08:22 +0100 (CET) Message-ID: <4CF2A8A6.4010400@megafon.hr> Date: Sun, 28 Nov 2010 20:08:22 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 MIME-Version: 1.0 To: Pierre C CC: pgsql-performance@postgresql.org Subject: Re: SELECT INTO large FKyed table is slow References: <4CF24103.8030209@megafon.hr> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2010-11-28_06:2010-11-27, 2010-11-28, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1011280098 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201011/390 X-Sequence-Number: 41414 On 11/28/2010 07:56 PM, Pierre C wrote: > >> When I remove foreign constraints (drones_history_fk__samples and >> drones_history_fk__drones) (I leave the primary key on drones_history) >> than that INSERT, even for 50k rows, takes no more than a second. >> >> So, my question is - is there anything I can do to make INSERTS with >> PK faster? Or, since all the reference checking is done inside the >> procedure for loading data, shall I abandon those constraints entirely? >> >> Mario > > Maybe... or not. Can you post details about : > > - the foreign keys > - the tables that are referred to (including indexes) I pasted DDL at the begining of my post. The only indexes tables have are the ones created because of PK constraints. Table drones has around 100k rows. Table drones_history has around 30M rows. I'm not sure what additional info you'd want but I'll be more than happy to provide more relevant information. > CREATE TABLE foo (x INTEGER PRIMARY KEY); I > generate_series( 1,100000 ); > Temps : 766,182 ms > test=> VACUUM ANALYZE foo; > Temps : 71,938 ms > test=> CREATE TABLE bar ( x INTEGER REFERENCES foo(x) ); > CREATE TABLE > test=> INSERT INTO bar SELECT * FROM generate_series( 1,100000 ); > Temps : 2834,430 ms > > As you can see, 100.000 FK checks take less than 3 seconds on this very > simple example. There is probably something that needs fixing. Yes, when the FKyed table is small enough inserts are quite fast. But when they grow larger the whole system slows down. I just repeated your test and I'm getting similar results - on my desktop. I'll try to assemble some code to recreate workload and see if I'll run into same problems. Mario