pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rhaoni Chiu Pereira <rhaoni@sistemica.info>
To: josh@agliodbs.com
Cc: PostgreSQL ADMIN <pgsql-admin@postgresql.org>
Cc: PostgreSQL Performance <pgsql-performance@postgresql.org>
Subject: Re: [PERFORM] How to force an Index ?
Date: Thu, 18 Sep 2003 11:45:09 -0300
Message-ID: <1063896309.3f69c4f5cd16a@sistemica.info> (raw)
In-Reply-To: <200309171538.33515.josh@agliodbs.com>
References: <1063837067.3f68dd8b5a62b@sistemica.info>
	<200309171538.33515.josh@agliodbs.com>

I solve this problem doing this:

 create function date_to_yyyymm( timestamp ) returns gsames00.ano_mes%type as
 'select to_char($1, ''YYYYMM'');
 ' language sql immutable strict;

And changing the SQL where clause:

 ... gsames00.ano_mes = to_char(ftnfco00.data_emissao,'YYYYMM') AND ...

to:

 ... gsames00.ano_mes = date_to_yyyymm(ftnfco00.data_emissao) AND ...

   Then it uses the gsames00 index instead of a SeqScan 'cuz it is camparing
same data type, but .. I don't want to create this function 'cuz this aplication
is used with Oracle too. 
I need to know if there is a way to set the to_char output to varchar instead of
text !
  Any Idea ? So, this way I wont have to change my aplication source.



Atenciosamente,

Rhaoni Chiu Pereira
Sistêmica Computadores

Visite-nos na Web: http://sistemica.info
Fone/Fax : +55 51 3328 1122



Citando Josh Berkus <josh@agliodbs.com>:

<> Rhaoni,
<> 
<> First off, thanks for posting such complete info.
<> 
<> >    ... gsames00.ano_mes = to_char(ftnfco00.data_emissao,'YYYYMM') AND ...
<> > 
<> >    ftnfco00.data_emissao is a timestamp. When I run the explain analyze it
<> 
<> says:
<> > 
<> > ...
<> >  ->  Seq Scan on gsames00  (cost=100000000.00..100000006.72 rows=372 
<> width=10) 
<> > (actual time=0.01..0.96 rows=372 loops=19923)
<> > ...
<> 
<> Your problem is that you're comparing against a calculated expression based
<> on 
<> ftnfco00, which is being filtered in about 18 other ways.  As a result, the
<> 
<> planner doesn't know what to estimate (see the cost estimate of 100000000, 
<> which is a "blind guess" values) and goes for a seq scan.
<> 
<>  Can I ask you to try this workaround, to create an expressional index on 
<> ftnfco00 (assuming that data_emmisao is of type DATE)
<> 
<> create function date_to_yyyymm( date ) returns text as
<> 'select to_char($1, ''YYYYMM'');
<> ' language sql immutable strict;
<> 
<> create index idx_data_yyyymm on ftnfco00(date_to_yyyymm(data_emmisao));
<> 
<> -- 
<> -Josh Berkus
<>  Aglio Database Solutions
<>  San Francisco
<> 
<> 
<> ---------------------------(end of broadcast)---------------------------
<> TIP 4: Don't 'kill -9' the postmaster
<> 




view thread (6+ messages)  latest in thread

Message-ID: <1063896309.3f69c4f5cd16a@sistemica.info>
Permalink:  ../1063896309.3f69c4f5cd16a@sistemica.info/
Also on:    postgresql.org/message-id/1063896309.3f69c4f5cd16a@sistemica.info

 · 

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: rhaoni@sistemica.info, josh@agliodbs.com, pgsql-admin@postgresql.org
  Subject: Re: [PERFORM] How to force an Index ?
  In-Reply-To: <1063896309.3f69c4f5cd16a@sistemica.info>
  References: <1063837067.3f68dd8b5a62b@sistemica.info> <200309171538.33515.josh@agliodbs.com> <1063896309.3f69c4f5cd16a@sistemica.info>

* 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