Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id A244F1700456 for ; Tue, 7 Aug 2012 00:13:46 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SyaEp-0001uu-QK for pgsql-performance@postgresql.org; Tue, 07 Aug 2012 03:13:45 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q773DRdo005169; Mon, 6 Aug 2012 23:13:27 -0400 (EDT) From: Tom Lane To: Ioannis Anagnostopoulos cc: pgsql-performance@postgresql.org Subject: Re: Sequential scan instead of index scan In-reply-to: <50203F5A.3040805@anatec.com> References: <501FDDD5.2000106@anatec.com> <501FDFCD.2030806@oneit.com.au> <501FE1C9.6010209@anatec.com> <10966.1344267248@sss.pgh.pa.us> <50203F5A.3040805@anatec.com> Comments: In-reply-to Ioannis Anagnostopoulos message dated "Mon, 06 Aug 2012 23:04:10 +0100" Date: Mon, 06 Aug 2012 23:13:27 -0400 Message-ID: <5168.1344309207@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201208/31 X-Sequence-Number: 47569 Ioannis Anagnostopoulos writes: > On 06/08/2012 16:34, Tom Lane wrote: >> What you should probably be looking for is a hash join plan. > ... > Which is a Merge join and not a hash. Any ideas how to make it a hash join? You might need to ANALYZE the temp table, if you didn't already. Also it might be that you need to increase work_mem enough to fit the temp table into memory. Another thing that's bothering me is that the rowcount estimates are so far off, particularly this one: > " -> Index Scan using > idx_ship_a_pos_messages_wk0_date_trunc on ship_a_pos_messages_wk0 > ship_pos_messages (cost=0.00..1109877.86 rows=99313 width=128) (actual > time=0.029..435784.376 rows=18499560 loops=1)" > " Index Cond: (date_trunc('day'::text, msg_date_rec) > = '2012-08-01 00:00:00'::timestamp without time zone)" > " Filter: (date_part('day'::text, msg_date_rec) = > 1::double precision)" Offhand I'd have thought that ANALYZE would gather stats on the date_trunc expression (because it is indexed) and then you should get something reasonably accurate for a comparison to a constant. "Reasonably accurate" meaning "not off by two orders of magnitude". Practically all of your runtime is going into this one indexscan, and TBH it seems likely you'd be better off with a seqscan there. regards, tom lane