Re: functional index not used, looping simpler query just faster

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
Cc: PGSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: functional index not used, looping simpler query just faster
Date: 2008-07-10 14:46:53
Message-ID: 1588.1215701213@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it> writes:
> Now I try this:

> explain select i1.brands, i1.name, i1.dataPub, i1.datainserimento
> from catalog_items i1
> inner join catalog_brands b1 on upper(i1.brands)=upper(b1.name)
> where i1.ItemID in (
> select i2.ItemID from catalog_items i2
> inner join catalog_brands b2 on upper(i2.brands)=upper(b2.name)
> where i1.brands=i2.brands
> and i2.dataPub>(now() - interval '8 month') and
> i2.datainserimento>(now() - interval '6 month') order by
> i2.datainserimento desc limit 3);

This sub-select is non optimizable because you've got an outer reference
in it, which compels re-evaluating it at every row of the outer query.
Try recasting as

explain select i1.brands, i1.name, i1.dataPub, i1.datainserimento
from catalog_items i1
inner join catalog_brands b1 on upper(i1.brands)=upper(b1.name)
where (i1.ItemID, i1.brands) in (
select i2.ItemID, i2.brands from catalog_items i2
inner join catalog_brands b2 on upper(i2.brands)=upper(b2.name)
where
i2.dataPub>(now() - interval '8 month') and
i2.datainserimento>(now() - interval '6 month') order by
i2.datainserimento desc limit 3);

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lennin Caro 2008-07-10 14:51:34 Re: SPACE FOR POSTGRESQL DATABASE
Previous Message aravind chandu 2008-07-10 14:18:39 SPACE FOR POSTGRESQL DATABASE