pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Roman Fail <rfail@posportal.com>
Cc: Josh Berkus <josh@agliodbs.com>
Cc: sszabo@megazone23.bigpanda.com
Cc: pgsql-performance@postgresql.org
Subject: Re: 7.3.1 New install, large queries are slow
Date: Thu, 16 Jan 2003 14:46:33 -0500
Message-ID: <26712.1042746393@sss.pgh.pa.us> (raw)
In-Reply-To: <9B1C77393DED0D4B9DAA1AA1742942DA0E4C06@pos_pdc.posportal.com>
References: <9B1C77393DED0D4B9DAA1AA1742942DA0E4C06@pos_pdc.posportal.com>

"Roman Fail" <rfail@posportal.com> writes:
>                                             ->  Merge Join  (cost=1543595.18..1545448.76 rows=1 width=172) (actual time=1195311.88..1195477.32 rows=5 loops=1)
>                                                   Merge Cond: ("outer".batchid = "inner".batchid)
>                                                   ->  Sort  (cost=476.17..476.18 rows=4 width=102) (actual time=30.57..30.59 rows=17 loops=1)
>                                                         Sort Key: b.batchid
>                                                         ->  Nested Loop  (cost=0.00..476.14 rows=4 width=102) (actual time=25.21..30.47 rows=17 loops=1)
>                                                               ->  Index Scan using merchants_ix_merchid_idx on merchants m  (cost=0.00..5.65 rows=1 width=78) (actual time=23.81..23.82 rows=1 loops=1)
>                                                                     Index Cond: (merchid = '701252267'::character varying)
>                                                               ->  Index Scan using batchheader_ix_merchantid_idx on batchheader b  (cost=0.00..470.30 rows=15 width=24) (actual time=1.38..6.55 rows=17 loops=1)
>                                                                     Index Cond: ("outer".merchantid = b.merchantid)
>                                                                     Filter: (batchdate > '2002-12-15 00:00:00'::timestamp without time zone)
>                                                   ->  Sort  (cost=1543119.01..1544045.79 rows=370710 width=70) (actual time=1194260.51..1194892.79 rows=368681 loops=1)
>                                                         Sort Key: d.batchid
>                                                         ->  Index Scan using batchdetail_ix_tranamount_idx on batchdetail d  (cost=0.00..1489103.46 rows=370710 width=70) (actual time=5.26..1186051.44 rows=370307 loops=1)
>                                                               Index Cond: ((tranamount >= 500.0) AND (tranamount <= 700.0))

The expensive part of this is clearly the sort and merge of the rows
extracted from batchdetail.  The index on tranamount is not helping
you at all, because the condition (between 500 and 700) isn't very
selective --- it picks up 370000 rows --- and since those rows are
totally randomly scattered in the table, you do a ton of random
seeking.  It's actually faster to scan the table linearly --- that's why
enable_indexscan=off was faster.

However, I'm wondering why the thing picked this plan, when it knew it
would get only a few rows out of the m/b join (estimate 4, actual 17,
not too bad).  I would have expected it to use an inner indexscan on
d.batchid.  Either you've not got an index on d.batchid, or there's a
datatype mismatch that prevents the index from being used.  What are the
datatypes of d.batchid and b.batchid, exactly?  If they're not the same,
either make them the same or add an explicit coercion to the query, like
	WHERE d.batchid = b.batchid::typeof_d_batchid

			regards, tom lane



view thread (66+ messages)  latest in thread

Message-ID: <26712.1042746393@sss.pgh.pa.us>
Permalink:  ../26712.1042746393@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/26712.1042746393@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rfail@posportal.com, josh@agliodbs.com, sszabo@megazone23.bigpanda.com
  Subject: Re: 7.3.1 New install, large queries are slow
  In-Reply-To: <26712.1042746393@sss.pgh.pa.us>
  References: <9B1C77393DED0D4B9DAA1AA1742942DA0E4C06@pos_pdc.posportal.com> <26712.1042746393@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox