Postgresql にクエリがあります。日付 BETWEEN '2012-01-01' AND '2012-01-04' を実行すると、レコード セットが小さいにもかかわらず、BETWEEN '2012-01-01' を実行すると遅くなります。 AND '2012-04-17'
これがなぜなのか教えてもらえますか?
より遅いクエリは次のとおりです。
"HashAggregate (cost=9388.82..9388.88 rows=1 width=311) (actual time=187787.183..187788.001 rows=153 loops=1)"
" -> Nested Loop (cost=9266.68..9388.77 rows=1 width=311) (actual time=8432.315..187785.091 rows=153 loops=1)"
" Join Filter: ((oh.ord_id = ord_line.ord_id) AND (iis.item_id = ord_line.item_id))"
" -> Nested Loop Left Join (cost=9266.68..9373.81 rows=1 width=333) (actual time=92.238..147161.052 rows=5847933 loops=1)"
" Join Filter: ((oh.division)::text = (item_color_cd.division)::text)"
" -> Nested Loop Left Join (cost=9266.68..9373.53 rows=1 width=322) (actual time=92.214..113058.202 rows=5847933 loops=1)"
" Join Filter: ((oh.division)::text = (item_size_cd.division)::text)"
" -> Nested Loop Left Join (cost=9266.68..9373.24 rows=1 width=312) (actual time=92.189..77932.977 rows=5847933 loops=1)"
" Join Filter: ((oh.division)::text = (item_class_cd.division)::text)"
" -> Nested Loop (cost=9266.68..9372.92 rows=1 width=294) (actual time=92.183..61183.081 rows=5847933 loops=1)"
" -> Nested Loop (cost=67.45..91.07 rows=1 width=75) (actual time=0.819..58.193 rows=8463 loops=1)"
" -> Nested Loop (cost=67.45..75.24 rows=1 width=51) (actual time=0.784..3.894 rows=360 loops=1)"
" -> Index Scan using item_dept_cd_division_key on item_dept_cd (cost=0.00..5.27 rows=1 width=27) (actual time=0.045..0.047 rows=1 loops=1)"
" Index Cond: (((division)::text = 'BSM'::text) AND ((dept_cd)::text = 'S'::text))"
" Filter: ((dept_cd)::text ~~ 'S'::text)"
" -> Bitmap Heap Scan on item_item_num iin (cost=67.45..69.96 rows=1 width=32) (actual time=0.734..3.315 rows=360 loops=1)"
" Recheck Cond: ((iin.dept_cd_id = item_dept_cd.dept_cd_id) AND ((iin.division)::text = 'BSM'::text))"
" -> BitmapAnd (cost=67.45..67.45 rows=1 width=0) (actual time=0.687..0.687 rows=0 loops=1)"
" -> Bitmap Index Scan on item_item_num_dept_id (cost=0.00..6.92 rows=213 width=0) (actual time=0.109..0.109 rows=364 loops=1)"
" Index Cond: (iin.dept_cd_id = item_dept_cd.dept_cd_id)"
" -> Bitmap Index Scan on item_item_num_division_key (cost=0.00..59.64 rows=2573 width=0) (actual time=0.544..0.544 rows=2753 loops=1)"
" Index Cond: ((iin.division)::text = 'BSM'::text)"
" -> Index Scan using item_sku_item_num_id_key on item_sku iis (cost=0.00..15.82 rows=1 width=32) (actual time=0.021..0.104 rows=24 loops=360)"
" Index Cond: (iis.item_num_id = iin.item_num_id)"
" Filter: ((iis.division)::text = 'BSM'::text)"
" -> HashAggregate (cost=9199.23..9281.69 rows=8 width=66) (actual time=0.020..6.298 rows=691 loops=8463)"
" -> Nested Loop (cost=0.01..9198.91 rows=8 width=66) (actual time=1.111..89.484 rows=691 loops=1)"
" -> Index Scan using ledger_demand_ix4 on ledger_demand ls (cost=0.01..4512.71 rows=710 width=35) (actual time=1.078..79.178 rows=691 loops=1)"
" Index Cond: ((date(trans_date) >= '2012-01-01'::date) AND (date(trans_date) <= '2012-01-04'::date))"
" Filter: (((division)::text = 'BSM'::text) AND (((qty_ordered > 0) AND ((ledger_code)::text ~~ 'D%'::text)) OR ((ledger_code)::text ~~ 'DC%'::text)))"
" -> Index Scan using ord_header_pkey on ord_header oh (cost=0.00..6.59 rows=1 width=39) (actual time=0.010..0.011 rows=1 loops=691)"
" Index Cond: (oh.ord_id = ls.ord_id)"
" Filter: ((oh.division)::text = 'BSM'::text)"
" SubPlan 1"
" -> Aggregate (cost=10.25..10.26 rows=1 width=8) (actual time=0.005..0.006 rows=1 loops=5847933)"
" -> Index Scan using ledger_discount_ord_line_id on ledger_discount (cost=0.00..10.25 rows=1 width=8) (actual time=0.003..0.004 rows=0 loops=5847933)"
" Index Cond: (ord_line_id = $2)"
" Filter: (((ledger_code)::text ~~ 'D%'::text) AND ((division)::text = ($0)::text) AND (ord_id = $1) AND (date(trans_date) = date($3)))"
" -> Index Scan using item_class_cd_pkey on item_class_cd (cost=0.00..0.30 rows=1 width=31) (actual time=0.001..0.001 rows=0 loops=5847933)"
" Index Cond: (iin.class_cd_id = item_class_cd.class_cd_id)"
" Filter: ((item_class_cd.division)::text = 'BSM'::text)"
" -> Index Scan using item_size_cd_pkey on item_size_cd (cost=0.00..0.27 rows=1 width=25) (actual time=0.002..0.003 rows=1 loops=5847933)"
" Index Cond: (iis.size_cd_id = item_size_cd.size_cd_id)"
" Filter: ((item_size_cd.division)::text = 'BSM'::text)"
" -> Index Scan using item_color_cd_pkey on item_color_cd (cost=0.00..0.27 rows=1 width=25) (actual time=0.002..0.003 rows=1 loops=5847933)"
" Index Cond: (iis.color_cd_id = item_color_cd.color_cd_id)"
" Filter: ((item_color_cd.division)::text = 'BSM'::text)"
" -> Index Scan using ord_line_pkey on ord_line (cost=0.00..14.94 rows=1 width=16) (actual time=0.004..0.005 rows=1 loops=5847933)"
" Index Cond: (ord_line.ord_line_id = ls.ord_line_id)"
" Filter: ((ord_line.division)::text = 'BSM'::text)"
"Total runtime: 187788.419 ms"
そして、より高速なクエリの場合:
"HashAggregate (cost=395482.02..395482.08 rows=1 width=311) (actual time=3176.868..3204.741 rows=4900 loops=1)"
" -> Nested Loop Left Join (cost=391021.12..395481.97 rows=1 width=311) (actual time=2074.121..3151.576 rows=5118 loops=1)"
" Join Filter: ((oh.division)::text = (item_color_cd.division)::text)"
" -> Nested Loop Left Join (cost=391021.12..395481.69 rows=1 width=314) (actual time=2074.086..3113.537 rows=5118 loops=1)"
" Join Filter: ((oh.division)::text = (item_size_cd.division)::text)"
" -> Nested Loop Left Join (cost=391021.12..395481.40 rows=1 width=304) (actual time=2074.067..3077.820 rows=5118 loops=1)"
" Join Filter: ((oh.division)::text = (item_class_cd.division)::text)"
" -> Hash Join (cost=391021.12..395481.11 rows=1 width=286) (actual time=2074.062..3061.180 rows=5118 loops=1)"
" Hash Cond: ((oh.ord_id = ord_line.ord_id) AND (ls.ord_line_id = ord_line.ord_line_id))"
" -> HashAggregate (cost=390858.20..395310.62 rows=432 width=66) (actual time=1089.300..1596.212 rows=38947 loops=1)"
" -> Nested Loop (cost=92108.27..390840.92 rows=432 width=66) (actual time=603.101..1004.552 rows=38947 loops=1)"
" -> Bitmap Heap Scan on ledger_demand ls (cost=92108.27..174982.73 rows=36833 width=35) (actual time=603.076..708.321 rows=38947 loops=1)"
" Recheck Cond: (((division)::text = 'BSM'::text) AND (date(trans_date) >= '2012-01-01'::date) AND (date(trans_date) <= '2012-04-17'::date))"
" Filter: (((qty_ordered > 0) AND ((ledger_code)::text ~~ 'D%'::text)) OR ((ledger_code)::text ~~ 'DC%'::text))"
" -> BitmapAnd (cost=92108.27..92108.27 rows=37705 width=0) (actual time=600.722..600.722 rows=0 loops=1)"
" -> Bitmap Index Scan on ledger_demand_ix2 (cost=0.00..12712.18 rows=559935 width=0) (actual time=91.763..91.763 rows=572985 loops=1)"
" Index Cond: ((division)::text = 'BSM'::text)"
" -> Bitmap Index Scan on ledger_demand_ix4 (cost=0.00..79377.42 rows=4397941 width=0) (actual time=483.385..483.385 rows=4444914 loops=1)"
" Index Cond: ((date(trans_date) >= '2012-01-01'::date) AND (date(trans_date) <= '2012-04-17'::date))"
" -> Index Scan using ord_header_pkey on ord_header oh (cost=0.00..5.85 rows=1 width=39) (actual time=0.004..0.005 rows=1 loops=38947)"
" Index Cond: (oh.ord_id = ls.ord_id)"
" Filter: ((oh.division)::text = 'BSM'::text)"
" SubPlan 1"
" -> Aggregate (cost=10.25..10.26 rows=1 width=8) (actual time=0.009..0.009 rows=1 loops=38947)"
" -> Index Scan using ledger_discount_ord_line_id on ledger_discount (cost=0.00..10.25 rows=1 width=8) (actual time=0.007..0.007 rows=0 loops=38947)"
" Index Cond: (ord_line_id = $2)"
" Filter: (((ledger_code)::text ~~ 'D%'::text) AND ((division)::text = ($0)::text) AND (ord_id = $1) AND (date(trans_date) = date($3)))"
" -> Hash (cost=162.91..162.91 rows=1 width=83) (actual time=984.452..984.452 rows=112897 loops=1)"
" -> Nested Loop (cost=67.45..162.91 rows=1 width=83) (actual time=0.736..852.432 rows=112897 loops=1)"
" -> Nested Loop (cost=67.45..91.07 rows=1 width=75) (actual time=0.705..45.978 rows=8463 loops=1)"
" -> Nested Loop (cost=67.45..75.24 rows=1 width=51) (actual time=0.675..4.358 rows=360 loops=1)"
" -> Index Scan using item_dept_cd_division_key on item_dept_cd (cost=0.00..5.27 rows=1 width=27) (actual time=0.030..0.032 rows=1 loops=1)"
" Index Cond: (((division)::text = 'BSM'::text) AND ((dept_cd)::text = 'S'::text))"
" Filter: ((dept_cd)::text ~~ 'S'::text)"
" -> Bitmap Heap Scan on item_item_num iin (cost=67.45..69.96 rows=1 width=32) (actual time=0.641..3.818 rows=360 loops=1)"
" Recheck Cond: ((iin.dept_cd_id = item_dept_cd.dept_cd_id) AND ((iin.division)::text = 'BSM'::text))"
" -> BitmapAnd (cost=67.45..67.45 rows=1 width=0) (actual time=0.598..0.598 rows=0 loops=1)"
" -> Bitmap Index Scan on item_item_num_dept_id (cost=0.00..6.92 rows=213 width=0) (actual time=0.089..0.089 rows=364 loops=1)"
" Index Cond: (iin.dept_cd_id = item_dept_cd.dept_cd_id)"
" -> Bitmap Index Scan on item_item_num_division_key (cost=0.00..59.64 rows=2573 width=0) (actual time=0.479..0.479 rows=2753 loops=1)"
" Index Cond: ((iin.division)::text = 'BSM'::text)"
" -> Index Scan using item_sku_item_num_id_key on item_sku iis (cost=0.00..15.82 rows=1 width=32) (actual time=0.015..0.074 rows=24 loops=360)"
" Index Cond: (iis.item_num_id = iin.item_num_id)"
" Filter: ((iis.division)::text = 'BSM'::text)"
" -> Index Scan using ord_line_item_id_idx on ord_line (cost=0.00..71.79 rows=3 width=16) (actual time=0.009..0.072 rows=13 loops=8463)"
" Index Cond: (ord_line.item_id = iis.item_id)"
" Filter: ((ord_line.division)::text = 'BSM'::text)"
" -> Index Scan using item_class_cd_pkey on item_class_cd (cost=0.00..0.27 rows=1 width=31) (actual time=0.001..0.001 rows=0 loops=5118)"
" Index Cond: (iin.class_cd_id = item_class_cd.class_cd_id)"
" Filter: ((item_class_cd.division)::text = 'BSM'::text)"
" -> Index Scan using item_size_cd_pkey on item_size_cd (cost=0.00..0.27 rows=1 width=25) (actual time=0.003..0.004 rows=1 loops=5118)"
" Index Cond: (iis.size_cd_id = item_size_cd.size_cd_id)"
" Filter: ((item_size_cd.division)::text = 'BSM'::text)"
" -> Index Scan using item_color_cd_pkey on item_color_cd (cost=0.00..0.27 rows=1 width=25) (actual time=0.003..0.004 rows=1 loops=5118)"
" Index Cond: (iis.color_cd_id = item_color_cd.color_cd_id)"
" Filter: ((item_color_cd.division)::text = 'BSM'::text)"
"Total runtime: 3208.876 ms"
クエリは、WHERE DATE(trans_date) BETWEEN '2012-04-01' AND '2012-04-17' のみを変更したものです。
SELECT trans_date AS order_date,
iin.item_num AS item_num,
color_cd AS color_code,
color_desc AS color_description,
size_cd AS size_code,
size_desc AS size_description,
class_cd AS class_code,
class_desc AS class_description,
dept_cd AS dept_code,
dept_desc AS dept_description,
Count(ord_id) AS num_of_orders,
Sum(vs.qty_ordered) AS units,
To_char(Sum(merch_amt_new3),'99G999G990.00') AS merch_amt,
To_char(Sum(gift_cert_new3 - gift_cert_canceled_new3),
'99G999G990.00') AS gift_cert,
To_char(Sum(discount_amt),'99G999G990.00') AS discount_amt,
To_char(Sum(tax_amt),'99G999G990.00') AS tax,
To_char(Sum(vs.qty_ordered * Coalesce(iis.cost,iin.cost)),
'99G999G990.00') AS extended_cost,
To_char(Sum(merch_amt - discount_amt),'99G999G990.00') AS total,
To_char(Sum(canceled_merch_new3),'99G999G990.00') AS canceled_merch,
iis.sku_id
FROM (SELECT oh.division,
oh.ord_id,
client_order_num,
source_id,
bus_type_id,
order_type_id,
Date(ls.trans_date) AS trans_date,
ls.ord_line_id,
Sum(ls.qty_ordered) AS qty_ordered,
Sum(ls.amount) AS merch_amt,
Sum(tax) AS tax_amt,
Coalesce((SELECT Sum(ledger_discount.amount) AS SUM
FROM ledger_discount
WHERE ledger_discount.ledger_code ~~ 'D%'
AND ledger_discount.division = oh.division
AND ledger_discount.ord_id = oh.ord_id
AND ledger_discount.ord_line_id = ls.ord_line_id
AND Date(ledger_discount.trans_date) = Date(ls.trans_date)),
0.0) AS discount_amt,
(CASE
WHEN ls.ledger_code = 'DO'
THEN Sum(ls.amount)
ELSE 0.0
END) AS merch_amt_new3,
(CASE
WHEN ls.ledger_code = 'DOG'
THEN Sum(ls.amount)
ELSE 0.0
END) AS gift_cert_new3,
(CASE
WHEN ls.ledger_code = 'DCG'
THEN Sum(-ls.amount)
ELSE 0.0
END) AS gift_cert_canceled_new3,
(CASE
WHEN ls.ledger_code = 'DC'
THEN Sum(-ls.amount)
ELSE 0.0
END) AS canceled_merch_new3
FROM ledger_demand ls
JOIN ord_header oh
USING(division,ord_id)
WHERE (ls.qty_ordered > 0
AND ls.ledger_code ~~ 'D%')
OR (ls.ledger_code ~~ 'DC%')
GROUP BY oh.division,
oh.ord_id,
client_order_num,
source_id,
bus_type_id,
order_type_id,
ls.trans_date,
ls.ord_line_id,
ls.ledger_code
) AS vs
JOIN ord_line
USING(division,ord_id,ord_line_id)
JOIN item_sku AS iis
USING(division,item_id)
JOIN item_item_num AS iin
USING(division,item_num_id)
LEFT JOIN item_color_cd
USING(division,color_cd_id)
LEFT JOIN item_size_cd
USING(division,size_cd_id)
LEFT JOIN item_class_cd
USING(division,class_cd_id)
LEFT JOIN item_dept_cd
USING(division,dept_cd_id)
WHERE DATE(trans_date) BETWEEN '2012-04-01' AND '2012-04-17'
AND item_dept_cd.dept_cd LIKE 'S'
AND division = 'BSM'
GROUP BY trans_date,
iin.item_num,
color_cd,
color_desc,
size_cd,
size_desc,
class_cd,
class_desc,
dept_cd,
dept_desc,
iis.sku_id