Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QUERY PLAN
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- WindowAgg (cost=248.25..263.39 rows=4 width=228) (actual time=39.176..39.176 rows=1 loops=1)
- Output: sum(t.group_suma) OVER (?), t.group_cost, t.group_suma, t.consumed, t.consumed_period, t.o, ((t.o).id)
- Buffers: shared hit=1071
- -> Subquery Scan on t (cost=248.25..263.33 rows=4 width=220) (actual time=39.165..39.166 rows=1 loops=1)
- Output: (t.o).id, t.group_suma, t.group_cost, t.consumed, t.consumed_period, t.o
- Buffers: shared hit=1071
- -> GroupAggregate (cost=248.25..263.29 rows=4 width=216) (actual time=39.163..39.164 rows=1 loops=1)
- Output: sum(cis.item_cost), sum(cis.item_suma), max(ic.consumed), ic.consumed_period, o.*
- Group Key: o.*, ic.consumed_period
- Buffers: shared hit=1071
- -> Sort (cost=248.25..250.75 rows=1000 width=216) (actual time=39.100..39.101 rows=5 loops=1)
- Output: ic.consumed_period, o.*, cis.item_cost, cis.item_suma, ic.consumed
- Sort Key: o.*, ic.consumed_period
- Sort Method: quicksort Memory: 27kB
- Buffers: shared hit=1062
- -> Nested Loop Left Join (cost=36.56..198.42 rows=1000 width=216) (actual time=4.184..39.024 rows=5 loops=1)
- Output: ic.consumed_period, o.*, cis.item_cost, cis.item_suma, ic.consumed
- Buffers: shared hit=1055
- -> Nested Loop (cost=36.31..178.17 rows=1 width=417) (actual time=3.677..16.561 rows=5 loops=1)
- Output: o.*, od.*, sp.*, ic.consumed, ic.consumed_period, ic.*
- Buffers: shared hit=545
- -> Nested Loop Left Join (cost=35.22..168.92 rows=1 width=388) (actual time=2.758..14.964 rows=20 loops=1)
- Output: o.*, o.app_period, split_period.split_period, od.app_period, od.*, sp.*
- Join Filter: (od.service_type_id = sp.service_type_id)
- Rows Removed by Join Filter: 1280
- Buffers: shared hit=370
- -> Nested Loop (cost=34.97..156.11 rows=1 width=368) (actual time=0.808..1.374 rows=20 loops=1)
- Output: o.*, o.service_level_id, o.app_period, o.period_id, split_period.split_period, od.app_period, od.*, od.service_type_id
- Join Filter: ((od.app_period * o.app_period) && split_period.split_period)
- Rows Removed by Join Filter: 12
- Buffers: shared hit=79
- -> Nested Loop (cost=34.69..141.49 rows=1 width=226) (actual time=0.767..0.789 rows=4 loops=1)
- Output: o.*, o.id, o.service_level_id, o.app_period, o.period_id, split_period.split_period
- Buffers: shared hit=43
- -> Bitmap Heap Scan on public.order_bt o (cost=4.44..18.76 rows=4 width=194) (actual time=0.103..0.112 rows=3 loops=1)
- Output: o.*, o.id, o.service_level_id, o.app_period, o.period_id
- Recheck Cond: ((o.id = 6154) AND (o.sys_period @> sys_time()))
- Heap Blocks: exact=2
- Buffers: shared hit=4
- -> Bitmap Index Scan on order_id_sys_period_app_period_excl (cost=0.00..4.44 rows=4 width=0) (actual time=0.077..0.077 rows=3 loops=1)
- Index Cond: ((o.id = 6154) AND (o.sys_period @> sys_time()))
- Buffers: shared hit=2
- -> Subquery Scan on split_period (cost=30.25..30.67 rows=1 width=32) (actual time=0.217..0.223 rows=1 loops=3)
- Output: split_period.split_period
- Filter: ((o.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange) && split_period.split_period)
- Rows Removed by Filter: 3
- Buffers: shared hit=39
- -> Subquery Scan on x (cost=30.25..30.55 rows=8 width=32) (actual time=0.210..0.219 rows=4 loops=3)
- Output: x.app_period
- Filter: (x.boundary IS NOT NULL)
- Rows Removed by Filter: 1
- Buffers: shared hit=39
- CTE ranges
- -> HashAggregate (cost=29.69..29.73 rows=4 width=32) (actual time=0.176..0.177 rows=5 loops=3)
- Output: ((o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Group Key: ((o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Buffers: shared hit=36
- -> Append (cost=4.42..29.68 rows=4 width=32) (actual time=0.043..0.166 rows=9 loops=3)
- Buffers: shared hit=36
- -> Bitmap Heap Scan on public.order_bt o_1 (cost=4.42..12.00 rows=2 width=32) (actual time=0.040..0.040 rows=1 loops=3)
- Output: (o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange)
- Recheck Cond: ((o_1.id = $0) AND (o_1.sys_period @> sys_time()) AND (o_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Heap Blocks: exact=3
- Buffers: shared hit=9
- -> Bitmap Index Scan on order_id_sys_period_app_period_excl (cost=0.00..4.42 rows=2 width=0) (actual time=0.014..0.014 rows=1 loops=3)
- Index Cond: ((o_1.id = $0) AND (o_1.sys_period @> sys_time()) AND (o_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Buffers: shared hit=6
- -> Index Scan using order_detail_idx_order_id on public.order_detail_bt od_1 (cost=0.29..17.63 rows=2 width=32) (actual time=0.036..0.120 rows=8 loops=3)
- Output: (od_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange)
- Index Cond: (od_1.order_id = $0)
- Filter: ((od_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange) AND (od_1.sys_period @> sys_time()))
- Rows Removed by Filter: 3
- Buffers: shared hit=27
- -> WindowAgg (cost=0.52..0.74 rows=8 width=40) (actual time=0.209..0.217 rows=5 loops=3)
- Output: tstzrange((lower(ranges.app_period)), lead((lower(ranges.app_period))) OVER (?)), lead((lower(ranges.app_period))) OVER (?)
- Buffers: shared hit=39
- -> Sort (cost=0.52..0.54 rows=8 width=8) (actual time=0.200..0.201 rows=5 loops=3)
- Output: (lower(ranges.app_period))
- Sort Key: (lower(ranges.app_period))
- Sort Method: quicksort Memory: 25kB
- Buffers: shared hit=39
- -> HashAggregate (cost=0.32..0.40 rows=8 width=8) (actual time=0.189..0.190 rows=5 loops=3)
- Output: (lower(ranges.app_period))
- Group Key: (lower(ranges.app_period))
- Buffers: shared hit=36
- -> Append (cost=0.00..0.30 rows=8 width=8) (actual time=0.179..0.185 rows=10 loops=3)
- Buffers: shared hit=36
- -> CTE Scan on ranges (cost=0.00..0.09 rows=4 width=8) (actual time=0.178..0.181 rows=5 loops=3)
- Output: lower(ranges.app_period)
- Buffers: shared hit=36
- -> CTE Scan on ranges ranges_1 (cost=0.00..0.09 rows=4 width=8) (actual time=0.001..0.002 rows=5 loops=3)
- Output: upper(ranges_1.app_period)
- -> Index Scan using order_detail_idx_order_id on public.order_detail_bt od (cost=0.29..14.59 rows=2 width=150) (actual time=0.026..0.136 rows=8 loops=4)
- Output: od.app_period, od.*, od.order_id, od.service_type_id
- Index Cond: (od.order_id = 6154)
- Filter: (od.sys_period @> sys_time())
- Rows Removed by Filter: 3
- Buffers: shared hit=36
- -> Function Scan on public.service_level_price sp (cost=0.25..12.75 rows=5 width=40) (actual time=0.636..0.669 rows=65 loops=20)
- Output: sp.*, sp.service_type_id, sp.period_id
- Function Call: service_level_price(o.service_level_id)
- Filter: (o.period_id = sp.period_id)
- Rows Removed by Filter: 38
- Buffers: shared hit=291
- -> Subquery Scan on ic (cost=1.09..9.24 rows=1 width=104) (actual time=0.078..0.079 rows=0 loops=20)
- Output: ic.consumed, ic.consumed_period, ic.*
- Buffers: shared hit=175
- -> Result (cost=1.09..9.23 rows=1 width=72) (actual time=0.077..0.077 rows=0 loops=20)
- Output: ROW(cr.id, cr.rate, cr.docdate, cr.from_currency_id, cr.to_currency_id, cr.app_period)::currency_rate, interval_length(((od.app_period * o.app_period) * split_period.split_period), $3), COALESCE(((od.app_period * o.app_period) * split_period.split_period), ((od.app_period * o.app_period) * split_period.split_period))
- One-Time Filter: (COALESCE(((od.app_period * o.app_period) * split_period.split_period), ((od.app_period * o.app_period) * split_period.split_period)) @> '2019-04-01 00:00:00+03'::timestamp with time zone)
- Buffers: shared hit=175
- InitPlan 2 (returns $3)
- -> Seq Scan on public.period (cost=0.00..1.07 rows=1 width=16) (actual time=0.005..0.007 rows=1 loops=5)
- Output: period."interval"
- Filter: (period.id = ($2).period_id)
- Rows Removed by Filter: 5
- Buffers: shared hit=5
- -> Nested Loop Left Join (cost=0.01..7.89 rows=1 width=56) (actual time=0.062..0.066 rows=1 loops=5)
- Output: cr.id, cr.rate, cr.docdate, cr.from_currency_id, cr.to_currency_id, cr.app_period
- Buffers: shared hit=5
- -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.000..0.000 rows=1 loops=5)
- -> Seq Scan on public.currency_rate_bt cr (cost=0.00..7.86 rows=1 width=56) (actual time=0.059..0.062 rows=1 loops=5)
- Output: cr.id, cr.rate, cr.docdate, cr.from_currency_id, cr.to_currency_id, cr.sid, cr.app_period, cr.sys_period
- Filter: ((cr.from_currency_id = (sp.*).currency_id) AND (cr.app_period @> lower(((od.app_period * o.app_period) * split_period.split_period))) AND (cr.sys_period @> sys_time()) AND (cr.to_currency_id = app_currency()))
- Rows Removed by Filter: 12
- Buffers: shared hit=5
- -> Function Scan on public.calc_item_suma cis (cost=0.25..10.25 rows=1000 width=16) (actual time=4.489..4.489 rows=1 loops=5)
- Output: cis.item_qty, cis.item_price, cis.item_cost, cis.item_suma
- Function Call: calc_item_suma(o.*, od.*, sp.*, ic.*)
- Buffers: shared hit=510
- Planning Time: 10.940 ms
- Execution Time: 40.260 ms
- (132 rows)
- QUERY PLAN
- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- Subquery Scan on ots (cost=86089.99..86096.49 rows=1 width=224) (actual time=2650.454..2651.194 rows=1 loops=1)
- Output: ots.total_suma, ots.group_cost, ots.group_suma, ots.consumed, ots.consumed_period, ots.o
- Filter: ((ots.consumed_period @> '2019-04-01 00:00:00+03'::timestamp with time zone) AND ((ots.o).id = 6154))
- Rows Removed by Filter: 877
- Buffers: shared hit=50838
- -> WindowAgg (cost=86089.99..86093.49 rows=200 width=228) (actual time=2650.185..2650.987 rows=878 loops=1)
- Output: sum(t.group_suma) OVER (?), t.group_cost, t.group_suma, t.consumed, t.consumed_period, t.o, ((t.o).id)
- Buffers: shared hit=50838
- -> Sort (cost=86089.99..86090.49 rows=200 width=220) (actual time=2650.174..2650.239 rows=878 loops=1)
- Output: ((t.o).id), t.group_suma, t.group_cost, t.consumed, t.consumed_period, t.o
- Sort Key: ((t.o).id)
- Sort Method: quicksort Memory: 272kB
- Buffers: shared hit=50838
- -> Subquery Scan on t (cost=86063.34..86082.34 rows=200 width=220) (actual time=2645.389..2649.952 rows=878 loops=1)
- Output: (t.o).id, t.group_suma, t.group_cost, t.consumed, t.consumed_period, t.o
- Buffers: shared hit=50838
- -> GroupAggregate (cost=86063.34..86080.34 rows=200 width=216) (actual time=2645.387..2649.751 rows=878 loops=1)
- Output: sum(cis.item_cost), sum(cis.item_suma), max(ic.consumed), ic.consumed_period, o.*
- Group Key: o.*, ic.consumed_period
- Buffers: shared hit=50838
- -> Sort (cost=86063.34..86065.84 rows=1000 width=216) (actual time=2645.353..2645.563 rows=2884 loops=1)
- Output: ic.consumed_period, o.*, cis.item_cost, cis.item_suma, ic.consumed
- Sort Key: o.*, ic.consumed_period
- Sort Method: quicksort Memory: 898kB
- Buffers: shared hit=50838
- -> Nested Loop Left Join (cost=32.51..86013.51 rows=1000 width=216) (actual time=1.756..2625.800 rows=2884 loops=1)
- Output: ic.consumed_period, o.*, cis.item_cost, cis.item_suma, ic.consumed
- Buffers: shared hit=50838
- -> Nested Loop Left Join (cost=32.26..85993.26 rows=1 width=417) (actual time=1.675..2330.464 rows=2884 loops=1)
- Output: o.*, od.*, sp.*, ic.consumed, ic.consumed_period, ic.*
- Buffers: shared hit=47880
- -> Nested Loop Left Join (cost=31.18..85984.03 rows=1 width=388) (actual time=1.520..2082.150 rows=2884 loops=1)
- Output: o.*, o.app_period, split_period.split_period, od.app_period, od.*, sp.*
- Join Filter: (od.service_type_id = sp.service_type_id)
- Rows Removed by Join Filter: 164206
- Buffers: shared hit=42112
- -> Nested Loop (cost=30.93..85971.21 rows=1 width=368) (actual time=0.354..363.420 rows=2882 loops=1)
- Output: o.*, o.service_level_id, o.app_period, o.period_id, split_period.split_period, od.app_period, od.*, od.service_type_id
- Join Filter: ((od.app_period * o.app_period) && split_period.split_period)
- Rows Removed by Join Filter: 970
- Buffers: shared hit=30650
- -> Nested Loop (cost=30.65..85791.13 rows=111 width=226) (actual time=0.308..283.564 rows=1005 loops=1)
- Output: o.*, o.id, o.service_level_id, o.app_period, o.period_id, split_period.split_period
- Buffers: shared hit=24861
- -> Index Scan using order_id_sys_period_app_period_excl on public.order_bt o (cost=0.40..497.05 rows=2780 width=194) (actual time=0.108..8.185 rows=2775 loops=1)
- Output: o.*, o.id, o.service_level_id, o.app_period, o.period_id
- Index Cond: (o.sys_period @> sys_time())
- Buffers: shared hit=807
- -> Subquery Scan on split_period (cost=30.25..30.67 rows=1 width=32) (actual time=0.097..0.098 rows=0 loops=2775)
- Output: split_period.split_period
- Filter: ((o.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange) && split_period.split_period)
- Rows Removed by Filter: 1
- Buffers: shared hit=24054
- -> Subquery Scan on x (cost=30.25..30.55 rows=8 width=32) (actual time=0.095..0.097 rows=1 loops=2775)
- Output: x.app_period
- Filter: (x.boundary IS NOT NULL)
- Rows Removed by Filter: 1
- Buffers: shared hit=24054
- CTE ranges
- -> HashAggregate (cost=29.69..29.73 rows=4 width=32) (actual time=0.082..0.082 rows=1 loops=2775)
- Output: ((o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Group Key: ((o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Buffers: shared hit=24054
- -> Append (cost=4.42..29.68 rows=4 width=32) (actual time=0.030..0.077 rows=4 loops=2775)
- Buffers: shared hit=24054
- -> Bitmap Heap Scan on public.order_bt o_1 (cost=4.42..12.00 rows=2 width=32) (actual time=0.027..0.027 rows=1 loops=2775)
- Output: (o_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange)
- Recheck Cond: ((o_1.id = $0) AND (o_1.sys_period @> sys_time()) AND (o_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Heap Blocks: exact=2775
- Buffers: shared hit=8442
- -> Bitmap Index Scan on order_id_sys_period_app_period_excl (cost=0.00..4.42 rows=2 width=0) (actual time=0.012..0.012 rows=1 loops=2775)
- Index Cond: ((o_1.id = $0) AND (o_1.sys_period @> sys_time()) AND (o_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange))
- Buffers: shared hit=5667
- -> Index Scan using order_detail_idx_order_id on public.order_detail_bt od_1 (cost=0.29..17.63 rows=2 width=32) (actual time=0.019..0.045 rows=3 loops=2775)
- Output: (od_1.app_period * '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange)
- Index Cond: (od_1.order_id = $0)
- Filter: ((od_1.app_period && '["2019-04-01 00:00:00+03","2019-05-01 00:00:00+03")'::tstzrange) AND (od_1.sys_period @> sys_time()))
- Rows Removed by Filter: 4
- Buffers: shared hit=15612
- -> WindowAgg (cost=0.52..0.74 rows=8 width=40) (actual time=0.094..0.096 rows=2 loops=2775)
- Output: tstzrange((lower(ranges.app_period)), lead((lower(ranges.app_period))) OVER (?)), lead((lower(ranges.app_period))) OVER (?)
- Buffers: shared hit=24054
- -> Sort (cost=0.52..0.54 rows=8 width=8) (actual time=0.091..0.091 rows=2 loops=2775)
- Output: (lower(ranges.app_period))
- Sort Key: (lower(ranges.app_period))
- Sort Method: quicksort Memory: 25kB
- Buffers: shared hit=24054
- -> HashAggregate (cost=0.32..0.40 rows=8 width=8) (actual time=0.087..0.087 rows=2 loops=2775)
- Output: (lower(ranges.app_period))
- Group Key: (lower(ranges.app_period))
- Buffers: shared hit=24054
- -> Append (cost=0.00..0.30 rows=8 width=8) (actual time=0.084..0.085 rows=2 loops=2775)
- Buffers: shared hit=24054
- -> CTE Scan on ranges (cost=0.00..0.09 rows=4 width=8) (actual time=0.083..0.084 rows=1 loops=2775)
- Output: lower(ranges.app_period)
- Buffers: shared hit=24054
- -> CTE Scan on ranges ranges_1 (cost=0.00..0.09 rows=4 width=8) (actual time=0.000..0.001 rows=1 loops=2775)
- Output: upper(ranges_1.app_period)
- -> Index Scan using order_detail_idx_order_id on public.order_detail_bt od (cost=0.29..1.59 rows=2 width=150) (actual time=0.021..0.073 rows=4 loops=1005)
- Output: od.app_period, od.*, od.order_id, od.service_type_id
- Index Cond: (od.order_id = o.id)
- Filter: (od.sys_period @> sys_time())
- Rows Removed by Filter: 4
- Buffers: shared hit=5789
- -> Function Scan on public.service_level_price sp (cost=0.25..12.75 rows=5 width=40) (actual time=0.556..0.587 rows=58 loops=2882)
- Output: sp.*, sp.service_type_id, sp.period_id
- Function Call: service_level_price(o.service_level_id)
- Filter: (o.period_id = sp.period_id)
- Rows Removed by Filter: 43
- Buffers: shared hit=11462
- -> Subquery Scan on ic (cost=1.07..9.23 rows=1 width=104) (actual time=0.083..0.085 rows=1 loops=2884)
- Output: ic.consumed, ic.consumed_period, ic.*
- Buffers: shared hit=5768
- -> Nested Loop Left Join (cost=1.07..9.22 rows=1 width=72) (actual time=0.081..0.083 rows=1 loops=2884)
- Output: ROW(cr.id, cr.rate, cr.docdate, cr.from_currency_id, cr.to_currency_id, cr.app_period)::currency_rate, interval_length(((od.app_period * o.app_period) * split_period.split_period), $3), COALESCE(((od.app_period * o.app_period) * split_period.split_period), ((od.app_period * o.app_period) * split_period.split_period))
- Buffers: shared hit=5768
- InitPlan 2 (returns $3)
- -> Seq Scan on public.period (cost=0.00..1.07 rows=1 width=16) (actual time=0.003..0.003 rows=1 loops=2884)
- Output: period."interval"
- Filter: (period.id = ($2).period_id)
- Rows Removed by Filter: 5
- Buffers: shared hit=2884
- -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.000..0.000 rows=1 loops=2884)
- -> Seq Scan on public.currency_rate_bt cr (cost=0.00..7.86 rows=1 width=56) (actual time=0.036..0.037 rows=1 loops=2884)
- Output: cr.id, cr.rate, cr.docdate, cr.from_currency_id, cr.to_currency_id, cr.sid, cr.app_period, cr.sys_period
- Filter: ((cr.from_currency_id = (sp.*).currency_id) AND (cr.app_period @> lower(((od.app_period * o.app_period) * split_period.split_period))) AND (cr.sys_period @> sys_time()) AND (cr.to_currency_id = app_currency()))
- Rows Removed by Filter: 12
- Buffers: shared hit=2884
- -> Function Scan on public.calc_item_suma cis (cost=0.25..10.25 rows=1000 width=16) (actual time=0.100..0.100 rows=1 loops=2884)
- Output: cis.item_qty, cis.item_price, cis.item_cost, cis.item_suma
- Function Call: calc_item_suma(o.*, od.*, sp.*, ic.*)
- Buffers: shared hit=2958
- Planning Time: 4.262 ms
- Execution Time: 2651.775 ms
- (134 rows)
- CREATE FUNCTION "order_cost_details" (in _target_range tstzrange default app_period())
- RETURNS table(
- item_qty double precision,
- item_price double precision,
- item_cost double precision,
- item_suma double precision,
- o order_bt, c order_detail_bt, p price, ic consume_info
- )
- LANGUAGE sql
- STABLE
- AS $$
- SELECT cis.*, o, od, sp, ic
- FROM "order_bt" o
- LEFT JOIN LATERAL order_item_intervals( o.id, _target_range ) split_period ON TRUE
- INNER JOIN order_detail_bt od ON od.order_id = o.id
- AND od.sys_period @> sys_time() AND (od.app_period * o.app_period) && split_period
- LEFT JOIN LATERAL service_level_price( o.service_level_id ) sp
- USING( service_type_id, period_id )
- LEFT JOIN LATERAL consume_info( sp, od.app_period * o.app_period * split_period ) ic ON true
- LEFT JOIN LATERAL calc_item_suma( o, od, sp, ic ) cis on TRUE
- WHERE o.sys_period @> sys_time()
- AND (o.app_period * _target_range)
- && split_period
- $$
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement