X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 75735D1B837 for ; Wed, 10 Mar 2004 14:50:13 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 01758-01 for ; Wed, 10 Mar 2004 10:50:07 -0400 (AST) Received: from gp.word-to-the-wise.com (gp.word-to-the-wise.com [64.71.176.18]) by svr1.postgresql.org (Postfix) with ESMTP id 5D9E2D1B80C for ; Wed, 10 Mar 2004 10:50:06 -0400 (AST) Received: by gp.word-to-the-wise.com (Postfix, from userid 500) id AF1F59029F3; Wed, 10 Mar 2004 06:42:54 -0800 (PST) Date: Wed, 10 Mar 2004 06:42:54 -0800 From: Steve Atkins To: pgsql-performance@postgresql.org Subject: Re: optimizing large query with IN (...) Message-ID: <20040310144253.GA31063@gp.word-to-the-wise.com> References: <62371.200.174.148.100.1078889715.squirrel@webmail.webnow.com.br> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <62371.200.174.148.100.1078889715.squirrel@webmail.webnow.com.br> User-Agent: Mutt/1.4.1i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/129 X-Sequence-Number: 5985 On Wed, Mar 10, 2004 at 12:35:15AM -0300, Marcus Andree S. Magalhaes wrote: > Guys, > > I got a Java program to tune. It connects to a 7.4.1 postgresql server > running Linux using JDBC. > > The program needs to update a counter on a somewhat large number of > rows, about 1200 on a ~130k rows table. The query is something like > the following: > > UPDATE table SET table.par = table.par + 1 > WHERE table.key IN ('value1', 'value2', ... , 'value1200' ) > > This query runs on a transaction (by issuing a call to > setAutoCommit(false)) and a commit() right after the query > is sent to the backend. > > The process of committing and updating the values is painfully slow > (no surprises here). Any ideas? I posted an analysis of use of IN () like this a few weeks ago on pgsql-general. The approach you're using is optimal for < 3 values. For any more than that, insert value1 ... value1200 into a temporary table, then do UPDATE table SET table.par = table.par + 1 WHERE table.key IN (SELECT value from temp_table); Indexing the temporary table marginally increases the speed, but not significantly. Cheers, Steve