Have view with query :
SELECT min(lbl.id) AS id, lb.order_id, lb.par_id, lbl.lb_id, lb.categ_id, lbl.mt_id, lbl.uom_id, lbl.tracking_id,
sum(lbl.orig_qty) AS orig_qty, max(lbl.qty) AS qty, sum(lbl.rch_qty) AS rch_qty, sum(lbl.rsrv_qty) AS rsrv_qty,
min(lbl.date_qty) AS date_qty, min(lbl.date_rchqty) AS date_rchqty, max(lbl.sequence) AS sequence, max((lbl.state)::text) AS state,
max(lbl.comment) AS comment, array_to_string(array_agg(lbl.id), ','::text) AS mat_ids
FROM tbl1 lb
JOIN tbl2 lbl ON lb.id = lbl.laborder_id
JOIN tbl2 prd ON lbl.mt_id = prd.id
GROUP BY lb.order_id, lb.par_id, lbl.lb_id, lb.categ_id, lbl.mt_id, lbl.uom_id, lbl.tracking_id;
Total runtime of the query is 2658.875 ms
Explain anayze plan when work_mem = 1 mb is :
GroupAggregate (cost=105065.17..127333.04 rows=356286 width=85) (actual time=1376.793..2636.413 rows=202404 loops=1)
-> Sort (cost=105065.17..105955.88 rows=356286 width=85) (actual time=1376.753..1491.741 rows=356286 loops=1)
Sort Key: lb.order_id, lb.par_id, lbl.lb_id, lb.categ_id, lbl.mt_id, lbl.uom_id, lbl.tracking_id
Sort Method: external merge Disk: 27336kB
-> Hash Join (cost=92.77..38106.83 rows=356286 width=85) (actual time=1.498..826.742 rows=356286 loops=1)
Hash Cond: (lbl.mt_id = prd.id)
-> Merge Join (cost=0.81..32670.59 rows=356286 width=85) (actual time=0.035..671.049 rows=356286 loops=1)
Merge Cond: (lb.id = lbl.lorder_id)
-> Index Scan using tbl1_pkey on tbl1 lb (cost=0.00..10545.07 rows=186136 width=16) (actualtime=0.015..105.777 rows=186136 loops=1)
-> Index Scan using tbl2_lorder_id_index on tbl2 lbl (cost=0.00..17206.72 rows=356286 width=77) (actual time=0.009..219.785 rows=356286 loops=1)
-> Hash (cost=64.82..64.82 rows=2171 width=4) (actual time=1.441..1.441 rows=2171 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 77kB
-> Index Only Scan using tbl2_pkey on tbl2 prd (cost=0.00..64.82 rows=2171 width=4) (actual time=0.089..0.659 rows=2171 loops=1)
Heap Fetches: 0
Total runtime: 2658.875 ms
(15 rows)
When work_mem = 200 mb
GroupAggregate (cost=63667.22..85935.09 rows=356286 width=85) (actual time=1057.591..2247.091 rows=202404 loops=1)
-> Sort (cost=63667.22..64557.93 rows=356286 width=85) (actual time=1057.553..1105.126 rows=356286 loops=1)
Sort Key: lb.order_id, lb.par_id, lbl.lb_id, lb.categ_id, lbl.mt_id, lbl.uom_id, lbl.tracking_id
Sort Method: quicksort Memory: 62432kB
-> Hash Join (cost=8321.01..30812.88 rows=356286 width=85) (actual time=122.309..673.887 rows=356286 loops=1)
Hash Cond: (lbl.mt_id = prd.id)
-> Hash Join (cost=8229.06..25376.64 rows=356286 width=85) (actual time=121.224..534.207 rows=356286 loops=1)
Hash Cond: (lbl.lorder_id = lb.id)
-> Seq Scan on tbl2 lbl (cost=0.00..10021.86 rows=356286 width=77) (actual time=0.003..70.330 rows=356286 loops=1)
-> Hash (cost=5902.36..5902.36 rows=186136 width=16) (actual time=121.065..121.065 rows=186136 loops=1)
Buckets: 32768 Batches: 1 Memory Usage: 8725kB
-> Seq Scan on tbl1 lb (cost=0.00..5902.36 rows=186136 width=16) (actual time=0.039..71.138 rows=186136 loops=1)
-> Hash (cost=64.82..64.82 rows=2171 width=4) (actual time=1.075..1.075 rows=2171 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 77kB
-> Index Only Scan using tbl2_pkey on tbl2 prd (cost=0.00..64.82 rows=2171 width=4) (actual time=0.021..0.537 rows=2171 loops=1)
Heap Fetches: 0
Total runtime: 2268.394 ms
(17 rows)
Have query which select data from this view and runtime is also 2658.875 ms
SELECT view1."rsrv_qty",view1."comment",view1."rch_qty",view1."sequence",view1."order_id",
view1."qty",view1."uom_id",view1."categ_id",view1."mat_ids",view1."date_rchqty",
view1."par_id",view1."mt_id",view1."state",view1."orig_qty",view1."tracking_id",
view1."lbl_id",view1."date_qty",view1.id
FROM "view1"
WHERE view1.id IN (271053, 271054)
ORDER BY id
Is there any way to optimize it ?