pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@gmail.com>
To: Joost Kraaijeveld <J.Kraaijeveld@askesis.nl>
Cc: Pgsql-Performance <pgsql-performance@postgresql.org>
Subject: Re: How to query and index for customer with lastname and city
Date: Sat, 4 Mar 2006 14:49:44 +0100
Message-ID: <9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com> (raw)
In-Reply-To: <1141466283.6729.17.camel@localhost>
References: <1141466283.6729.17.camel@localhost>

On 3/4/06, Joost Kraaijeveld <J.Kraaijeveld@askesis.nl> wrote:
>  Below is what I actually have. Given the fact that it takes forever to get
> a result (> 6 seconds) , there must be something wrong with my solution or
> my expectation. Can anyone tell what I should do to make this query go
> faster ( or convince me to wait for the result ;-()?
>  Explain analyze after a full alayse vacuum:
>  Sort  (cost=54710.68..54954.39 rows=97484 width=111) (actual
> time=7398.971..7680.405 rows=96041 loops=1)
>    Sort Key: btrim(upper(customers.lastname)), btrim(upper(addresses.city))
>    ->  Hash Join  (cost=14341.12..46632.73 rows=97484 width=111) (actual
> time=1068.862..5472.788 rows=96041 loops=1)
>          Hash Cond: ("outer".contactaddress = "inner".objectid)
>          ->  Seq Scan on customers  (cost=0.00..24094.01 rows=227197
> width=116) (actual time=0.018..1902.646 rows=223990 loops=1)
>                Filter: (btrim(upper(lastname)) >= 'JANSEN'::text)
>          ->  Hash  (cost=13944.94..13944.94 rows=158473 width=75) (actual
> time=1068.467..1068.467 rows=158003 loops=1)
>                ->  Bitmap Heap Scan on addresses  (cost=1189.66..13944.94
> rows=158473 width=75) (actual time=71.259..530.986 rows=158003 loops=1)
>                      Recheck Cond: (btrim(upper(city)) >= 'NIJMEGEN'::text)
>                      ->  Bitmap Index Scan on
> prototype_addresses_trim_upper_city  (cost=0.00..1189.66
> rows=158473 width=0) (actual time=68.290..68.290 rows=158003 loops=1)
>                            Index Cond: (btrim(upper(city)) >=
> 'NIJMEGEN'::text)
>  Total runtime: 7941.095 ms

explain clearly shows, that index is used for addresses scan, but it
is not so for users.
explain estimates that 227197 customers match the lastname criteria -
which looks awfuly high.
how many record do you have in the customers table?

i would try to create index test on customers(contactAddress,
trim(uppercase(lastname)));
or with other ordring of fields.

try this - create the index, make analyze of customers table, and
recheck explain.
then try the second index in the same manner.

maybe this could of some help...

depesz



view thread (5+ messages)  latest in thread

Message-ID: <9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com>
Permalink:  ../9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com/
Also on:    postgresql.org/message-id/9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com

 · 

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: depesz@gmail.com, J.Kraaijeveld@askesis.nl
  Subject: Re: How to query and index for customer with lastname and city
  In-Reply-To: <9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com>
  References: <1141466283.6729.17.camel@localhost> <9e4684ce0603040549n44e71c29jab72a45f5c440f9@mail.gmail.com>

* 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