|
pgdb=# explain -- 22
pgdb-# select
pgdb-# cntrycode,
pgdb-# count(*) as numcust,
pgdb-# sum(c_acctbal) as totacctbal
pgdb-# from
pgdb-# (
pgdb(# select
pgdb(# substr(c_phone ,1 ,2) as cntrycode,
pgdb(# c_acctbal
pgdb(# from
pgdb(# customer
pgdb(# where
pgdb(# substr(c_phone ,1 ,2) in
pgdb(# ('23', '39', '40', '27', '33', '31', '25')
pgdb(# and c_acctbal > (
pgdb(# select
pgdb(# avg(c_acctbal)
pgdb(# from
pgdb(# customer
pgdb(# where
pgdb(# c_acctbal > 0.00
pgdb(# and substr(c_phone ,1 ,2) in
pgdb(# ('23', '39', '40', '27', '33', '31', '25')
pgdb(# )
pgdb(# and not exists (
pgdb(# select
pgdb(# *
pgdb(# from
pgdb(# orders
pgdb(# where
pgdb(# o_custkey = c_custkey
pgdb(# )
pgdb(# ) custsale
pgdb-# group by
pgdb-# cntrycode
pgdb-# order by
pgdb-# cntrycode limit 10;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------
Limit (cost=289126.87..289126.90 rows=1 width=24)
InitPlan 1 (returns $0)
-> Aggregate (cost=75688.04..75688.04 rows=1 width=8)
-> Seq Scan on customer (cost=0.00..75571.00 rows=46813 width=8)
Filter: ((c_acctbal > 0.00) AND (substr((c_phone)::text, 1, 2) = ANY ('{23,39,40,27,33,31,25}'::text[])))
-> GroupAggregate (cost=213438.82..213438.86 rows=1 width=24)
-> Sort (cost=213438.82..213438.83 rows=1 width=24)
Sort Key: (substr((public.customer.c_phone)::text, 1, 2))
-> Nested Loop Anti Join (cost=0.00..213438.81 rows=1 width=24)
-> Seq Scan on customer (cost=0.00..75571.00 rows=17240 width=28)
Filter: ((c_acctbal > $0) AND (substr((c_phone)::text, 1, 2) = ANY ('{23,39,40,27,33,31,25}'::text[])))
-> Index Scan using orders_fk1 on orders (cost=0.00..70.86 rows=17 width=4)
Index Cond: (orders.o_custkey = public.customer.c_custkey)
(13 行)
时间: 20.709 ms
pgdb=# explain -- 17
pgdb-# select
pgdb-# sum(l_extendedprice) / 7.0 as avg_yearly
pgdb-# from
pgdb-# lineitem,
pgdb-# part
pgdb-# where
pgdb-# p_partkey = l_partkey
pgdb-# and p_brand = 'Brand#23'
pgdb-# and p_container = 'MED CAN'
pgdb-# and l_quantity < (
pgdb(# select
pgdb(# 0.2 * avg(l_quantity)
pgdb(# from
pgdb(# lineitem
pgdb(# where
pgdb(# l_partkey = p_partkey
pgdb(# ) limit 10;
QUERY PLAN
----------------------------------------------------------------------------------------------------------
Limit (cost=9090485.90..9090485.91 rows=1 width=10)
-> Aggregate (cost=9090485.90..9090485.91 rows=1 width=10)
-> Nested Loop (cost=0.00..9090431.30 rows=21839 width=10)
Join Filter: (public.lineitem.l_quantity < (SubPlan 1))
-> Seq Scan on part (cost=0.00..71608.29 rows=2081 width=4)
Filter: ((p_brand = 'Brand#23'::bpchar) AND (p_container = 'MED CAN'::bpchar))
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..134.05 rows=31 width=21)
Index Cond: (public.lineitem.l_partkey = part.p_partkey)
SubPlan 1
-> Aggregate (cost=135.45..135.46 rows=1 width=7)
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..135.37 rows=31 width=7)
Index Cond: (l_partkey = $0)
(12 行)
时间: 3.414 ms
pgdb=# explain -- 17c
pgdb-# select
pgdb-# sum(l_extendedprice) / 7.0 as avg_yearly
pgdb-# from (select l_extendedprice, l_partkey ,l_quantity
pgdb(# from
pgdb(# lineitem,
pgdb(# part
pgdb(# where
pgdb(# p_partkey = l_partkey
pgdb(# and p_brand = 'Brand#23'
pgdb(# and p_container = 'MED CAN'
pgdb(# )b
pgdb-# ,(select l_partkey,0.2 * avg(l_quantity) p2avg_l_quantity
pgdb(# from
pgdb(# (select l_partkey,l_quantity from
pgdb(# lineitem
pgdb(# where
pgdb(# l_partkey in (select p_partkey from part where p_brand = 'Brand#23'and p_container = 'MED CAN')
pgdb(# )a
pgdb(# group by l_partkey
pgdb(# )c
pgdb-# where b.l_partkey=c.l_partkey and b.l_quantity <p2avg_l_quantity
pgdb-# ;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=746887.13..746887.14 rows=1 width=10)
-> Hash Join (cost=710378.97..745168.26 rows=687545 width=10)
Hash Cond: (public.lineitem.l_partkey = public.part.p_partkey)
Join Filter: (public.lineitem.l_quantity < ((0.2 * avg(public.lineitem.l_quantity))))
-> GroupAggregate (cost=357749.59..359223.70 rows=65516 width=11)
-> Sort (cost=357749.59..357913.38 rows=65516 width=11)
Sort Key: public.lineitem.l_partkey
-> Nested Loop (cost=71613.49..351388.45 rows=65516 width=11)
-> HashAggregate (cost=71613.49..71634.30 rows=2081 width=4)
-> Seq Scan on part (cost=0.00..71608.29 rows=2081 width=4)
Filter: ((p_brand = 'Brand#23'::bpchar) AND (p_container = 'MED CAN'::bpchar))
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..134.05 rows=31 width=11)
Index Cond: (public.lineitem.l_partkey = public.part.p_partkey)
-> Hash (cost=351362.44..351362.44 rows=65516 width=25)
-> Nested Loop (cost=0.00..351362.44 rows=65516 width=25)
-> Seq Scan on part (cost=0.00..71608.29 rows=2081 width=4)
Filter: ((p_brand = 'Brand#23'::bpchar) AND (p_container = 'MED CAN'::bpchar))
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..134.05 rows=31 width=21)
Index Cond: (public.lineitem.l_partkey = public.part.p_partkey)
(19 行)
时间: 2.887 ms
pgdb=# explain -- 17d
pgdb-# select
pgdb-# sum(l_extendedprice) / 7.0 as avg_yearly
pgdb-# from (select l_extendedprice, l_partkey ,l_quantity
pgdb(# from
pgdb(# lineitem,
pgdb(# part
pgdb(# where
pgdb(# p_partkey = l_partkey
pgdb(# and p_brand = 'Brand#23'
pgdb(# and p_container = 'MED CAN'
pgdb(# )b
pgdb-# ,(select l_partkey,0.2 * avg(l_quantity) p2avg_l_quantity
pgdb(# from
pgdb(# (select l_partkey,l_quantity from
pgdb(# lineitem ,(select distinct p_partkey from part where p_brand = 'Brand#23'and p_container = 'MED CAN')t
pgdb(# where
pgdb(# l_partkey = p_partkey
pgdb(# )a
pgdb(# group by l_partkey
pgdb(# )c
pgdb-# where b.l_partkey=c.l_partkey and b.l_quantity <p2avg_l_quantity
pgdb-# ;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Aggregate (cost=746835.94..746835.95 rows=1 width=10)
-> Hash Join (cost=710327.78..745117.07 rows=687545 width=10)
Hash Cond: (public.lineitem.l_partkey = public.part.p_partkey)
Join Filter: (public.lineitem.l_quantity < ((0.2 * avg(public.lineitem.l_quantity))))
-> GroupAggregate (cost=357698.40..359172.51 rows=65516 width=11)
-> Sort (cost=357698.40..357862.19 rows=65516 width=11)
Sort Key: public.lineitem.l_partkey
-> Nested Loop (cost=71613.49..351337.26 rows=65516 width=11)
-> HashAggregate (cost=71613.49..71634.30 rows=2081 width=4)
-> Seq Scan on part (cost=0.00..71608.29 rows=2081 width=4)
Filter: ((p_brand = 'Brand#23'::bpchar) AND (p_container = 'MED CAN'::bpchar))
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..134.01 rows=31 width=11)
Index Cond: (public.lineitem.l_partkey = public.part.p_partkey)
-> Hash (cost=351362.44..351362.44 rows=65516 width=25)
-> Nested Loop (cost=0.00..351362.44 rows=65516 width=25)
-> Seq Scan on part (cost=0.00..71608.29 rows=2081 width=4)
Filter: ((p_brand = 'Brand#23'::bpchar) AND (p_container = 'MED CAN'::bpchar))
-> Index Scan using lineitem_fk2 on lineitem (cost=0.00..134.05 rows=31 width=21)
Index Cond: (public.lineitem.l_partkey = public.part.p_partkey)
(19 行)
时间: 2.261 ms
pgdb=# -- 17c
pgdb=# select
pgdb-# sum(l_extendedprice) / 7.0 as avg_yearly
pgdb-# from (select l_extendedprice, l_partkey ,l_quantity
pgdb(# from
pgdb(# lineitem,
pgdb(# part
pgdb(# where
pgdb(# p_partkey = l_partkey
pgdb(# and p_brand = 'Brand#23'
pgdb(# and p_container = 'MED CAN'
pgdb(# )b
pgdb-# ,(select l_partkey,0.2 * avg(l_quantity) p2avg_l_quantity
pgdb(# from
pgdb(# (select l_partkey,l_quantity from
pgdb(# lineitem
pgdb(# where
pgdb(# l_partkey in (select p_partkey from part where p_brand = 'Brand#23'and p_container = 'MED CAN')
pgdb(# )a
pgdb(# group by l_partkey
pgdb(# )c
pgdb-# where b.l_partkey=c.l_partkey and b.l_quantity <p2avg_l_quantity
pgdb-# ;
avg_yearly
----------------------
3188455.648571428571
(1 行)
时间: 2836.308 ms
pgdb=# explain -- 18
pgdb-# select
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice,
pgdb-# sum(l_quantity)
pgdb-# from
pgdb-# customer,
pgdb-# orders,
pgdb-# lineitem
pgdb-# where
pgdb-# o_orderkey in (
pgdb(# select
pgdb(# l_orderkey
pgdb(# from
pgdb(# lineitem
pgdb(# group by
pgdb(# l_orderkey having
pgdb(# sum(l_quantity) > 313
pgdb(# )
pgdb-# and c_custkey = o_custkey
pgdb-# and o_orderkey = l_orderkey
pgdb-# group by
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice
pgdb-# order by
pgdb-# o_totalprice desc,
pgdb-# o_orderdate limit 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=15154938.05..15154938.35 rows=10 width=48)
-> GroupAggregate (cost=15154938.05..16054728.83 rows=29993026 width=48)
-> Sort (cost=15154938.05..15229920.62 rows=29993026 width=48)
Sort Key: orders.o_totalprice, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey
-> Hash Join (cost=4253312.16..7739531.59 rows=29993026 width=48)
Hash Cond: (public.lineitem.l_orderkey = orders.o_orderkey)
-> Seq Scan on lineitem (cost=0.00..1784743.52 rows=59986052 width=11)
-> Hash (cost=4093644.16..4093644.16 rows=7500000 width=45)
-> Hash Join (cost=78736.00..4093644.16 rows=7500000 width=45)
Hash Cond: (orders.o_custkey = customer.c_custkey)
-> Merge Semi Join (cost=0.00..3753578.16 rows=7500000 width=26)
Merge Cond: (orders.o_orderkey = public.lineitem.l_orderkey)
-> Index Scan using orders_pkey on orders (cost=0.00..657372.90 rows=15000000 width=22)
-> Materialize (cost=0.00..3053651.71 rows=404285 width=4)
-> GroupAggregate (cost=0.00..3048598.14 rows=404285 width=11)
Filter: (sum(public.lineitem.l_quantity) > 313::numeric)
-> Index Scan using lineitem_fk1 on lineitem (cost=0.00..2742603.61 rows=59986052 width=11)
-> Hash (cost=51196.00..51196.00 rows=1500000 width=23)
-> Seq Scan on customer (cost=0.00..51196.00 rows=1500000 width=23)
(19 行)
时间: 50.062 ms
pgdb=# explain -- 18c
pgdb-# select
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice,
pgdb-# sum(l_quantity)
pgdb-# from
pgdb-# customer,
pgdb-# orders,
pgdb-# lineitem l,
pgdb-# (
pgdb(# select
pgdb(# l_orderkey
pgdb(# from
pgdb(# lineitem
pgdb(# group by
pgdb(# l_orderkey having
pgdb(# sum(l_quantity) > 313
pgdb(# )l1
pgdb-# where
pgdb-# o_orderkey =l1.l_orderkey
pgdb-# and c_custkey = o_custkey
pgdb-# and o_orderkey = l.l_orderkey
pgdb-# group by
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice
pgdb-# order by
pgdb-# o_totalprice desc,
pgdb-# o_orderdate limit 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=6793460.75..6793461.05 rows=10 width=48)
-> GroupAggregate (cost=6793460.75..6841963.67 rows=1616764 width=48)
-> Sort (cost=6793460.75..6797502.66 rows=1616764 width=48)
Sort Key: orders.o_totalprice, orders.o_orderdate, customer.c_name, customer.c_custkey, orders.o_orderkey
-> Hash Join (cost=3862314.71..6477529.57 rows=1616764 width=48)
Hash Cond: (l.l_orderkey = orders.o_orderkey)
-> Seq Scan on lineitem l (cost=0.00..1784743.52 rows=59986052 width=11)
-> Hash (cost=3853707.15..3853707.15 rows=404285 width=45)
-> Hash Join (cost=78736.00..3853707.15 rows=404285 width=45)
Hash Cond: (orders.o_custkey = customer.c_custkey)
-> Merge Join (cost=0.00..3752567.45 rows=404285 width=26)
Merge Cond: (lineitem.l_orderkey = orders.o_orderkey)
-> GroupAggregate (cost=0.00..3048598.14 rows=404285 width=11)
Filter: (sum(lineitem.l_quantity) > 313::numeric)
-> Index Scan using lineitem_fk1 on lineitem (cost=0.00..2742603.61 rows=59986052 width=11)
-> Index Scan using orders_pkey on orders (cost=0.00..657372.90 rows=15000000 width=22)
-> Hash (cost=51196.00..51196.00 rows=1500000 width=23)
-> Seq Scan on customer (cost=0.00..51196.00 rows=1500000 width=23)
(18 行)
时间: 2.452 ms
pgdb=# -- 18c
pgdb=# select
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice,
pgdb-# sum(l_quantity)
pgdb-# from
pgdb-# customer,
pgdb-# orders,
pgdb-# lineitem l,
pgdb-# (
pgdb(# select
pgdb(# l_orderkey
pgdb(# from
pgdb(# lineitem
pgdb(# group by
pgdb(# l_orderkey having
pgdb(# sum(l_quantity) > 313
pgdb(# )l1
pgdb-# where
pgdb-# o_orderkey =l1.l_orderkey
pgdb-# and c_custkey = o_custkey
pgdb-# and o_orderkey = l.l_orderkey
pgdb-# group by
pgdb-# c_name,
pgdb-# c_custkey,
pgdb-# o_orderkey,
pgdb-# o_orderdate,
pgdb-# o_totalprice
pgdb-# order by
pgdb-# o_totalprice desc,
pgdb-# o_orderdate limit 10;
c_name | c_custkey | o_orderkey | o_orderdate | o_totalprice | sum
--------------------+-----------+------------+-------------+--------------+--------
Customer#001287812 | 1287812 | 42290181 | 1997-11-26 | 558289.17 | 318.00
Customer#001172513 | 1172513 | 36667107 | 1997-06-06 | 550142.18 | 322.00
Customer#000571654 | 571654 | 21213895 | 1992-01-03 | 549380.08 | 327.00
Customer#000667882 | 667882 | 2199712 | 1996-09-30 | 542154.01 | 327.00
Customer#001471966 | 1471966 | 1263015 | 1997-02-02 | 540476.80 | 320.00
Customer#001101754 | 1101754 | 46794464 | 1992-04-28 | 532623.90 | 320.00
Customer#000591466 | 591466 | 55799200 | 1996-02-11 | 524209.45 | 327.00
Customer#001196317 | 1196317 | 34509573 | 1995-10-15 | 522897.01 | 318.00
Customer#000634048 | 634048 | 36327201 | 1992-04-25 | 519634.30 | 315.00
Customer#001308376 | 1308376 | 28077922 | 1996-10-27 | 518711.99 | 319.00
(10 行)
时间: 93872.013 ms
p |
|