Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C8091B5DBCA for ; Wed, 2 Nov 2011 17:14:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 25440-02 for ; Wed, 2 Nov 2011 20:14:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-qw0-f46.google.com (mail-qw0-f46.google.com [209.85.216.46]) by mail.postgresql.org (Postfix) with ESMTP id 241EFB5DBC8 for ; Wed, 2 Nov 2011 17:14:01 -0300 (ADT) Received: by qabj40 with SMTP id j40so523060qab.19 for ; Wed, 02 Nov 2011 13:14:00 -0700 (PDT) Received: by 10.68.28.3 with SMTP id x3mr6653090pbg.132.1320264839997; Wed, 02 Nov 2011 13:13:59 -0700 (PDT) Received: from sidon.consistentstate.com (173-164-51-9-colorado.hfc.comcastbusiness.net. [173.164.51.9]) by mx.google.com with ESMTPS id lk8sm7309351pbb.4.2011.11.02.13.13.58 (version=SSLv3 cipher=OTHER); Wed, 02 Nov 2011 13:13:59 -0700 (PDT) Message-ID: <4EB1A419.1080901@consistentstate.com> Date: Wed, 02 Nov 2011 14:12:09 -0600 From: Brian Fehrle User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: two table join just not fast enough. Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201111/26 X-Sequence-Number: 45537 Hi all, I've got a query that I need to squeeze as much speed out of as I can. When I execute this query, the average time it takes is about 190 ms. I increased my work_mem from 1 MB to 50MB and it decreased the timing down to an average of 170 ms, but that's still not fast enough. This query is executed extremely frequently, so much of it should be easily cached. Some settings work_mem = 50MB shared_buffers = 5GB I've made sure that the two tables are recently analyzed (with default_stats to 100, 400, and 1500 even), and table bloat is low (150 meg table has 7 megs bloat). Here's the query: SELECT yankee.my_id FROM yankee INNER JOIN hotel_zulu ON hotel_zulu.my_id = yankee.zulu_id AND hotel_zulu.type IN ('string1', 'string2', 'string3', 'string4') WHERE yankee.your_id=402513; And here is a query plan. Hash Join (cost=17516.470..26386.660 rows=27624 width=4) (actual time=309.194..395.135 rows=12384 loops=1) Hash Cond: (yankee.alpha = hotel_zulu.quebec) -> Bitmap Heap Scan on yankee (cost=1066.470..8605.770 rows=27624 width=20) (actual time=5.178..34.693 rows=26963 loops=1) Recheck Cond: (mike = 402513) -> Bitmap Index Scan on hotel_alpha (cost=0.000..1059.570 rows=27624 width=0) (actual time=4.770..4.770 rows=26967 loops=1) Index Cond: (mike = 402513) -> Hash (cost=14465.080..14465.080 rows=114154 width=16) (actual time=303.717..303.717 rows=129908 loops=1) Buckets: 4096 Batches: 8 Memory Usage: 784kB -> Bitmap Heap Scan on hotel_zulu (cost=2461.770..14465.080 rows=114154 width=16) (actual time=25.642..185.253 rows=129908 loops=1) Recheck Cond: ((two)::golf = ANY ('xray'::golf[])) -> Bitmap Index Scan on kilo (cost=0.000..2433.230 rows=114154 width=0) (actual time=23.887..23.887 rows=130292 loops=1) Index Cond: ((two)::golf = ANY ('xray'::golf[])) One thing I notice is the rows estimated is 27624 and the actual rows returned is 12384. Quite a bit different. Table 2 (known here as hotel_zulu) is being joined on zulu_id to the first table, and then a where clause on the column 'type'. There are single column indexes on each of these columns, and any multi column index I put on these are just ignored by the planner. Any thoughts on ways to tweak this? - Brian F