X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id DFB2111F8CA5 for ; Tue, 11 Apr 2006 17:52:00 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.12 X-Spam-Level: X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id WU6Fx7-49nJP for ; Tue, 11 Apr 2006 17:51:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id C03A311F8CC3 for ; Tue, 11 Apr 2006 17:19:01 -0300 (ADT) Received: (qmail 21672 invoked by uid 500); 11 Apr 2006 20:21:06 -0000 Date: Tue, 11 Apr 2006 15:21:06 -0500 From: Bruno Wolff III To: FavoYang@gmail.com Cc: pgsql-sql@postgresql.org Subject: Re: slow 'IN' clause Message-ID: <20060411202106.GA17919@wolff.to> Mail-Followup-To: Bruno Wolff III , FavoYang@gmail.com, pgsql-sql@postgresql.org References: <1144640674.181974.108190@u72g2000cwu.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1144640674.181974.108190@u72g2000cwu.googlegroups.com> User-Agent: Mutt/1.4.2.1i X-Archive-Number: 200604/82 X-Sequence-Number: 24891 On Sun, Apr 09, 2006 at 20:44:34 -0700, FavoYang@gmail.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.