Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 557859FB8AC for ; Tue, 17 Jul 2007 17:50:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 31008-01 for ; Tue, 17 Jul 2007 17:50:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from pat.uio.no (pat.uio.no [129.240.10.15]) by postgresql.org (Postfix) with ESMTP id 58F6B9FB744 for ; Tue, 17 Jul 2007 17:50:24 -0300 (ADT) Received: from mail-mx6.uio.no ([129.240.10.47]) by pat.uio.no with esmtp (Exim 4.67) (envelope-from ) id 1IAtzz-0007Pa-3H for pgsql-performance@postgresql.org; Tue, 17 Jul 2007 22:50:23 +0200 Received: from smtp.uio.no ([129.240.10.9] helo=mail-mx6.uio.no) by mail-mx6.uio.no with esmtp (Exim 4.67) (envelope-from ) id 1IAtzy-0002Vy-O9 for pgsql-performance@postgresql.org; Tue, 17 Jul 2007 22:50:22 +0200 Received: from 113.80-203-240.nextgentel.com ([80.203.240.113] helo=[10.0.0.128]) by mail-mx6.uio.no with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.67) (envelope-from ) id 1IAtzy-0002VU-Je for pgsql-performance@postgresql.org; Tue, 17 Jul 2007 22:50:22 +0200 Message-ID: <469D2B8E.7050208@ifi.uio.no> Date: Tue, 17 Jul 2007 22:50:22 +0200 From: Thomas Finneid User-Agent: Thunderbird 1.5.0.12 (X11/20070604) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: insert vs select into performance References: <469D1AD3.9030004@ifi.uio.no> <0D6011B2-D09F-45E8-A5D7-5F84ADD1DC2B@seespotcode.net> In-Reply-To: <0D6011B2-D09F-45E8-A5D7-5F84ADD1DC2B@seespotcode.net> Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-UiO-SPF-Received: X-UiO-Resend: resent X-UiO-SPF-Received: X-UiO-Spam-info: not spam, SpamAssassin (score=0.0, required=12.0, autolearn=disabled, none) X-UiO-Scanned: 215E793FAB9608A0C337B666C286F8086B33AFD3 X-UiO-SPAM-Test: remote_host: 129.240.10.9 spam_score: 0 maxlevel 200 minaction 2 bait 0 mail/h: 396 total 2876930 max/h 8345 blacklist 0 greylist 0 ratelimit 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/194 X-Sequence-Number: 25878 Michael Glaesemann wrote: > > On Jul 17, 2007, at 14:38 , Thomas Finneid wrote: > >> I was doing some testing on "insert" compared to "select into". I >> inserted 100 000 rows (with 8 column values) into a table, which took >> 14 seconds, compared to a select into, which took 0.8 seconds. >> (fyi, the inserts where batched, autocommit was turned off and it all >> happend on the local machine) >> >> Now I am wondering why the select into is that much faster? > > It would be helpful if you included the actual queries you're using, as > there are a number of variables: create table ciu_data_type ( id integer, loc_id integer, value1 integer, value2 real, value3 integer, value4 real, value5 real, value6 char(2), value7 char(3), value8 bigint, value9 bigint, value10 real, value11 bigint, value12 smallint, value13 double precision, value14 real, value15 real, value16 char(1), value17 varchar(18), value18 bigint, value19 char(4) ); performed with JDBC insert into ciu_data_type (id, loc_id, value3, value5, value8, value9, value10, value11 ) values (?,?,?,?,?,?,?,?) select * into ciu_data_type_copy from ciu_data_type > 1) If there are any constraints on the original table, the INSERT will > be checking those constraints. AIUI, SELECT INTO does not generate any > table constraints. No constraints in this test. > 2a) Are you using INSERT INTO foo (foo1, foo2, foo2) SELECT foo1, foo2, > foo3 FROM pre_foo or individual inserts for each row? The former would > be faster than the latter. > > 2b) If you are doing individual inserts, are you wrapping them in a > transaction? The latter would be faster. disabling autocommit, but nothing more than that I havent done this test in a stored function yet, nor have I tried it with a C client so far, so there is the chance that it is java/jdbc that makes the insert so slow. I'll get to that test soon if there is any chance my theory makes sence. regards thomas