Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 772161337BA0 for ; Wed, 1 Dec 2010 04:43:18 -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 57061-08 for ; Wed, 1 Dec 2010 08:43:10 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 30.mail-out.ovh.net (30.mail-out.ovh.net [213.186.62.213]) by mail.postgresql.org (Postfix) with SMTP id C39521337B49 for ; Wed, 1 Dec 2010 04:43:09 -0400 (AST) Received: (qmail 22011 invoked by uid 503); 1 Dec 2010 08:18:58 -0000 Received: from b9.ovh.net (HELO mail632.ha.ovh.net) (213.186.33.59) by 30.mail-out.ovh.net with SMTP; 1 Dec 2010 08:18:58 -0000 Received: from b0.ovh.net (HELO queueout) (213.186.33.50) by b0.ovh.net with SMTP; 1 Dec 2010 10:43:09 +0200 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO apollo13) (lists%peufeu.com@88.161.102.87) by ns0.ovh.net with SMTP; 1 Dec 2010 10:43:07 +0200 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Mario Splivalo" Cc: mark.kirkwood@catalyst.net.nz, pgsql-performance@postgresql.org Subject: Re: SELECT INTO large FKyed table is slow References: <4CF24103.8030209@megafon.hr> <4CF3522B.2050203@catalyst.net.nz> <4CF39CF4.2050506@megafon.hr> <4CF587B0.5020003@megafon.hr> Date: Wed, 01 Dec 2010 09:43:07 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Pierre C" Message-ID: In-Reply-To: <4CF587B0.5020003@megafon.hr> User-Agent: Opera Mail/10.62 (Linux) X-Ovh-Tracer-Id: 764204563190777462 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|U 0.5/N 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, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201012/5 X-Sequence-Number: 41457 > So, I did. I run the whole script in psql, and here is the result for > the INSERT: > > realm_51=# explain analyze INSERT INTO drones_history (2771, drone_id, > drone_log_notice, drone_temperature, drone_pressure) SELECT * FROM > tmp_drones_history; > QUERY PLAN > ---------------------------------------------------------------------------------------------------------------------- > Seq Scan on tmp_drones_history (cost=0.00..81.60 rows=4160 width=48) > (actual time=0.008..5.296 rows=5150 loops=1) > Trigger for constraint drones_history_fk__drones: time=92.948 > calls=5150 > Total runtime: 16779.644 ms > (3 rows) > > > Now, this is only 16 seconds. In this 'batch' I've inserted 5150 rows. > The batch before, I run that one 'the usual way', it inserted 9922 rows, > and it took 1 minute and 16 seconds. > > I did not, however, enclose the process into begin/end. > > So, here are results when I, in psql, first issued BEGIN: > > realm_51=# explain analyze INSERT INTO drones_history (2772, drone_id, > drone_log_notice, drone_temperature, drone_pressure) SELECT * FROM > tmp_drones_history; > QUERY PLAN > ---------------------------------------------------------------------------------------------------------------------- > Seq Scan on tmp_drones_history (cost=0.00..79.56 rows=4056 width=48) > (actual time=0.008..6.490 rows=5059 loops=1) > Trigger for constraint drones_history_fk__drones: time=120.224 > calls=5059 > Total runtime: 39658.250 ms > (3 rows) > > Time: 39658.906 ms > > > > Mario > Note that in both cases postgres reports that the FK checks take 92-120 milliseconds... which is a normal time for about 4000 rows. Inserting 4000 lines with just a few fields like you got should take quite much less than 1 s... Where the rest of the time goes, I have no idea. Disk thrashing ? Locks ? Gremlins ? - try it on a fresh copy of all your tables (CREATE TABLE, INSERT INTO SELECT) - try to put the WAL on a separate physical disk (or do a check with fsync=off) - try it on another computer - try it on another harddisk - run oprofile on a debug compile of postgres - it could even be the process title updates (I don't think so but...) - try a ramdisk tablespace