please help on query

From: "Luis Alberto Amigo Navarro" <lamigo(at)atc(dot)unican(dot)es>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: please help on query
Date: 2002-07-11 15:22:14
Message-ID: 005e01c228ee$bd1913e0$cab990c1@atc.unican.es
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

I can't improve performance on this query:

SELECT
supplier.name,
supplier.address
FROM
supplier,
nation
WHERE
supplier.suppkey IN(
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
)
)
AND supplier.nationkey=nation.nationkey
AND nation.name='CANADA'
ORDER BY
supplier.name;

explain results:
NOTICE: QUERY PLAN:

Sort (cost=2777810917708.17..2777810917708.17 rows=200 width=81)
-> Nested Loop (cost=0.00..2777810917700.53 rows=200 width=81)
-> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4)
-> Index Scan using snation_index on supplier (cost=0.00..2777810917696.72 rows=200 width=77)
SubPlan
-> Materialize (cost=6944527291.72..6944527291.72 rows=133333 width=4)
-> Seq Scan on partsupp (cost=0.00..6944527291.72 rows=133333 width=4)
SubPlan
-> Materialize (cost=8561.00..8561.00 rows=1 width=4)
-> Seq Scan on part (cost=0.00..8561.00 rows=1 width=4)
-> Aggregate (cost=119.61..119.61 rows=1 width=4)
-> Index Scan using lineitem_index on lineitem (cost=0.00..119.61 rows=1 width=4)

partsupp::800000 tuples
Table "partsupp"
Column | Type | Modifiers
------------+----------------+-----------
partkey | integer | not null
suppkey | integer | not null
availqty | integer |
supplycost | numeric(10,2) |
comment | character(199) |
Primary key: partsupp_pkey
Triggers: RI_ConstraintTrigger_16597,
RI_ConstraintTrigger_16603

tpch=# select attname,n_distinct,correlation from pg_stats where tablename='partsupp';
attname | n_distinct | correlation
------------+------------+-------------
partkey | -0.195588 | 1
suppkey | 9910 | 0.00868363
availqty | 9435 | -0.00788662
supplycost | -0.127722 | -0.0116864
comment | -1 | 0.0170702

I accept query changes, reordering, indexes ideas and horizontal partitioning
thanks in advance.
Regards

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kaare Rasmussen 2002-07-11 15:51:00 Re: I am being interviewed by OReilly
Previous Message Jan Wieck 2002-07-11 15:17:44 Re: I am being interviewed by OReilly

Browse pgsql-sql by date

  From Date Subject
Next Message Jie Liang 2002-07-11 15:41:00 Re: pg_restore cannot restore index
Previous Message Rudi Starcevic 2002-07-11 15:18:24 Re: [SQL] XML to Postgres conversion