Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 98E099FBA40 for ; Tue, 17 Jul 2007 18:13:00 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 71809-05 for ; Tue, 17 Jul 2007 18:12:57 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from h07.llord.com (unknown [195.140.142.194]) by postgresql.org (Postfix) with ESMTP id 644C19FB9BB for ; Tue, 17 Jul 2007 18:12:57 -0300 (ADT) Received: from par69-8-88-161-102-87.fbx.proxad.net ([88.161.102.87] helo=apollo13) by h07.llord.com with esmtpa (Exim 4.66) (envelope-from ) id 1IAuLj-0004Bq-34; Tue, 17 Jul 2007 23:12:51 +0200 Date: Tue, 17 Jul 2007 23:14:36 +0200 To: "Thomas Finneid" , pgsql-performance@postgresql.org Subject: Re: insert vs select into performance From: PFC Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <469D1AD3.9030004@ifi.uio.no> Content-Transfer-Encoding: Quoted-Printable Message-ID: In-Reply-To: <469D1AD3.9030004@ifi.uio.no> User-Agent: Opera Mail/9.21 (Linux) X-PopBeforeSMTPSenders: junk@peufeu.com, lists@peufeu.com, peufeu@peufeu.com, pfcaillaud@peufeu.com X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - h07.llord.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - peufeu.com X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.189 tagged_above=0 required=5 tests=AWL=0.016, BAYES_50=0.001, RCVD_IN_NJABL_SPAM=2.072, RDNS_NONE=0.1 X-Spam-Level: ** X-Archive-Number: 200707/201 X-Sequence-Number: 25885 > 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) Did you use prepared statements ? Did you use INSERT INTO ... VALUES () with a long list of values, or ju= st = 100K insert statements ? It's the time to parse statements, plan, execute, roundtrips with the = client, context switches, time for your client library to escape the dat= a = and encode it and for postgres to decode it, etc. In a word : OVERHEAD. By the way which language and client library are you using ? FYI 14s / 100k =3D 140 microseconds per individual SQL query. That ain'= t = slow at all. > Does the select into translate into a specially optimised function in = c = > that can cut corners which a insert can not do (e.g. lazy copying), or= = > is it some other reason? Yeah : instead of your client having to encode 100K * 8 values, send it= = over a socket, and postgres decoding it, INSERT INTO SELECT just takes t= he = data, and writes the data. Same thing as writing a file a byte at a time= = versus using a big buffer. > The reason I am asking is that select into shows that a number of rows= = > can be inserted into a table quite a lot faster than one would think w= as = > possible with ordinary sql. If that is the case, it means that if I = > write an pl-pgsql insert function in C instead of sql, then I can have= = > my db perform order of magnitude faster. Fortunately this is already done for you : there is the PREPARE = statement, which will remove the parsing overhead. If you must insert ma= ny = rows, use VALUES (),(),()...