X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AE4DCD6EB0 for ; Sat, 24 Sep 2005 17:51:29 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65170-08 for ; Sat, 24 Sep 2005 20:51:26 +0000 (GMT) Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 9FE70D6DA1 for ; Sat, 24 Sep 2005 17:51:26 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 02E2131F51; Sat, 24 Sep 2005 22:51:26 +0200 (MET DST) From: Ron Mayer X-Newsgroups: pgsql.performance Subject: Multiple insert performance trick or performance misunderstanding? Date: Sat, 24 Sep 2005 13:51:16 -0700 Organization: Hub.Org Networking Services Lines: 32 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en To: pgsql-performance@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=-0.012 required=5 tests=[AWL=-0.012] X-Spam-Level: X-Archive-Number: 200509/327 X-Sequence-Number: 14635 When I need to insert a few hundred or thousand things in a table from a 3-tier application, it seems I'm much better off creating a big string of semicolon separated insert statements rather than sending them one at a time - even when I use the obvious things like wrapping the statements in a transaction and using the library's prepared statements. I tried both Ruby/DBI and C#/Npgsql; and in both cases sets of inserts that took 3 seconds when run individually took about 0.7 seconds when concatenated together. Is it expected that I'd be better off sending big concatenated strings like "insert into tbl (c1,c2) values (v1,v2);insert into tbl (c1,c2) values (v3,v4);..." instead of sending them one at a time? db.ExecuteSQL("BEGIN"); sql = new System.Text.StringBulder(10000); for ([a lot of data elements]) { sql.Append( "insert into user_point_features (col1,col2)"+ " values (" +obj.val1 +","+obj.val2+");" ); } db.ExecuteSQL(sql.ToString()); db.ExecuteSQL("COMMIT");