pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Atkins <steve@blighty.com>
To: pgsql-performance@postgresql.org
Subject: Re: optimizing large query with IN (...)
Date: Wed, 10 Mar 2004 06:42:54 -0800
Message-ID: <20040310144253.GA31063@gp.word-to-the-wise.com> (raw)
In-Reply-To: <62371.200.174.148.100.1078889715.squirrel@webmail.webnow.com.br>
References: <62371.200.174.148.100.1078889715.squirrel@webmail.webnow.com.br>

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





view thread (10+ messages)  latest in thread

Message-ID: <20040310144253.GA31063@gp.word-to-the-wise.com>
Permalink:  ../20040310144253.GA31063@gp.word-to-the-wise.com/
Also on:    postgresql.org/message-id/20040310144253.GA31063@gp.word-to-the-wise.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: steve@blighty.com
  Subject: Re: optimizing large query with IN (...)
  In-Reply-To: <20040310144253.GA31063@gp.word-to-the-wise.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox