Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 321D09FBE3E for ; Wed, 14 Feb 2007 18:24:28 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 97393-05-2 for ; Wed, 14 Feb 2007 18:24:24 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from gw.talleyds.com (mesagate.talleyds.com [207.114.138.139]) by postgresql.org (Postfix) with ESMTP id 75BBE9FBE0E for ; Wed, 14 Feb 2007 18:24:24 -0400 (AST) Received: from no.name.available by gw.talleyds.com via smtpd (for postgresql.org [200.46.204.71]) with ESMTP; Wed, 14 Feb 2007 15:24:33 -0700 Received: from GATEDOM-MTA by mesagate.talleyds.com with Novell_GroupWise; Wed, 14 Feb 2007 15:24:22 -0700 Message-Id: <45D32990.A961.00E7.1@talleyds.com> X-Mailer: Novell GroupWise Internet Agent 7.0.1 Date: Wed, 14 Feb 2007 15:24:00 -0700 From: "Lou O'Quin" To: , "Karen Hill" Subject: Re: How long should it take to insert 200,000 records? References: <1170722158.973141.208230@j27g2000cwj.googlegroups.com> <29906.1170740015@sss.pgh.pa.us> <1170797956.583767.246670@p10g2000cwp.googlegroups.com> In-Reply-To: <1170797956.583767.246670@p10g2000cwp.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50 X-Spam-Level: X-Archive-Number: 200702/161 X-Sequence-Number: 23215 unless you specify otherwiise, every insert carries its own transaction begin/commit. That's a lot of overhead for a single insert, no? Why not use a single transaction for, say, each 1000 inserts? That would strike a nice balance of security with efficiency. pseudo code for the insert: Begin Transaction; FOR i in 1..200000 LOOP INSERT INTO viewfoo (x) VALUES (x); IF i % 1000 = 0 THEN Commit Transaction; Begin Transaction; END IF; END LOOP; Commit Transaction; End This approach should speed up things dramatically. >>> "Karen Hill" 2/6/2007 2:39 PM >>> On Feb 5, 9:33 pm, t...@sss.pgh.pa.us (Tom Lane) wrote: > "Karen Hill" writes: > > I have a pl/pgsql function that is inserting 200,000 records for > > testing purposes. What is the expected time frame for this operation > > on a pc with 1/2 a gig of ram and a 7200 RPM disk? > > I think you have omitted a bunch of relevant facts. Bare INSERT is > reasonably quick: > > regression=# create table foo (f1 int); > CREATE TABLE > regression=# \timing > Timing is on. > regression=# insert into foo select x from generate_series(1,200000) x; > INSERT 0 200000 > Time: 5158.564 ms > regression=# > > (this on a not-very-fast machine) but if you weigh it down with a ton > of index updates, foreign key checks, etc, it could get slow ... > also you haven't mentioned what else that plpgsql function is doing. > The postgres version is 8.2.1 on Windows. The pl/pgsql function is inserting to an updatable view (basically two tables). CREATE TABLE foo1 ( ) ; CREATE TABLE foo2 ( ); CREATE VIEW viewfoo AS ( ); CREATE RULE ruleFoo AS ON INSERT TO viewfoo DO INSTEAD ( ); CREATE OR REPLACE FUNCTION functionFoo() RETURNS VOID AS $$ BEGIN FOR i in 1..200000 LOOP INSERT INTO viewfoo (x) VALUES (x); END LOOP; END; $$ LANGUAGE plpgsql; ---------------------------(end of broadcast)--------------------------- TIP 6: explain analyze is your friend