Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id F19279F9868 for ; Tue, 21 Aug 2007 23:40:46 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 37787-01 for ; Tue, 21 Aug 2007 23:40:43 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mustang.sdc.com.au (mustang.sdc.com.au [203.2.199.1]) by postgresql.org (Postfix) with ESMTP id BE3E29F984F for ; Tue, 21 Aug 2007 23:40:42 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mustang.sdc.com.au (8.13.3/8.13.0) with ESMTP id l7M2ebQh008233 for ; Wed, 22 Aug 2007 12:10:37 +0930 From: Stephen Davies Organization: Stephen Davies Consulting P/L To: pgsql-performance@postgresql.org Subject: Optimising "in" queries User-Agent: KMail/1.9.5 MIME-Version: 1.0 Content-Disposition: inline Date: Wed, 22 Aug 2007 12:10:36 +0930 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200708221210.36770.scldad@sdc.com.au> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200708/227 X-Sequence-Number: 26357 I have a PostgreSQL 8.2.4 table with some seven million rows. The psql query: select count(rdate),rdate from reading where sensor_id in (1137,1138,1139) group by rdate order by rdate desc limit 1; takes a few seconds but: select count(rdate),rdate from reading where sensor_id in (1137,1138,1139,1140) group by rdate order by rdate desc limit 1; (anything with four or more values in the "in" list) takes several minutes. Is there any way to make the "larger" queries more efficient? Both rdate and sensor_id are indexed and the database is vacuumed every night. The values in the "in" list are seldom as "neat" as in the above examples. Actual values can range from 1 to about 2000. The number of values ranges from 2 to about 10. Explain outputs are: benparts=# explain select count(rdate),rdate from reading where sensor_id in (1137,1138,1139,1140) group by rdate order by rdate desc limit 1; QUERY PLAN --------------------------------------------------------------------------------------------------- Limit (cost=0.00..39890.96 rows=1 width=8) -> GroupAggregate (cost=0.00..7938300.21 rows=199 width=8) -> Index Scan Backward using date on reading (cost=0.00..7937884.59 rows=82625 width=8) Filter: (sensor_id = ANY ('{1137,1138,1139,1140}'::integer[])) (4 rows) benparts=# explain select count(rdate),rdate from reading where sensor_id in (1137,1138,1139) group by rdate order by rdate desc limit 1; QUERY PLAN ----------------------------------------------------------------------------------------------------- Limit (cost=48364.32..48364.32 rows=1 width=8) -> Sort (cost=48364.32..48364.49 rows=69 width=8) Sort Key: rdate -> HashAggregate (cost=48361.35..48362.21 rows=69 width=8) -> Bitmap Heap Scan on reading (cost=535.53..48218.10 rows=28650 width=8) Recheck Cond: (sensor_id = ANY ('{1137,1138,1139}'::integer[])) -> Bitmap Index Scan on reading_sensor (cost=0.00..528.37 rows=28650 width=0) Index Cond: (sensor_id = ANY ('{1137,1138,1139}'::integer[])) (8 rows) TIA, Stephen Davies -- ======================================================================== This email is for the person(s) identified above, and is confidential to the sender and the person(s). No one else is authorised to use or disseminate this email or its contents. Stephen Davies Consulting Voice: 08-8177 1595 Adelaide, South Australia. Fax: 08-8177 0133 Computing & Network solutions. Mobile:0403 0405 83