pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hannu Krosing <hannu@tm.ee>
To: Luis Alberto Amigo Navarro <lamigo@atc.unican.es>
Cc: pgsql-hackers@postgresql.org
Subject: Re: please help on query
Date: 11 Jul 2002 22:48:08 +0200
Message-ID: <1026420488.18194.18.camel@taru.tm.ee> (raw)
In-Reply-To: <005e01c228ee$bd1913e0$cab990c1@atc.unican.es>
References: <005e01c228ee$bd1913e0$cab990c1@atc.unican.es>

On Thu, 2002-07-11 at 17:22, Luis Alberto Amigo Navarro wrote:
> I can't improve performance on this query:

You could try rewriting the IN's into = joins
or even use explicit INNER JOIN syntax to force certain plans

with a select inside another and depending on value of partsupp.partkey
it is really hard for optimiser to do anything else than to perform the
query for each row.

But it may help to rewrite

  SELECT
   partsupp.suppkey
  FROM
   partsupp
  WHERE
   partsupp.partkey IN (
    SELECT
     part.partkey
    FROM
     part
    WHERE
     part.name like 'forest%'
     )
   AND partsupp.availqty>(
    SELECT
     0.5*(sum(lineitem.quantity)::FLOAT)
    FROM
     lineitem
    WHERE
     lineitem.partkey=partsupp.partkey
     AND lineitem.suppkey=partsupp.partkey
     AND lineitem.shipdate>=('1994-01-01')::DATE
     AND lineitem.shipdate<(('1994-01-01')::DATE+('1
year')::INTERVAL)::DATE
     )
  )

into

  SELECT
    partsupp.suppkey
  FROM
   partsupp,
   (SELECT part.partkey as partkey
      FROM part
     WHERE part.name like 'forest%'
    ) fp,
   (SELECT 0.5*(sum(lineitem.quantity)::FLOAT) as halfsum,
           partkey
      FROM lineitem
    WHERE
     lineitem.partkey=partsupp.partkey
     AND lineitem.suppkey=partsupp.partkey
     AND lineitem.shipdate>=('1994-01-01')::DATE
     AND lineitem.shipdate<(('1994-01-01')::DATE+('1
year')::INTERVAL)::DATE
    ) li
  WHERE partsupp.partkey = fp.partkey 
   AND partsupp.partkey  = li.partkey 
   AND partsupp.availqty > halfsum

if "lineitem" is significantly smaller than "partsupp"



But you really should tell us more, like how many lines does lineitem
and other tables have,  

----------
Hannu




view thread (18+ messages)  latest in thread

Message-ID: <1026420488.18194.18.camel@taru.tm.ee>
Permalink:  ../1026420488.18194.18.camel@taru.tm.ee/
Also on:    postgresql.org/message-id/1026420488.18194.18.camel@taru.tm.ee

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: hannu@tm.ee, lamigo@atc.unican.es, pgsql-hackers@postgresql.org
  Subject: Re: please help on query
  In-Reply-To: <1026420488.18194.18.camel@taru.tm.ee>

* 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