Re: Question about double table scans for a table

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Ba Jinsheng <bajinsheng(at)u(dot)nus(dot)edu>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>, Manuel Rigger <rigger(at)nus(dot)edu(dot)sg>
Subject: Re: Question about double table scans for a table
Date: 2023-07-28 00:11:58
Message-ID: CACJufxEH6PC0iqUcogSM_J0L3hN88w4wj4SZvMpBq0Y9Pg88Vg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, Jul 27, 2023 at 4:49 PM Ba Jinsheng <bajinsheng(at)u(dot)nus(dot)edu> wrote:
>
> Hi everyone,
>
>
>
> Consider the query 11 in the TPC-H benchmark:
>
> select
>
> ps_partkey,
>
> sum(ps_supplycost * ps_availqty) as value
>
> from
>
> PARTSUPP,
>
> SUPPLIER,
>
> NATION
>
> where
>
> ps_suppkey = s_suppkey
>
> and s_nationkey = n_nationkey
>
> and n_name = 'MOZAMBIQUE'
>
> group by
>
> ps_partkey
>
> having
>
> sum(ps_supplycost * ps_availqty) > (
>
> select
>
> sum(ps_supplycost * ps_availqty) * 0.0001000000
>
> from
>
> PARTSUPP,
>
> SUPPLIER,
>
> NATION
>
> where
>
> ps_suppkey = s_suppkey
>
> and s_nationkey = n_nationkey
>
> and n_name = 'MOZAMBIQUE'
>
> )
>
> order by
>
> value desc;
>
>

I think you query is equivalent to following:

select
ps_partkey,
sum(ps_supplycost * ps_availqty) filter (where ps_supplycost > 0
and ps_availqty > 0 ) as value
from
PARTSUPP,
SUPPLIER,
NATION
where
ps_suppkey = s_suppkey
and s_nationkey = n_nationkey
and n_name = 'MOZAMBIQUE'
group by
ps_partkey;

maybe you can use inner join like:
select
ps_partkey,
sum(ps_supplycost * ps_availqty) filter (where ps_supplycost > 0
and ps_availqty > 0 ) as value
from PARTSUPP join SUPPLIER on (ps_suppkey = s_suppkey)
join NATION on (s_nationkey = n_nationkey)
where n_name = 'MOZAMBIQUE'
group by
ps_partkey;

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2023-07-28 01:50:50 Re: Fwd: BUG #18016: REINDEX TABLE failure
Previous Message Nathan Bossart 2023-07-27 23:14:41 Re: Fwd: BUG #18016: REINDEX TABLE failure