Re: slow 'IN' clause

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: FavoYang(at)gmail(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: slow 'IN' clause
Date: 2006-04-11 20:21:06
Message-ID: 20060411202106.GA17919@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sun, Apr 09, 2006 at 20:44:34 -0700,
FavoYang(at)gmail(dot)com wrote:
> I have a slow sql:
> SELECT * FROM mytable WHERE id IN (1,3,5,7,....3k here...);
> mytable is about 10k rows.
>
> if don't use the "IN" clause, it will cost 0,11 second, otherwise it
> will cost 2.x second
> I guess pg use linear search to deal with IN clause, is there any way
> to let pg use other search method with IN clause? (ex.Binary Search or
> hash Search)

It would help if you told us what version of Postgres you are running
and showed EXPLAIN ANALYZE results for the two queries.

I have heard of people getting better speed by loading the constant list
into a temp table and then doing a join.

Some older versions of Postgres also had problems with large constant lists,
so if you are using an old version of Postgres upgrading might help.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message gurkan 2006-04-12 18:45:06 SQL help (Informix outer to EnterpriseDB outer)
Previous Message mike 2006-04-11 18:00:32 Re: how to use recursion to find end nodes of a tree