powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Оптимизация запроса
12 сообщений из 12, страница 1 из 1
Оптимизация запроса
    #39178834
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Есть VIEW :
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
WITH sl AS (
    SELECT so.id, pt.categ_id, so.partner_id, irch.material_id, sum(irch.material_qty) AS qty 
    FROM ((((swdo so 
        JOIN sol soi ON ((soi.order_id = so.id))) 
        JOIN product_product prd ON ((prd.id = soi.product_id))) 
        JOIN product_template pt ON((pt.id = prd.product_tmpl_id))) 
        JOIN rchmat irch ON ((irch.product_id = soi.product_id))) 
    GROUP BY so.id, so.partner_id,pt.categ_id, irch.material_id) 
SELECT min(lbl.id) AS id, COALESCE(sl.id, 0) AS order_id, CASE WHEN (sum(lbl.rsrv_qty1) > (0)::numeric) 
THEN sl.categ_id ELSE 0 END AS labcateg, lbl.material_id, (lbl.date_rchqty)::date AS date_qty, ((lbl.date_rchqty)::date + COALESCE(prd.life_time, 7)) AS date_exp,
sum(lbl.rsrv_qty) AS qty, sum(lbl.rsrv_qty1) AS labqty, sum(sl.qty) AS rqqty, COALESCE(prd.life_time, 7) AS life_time, 
(('now'::text)::date - (lbl.date_rchqty)::date) AS date_diff, (COALESCE(prd.life_time, 7) - (('now'::text)::date - (lbl.date_rchqty)::date)) AS left_days 
FROM (((lab lb 
    JOIN labmat lbl ON ((lb.id = lbl.laborder_id)))
    JOIN product_product prd ON ((lbl.material_id = prd.id))) 
    LEFT JOIN sl ON ((((sl.partner_id = lb.partner_id) AND (sl.material_id = lbl.material_id)) AND ((sl.categ_id = lb.categ_id) 
        OR (lbl.rsrv_qty > (0)::numeric))))) 
WHERE ((lbl.rsrv_qty1 > (0)::numeric) OR (lbl.rsrv_qty > (0)::numeric)) 
GROUP BY sl.categ_id, COALESCE(sl.id, 0), lbl.material_id, lbl.date_rchqty, prd.life_time;



Он выполняется 4 сек и возвращяет 300к строк

Есть запрос к VIEW
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
select so.id,
array_to_string(
array(
select id from my_view where order_id in (so.id,so.parent_id)
),','
)  as sel_id
from swdo so
where so.id in (9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997)



котрый выполняется 17 сек и возвращяет 15 строк . Индексы имеются на все JOIN и WHERE. В некоторых полях много дублированных данных и индекс выбтрает seq scan. Помогите оптимизировать подалуйста. Версия 9.2


explain analyze join table to view :
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
Index Scan using swdo_pkey on swdo so  (cost=0.02..2378195.61 rows=15 width=8) (actual time=2442.492..17248.868 rows=15
 loops=1)
   Index Cond: (id = ANY ('{9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997}'::integer[]))
   SubPlan 2
     ->  Subquery Scan on my_view  (cost=158527.30..158541.00 rows=261 width=4) (actual time=1149.888..1149.890 rows=1 loops
=15)
           ->  HashAggregate  (cost=158527.30..158538.39 rows=261 width=67) (actual time=1149.881..1149.882 rows=1 loops=15)
                 CTE sl
                   ->  GroupAggregate  (cost=73007.32..80251.10 rows=289751 width=21) (actual time=960.652..1243.156 rows=203841 loo
ps=1)
                         ->  Sort  (cost=73007.32..73731.70 rows=289751 width=21) (actual time=960.641..1055.999 rows=316803 loops=1
)
                               Sort Key: so.id, so.partner_id, pt.categ_id, irch.material_id
                               Sort Method: external merge  Disk: 9712kB
                               ->  Hash Join  (cost=8003.86..34834.45 rows=289751 width=21) (actual time=91.374..646.364 rows=316803
 loops=1)
                                     Hash Cond: (soi.order_id = so.id)
                                     ->  Hash Join  (cost=353.79..18202.74 rows=289751 width=17) (actual time=5.928..299.283 rows=31
6803 loops=1)
                                           Hash Cond: (soi.product_id = prd.id)
                                           ->  Seq Scan on sol soi  (cost=0.00..13271.96 rows=335896 width=8) (actual ti
me=0.015..90.387 rows=335896 loops=1)
                                           ->  Hash  (cost=330.38..330.38 rows=1873 width=21) (actual time=5.896..5.896 rows=1873 lo
ops=1)
                                                 Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                 ->  Hash Join  (cost=214.09..330.38 rows=1873 width=21) (actual time=3.713..5.332 r
ows=1873 loops=1)
                                                       Hash Cond: (pt.id = prd.product_tmpl_id)
                                                       ->  Seq Scan on product_template pt  (cost=0.00..86.71 rows=2171 width=8) (ac
tual time=0.006..0.563 rows=2171 loops=1)
                                                       ->  Hash  (cost=190.67..190.67 rows=1873 width=21) (actual time=3.699..3.699 
rows=1873 loops=1)
                                                             Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                             ->  Hash Join  (cost=123.85..190.67 rows=1873 width=21) (actual time=2.
016..3.217 rows=1873 loops=1)
                                                                   Hash Cond: (irch.product_id = prd.id)
                                                                   ->  Seq Scan on rchmat  irch  (cost=0.00..38.73 rows=187
3 width=13) (actual time=0.006..0.295 rows=1873 loops=1)
                                                                   ->  Hash  (cost=96.71..96.71 rows=2171 width=8) (actual time=2.00
1..2.001 rows=2171 loops=1)
                                                                         Buckets: 1024  Batches: 1  Memory Usage: 85kB
                                                                         ->  Seq Scan on product_product prd  (cost=0.00..96.71 rows
=2171 width=8) (actual time=0.010..1.488 rows=2171 loops=1)
                                     ->  Hash  (cost=5490.03..5490.03 rows=131603 width=8) (actual time=85.348..85.348 rows=131603 l
oops=1)
                                           Buckets: 4096  Batches: 8  Memory Usage: 655kB
                                           ->  Seq Scan on swdo so  (cost=0.00..5490.03 rows=131603 width=8) (actual time=0.02
3..51.313 rows=131603 loops=1)
                 ->  Nested Loop  (cost=75079.78..78269.68 rows=261 width=67) (actual time=1094.753..1149.864 rows=1 loops=15)
                       ->  Merge Left Join  (cost=75079.78..78193.19 rows=261 width=63) (actual time=1092.741..1147.850 rows=1 loops
=15)
                             Merge Cond: ((lbl.material_id = sl.material_id) AND (lb.partner_id = sl.partner_id))
                             Join Filter: ((sl.categ_id = lb.categ_id) OR (lbl.rsrv_qty > 0::numeric))
                             Rows Removed by Join Filter: 238
                             Filter: (COALESCE(sl.id, 0) = ANY (ARRAY[so.id, so.parent_id]))
                             Rows Removed by Filter: 95864
                             ->  Sort  (cost=25168.90..25234.02 rows=26051 width=31) (actual time=308.323..316.972 rows=26361 loops=
15)
                                   Sort Key: lbl.material_id, lb.partner_id
                                   Sort Method: external merge  Disk: 1104kB
                                   ->  Hash Join  (cost=9138.06..22631.68 rows=26051 width=31) (actual time=115.378..274.437 rows=26
361 loops=15)
                                         Hash Cond: (lbl.laborder_id = lb.id)
                                         ->  Seq Scan on labmat  lbl  (cost=0.00..11803.29 rows=26051 width=27) (actu
al time=0.012..114.543 rows=26361 loops=15)
                                               Filter: ((rsrv_qty1 > 0::numeric) OR (rsrv_qty > 0::numeric))
                                               Rows Removed by Filter: 329925
                                         ->  Hash  (cost=5902.36..5902.36 rows=186136 width=12) (actual time=115.275..115.275 rows=1
86136 loops=15)
                                               Buckets: 4096  Batches: 16  Memory Usage: 521kB
                                               ->  Seq Scan on inno_laborder lb  (cost=0.00..5902.36 rows=186136 width=12) (actual t
ime=0.017..68.331 rows=186136 loops=15)
                             ->  Materialize  (cost=49910.89..51359.64 rows=289751 width=48) (actual time=583.155..751.126 rows=2464
87 loops=15)
                                   ->  Sort  (cost=49910.89..50635.27 rows=289751 width=48) (actual time=583.151..711.633 rows=19981
4 loops=15)
                                         Sort Key: sl.material_id, sl.partner_id
                                         Sort Method: external merge  Disk: 6752kB
                                         ->  CTE Scan on sl  (cost=0.00..5795.02 rows=289751 width=48) (actual time=64.053..137.742 
rows=203841 loops=15)
                       ->  Index Scan using product_product_pkey on product_product prd  (cost=0.00..0.28 rows=1 width=8) (actual ti
me=0.012..0.013 rows=1 loops=9)
                             Index Cond: (id = lbl.material_id)
 Total runtime: 17255.919 ms
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39178847
Фотография vyegorov
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
NewBie77,

Покжаите `EXPLAIN (analyze, buffers)`. И вывод команды отформатируйте чтобы не было лишних переводов строк.
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39178928
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
vyegorov,

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
 Index Scan using swdo_order_pkey on swdo so  (cost=0.02..2378195.61 rows=15 width=8) (actual time=2591.836..17798.120 rows=15
 loops=1)
   Index Cond: (id = ANY ('{9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997}'::integer[]))
   Buffers: shared hit=45347 read=126484, temp read=52083 written=41434
   SubPlan 2
     ->  Subquery Scan on my_view  (cost=158527.30..158541.00 rows=261 width=4) (actual time=1186.505..1186.506 rows=1 loops
=15)
           Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
           ->  HashAggregate  (cost=158527.30..158538.39 rows=261 width=67) (actual time=1186.498..1186.499 rows=1 loops=15)
                 Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
                 CTE sl
                   ->  GroupAggregate  (cost=73007.32..80251.10 rows=289751 width=21) (actual time=1021.418..1327.415 rows=203841 lo
ops=1)
                         Buffers: shared hit=503 read=13744, temp read=3491 written=3477
                         ->  Sort  (cost=73007.32..73731.70 rows=289751 width=21) (actual time=1021.406..1122.863 rows=316803 loops=
1)
                               Sort Key: so.id, so.partner_id, pt.categ_id, irch.material_id
                               Sort Method: external merge  Disk: 9712kB
                               Buffers: shared hit=503 read=13744, temp read=3491 written=3477
                               ->  Hash Join  (cost=8003.86..34834.45 rows=289751 width=21) (actual time=101.055..694.974 rows=31680
3 loops=1)
                                     Hash Cond: (soi.order_id = so.id)
                                     Buffers: shared hit=503 read=13744, temp read=1666 written=1652
                                     ->  Hash Join  (cost=353.79..18202.74 rows=289751 width=17) (actual time=6.128..320.858 rows=31
6803 loops=1)
                                           Hash Cond: (soi.product_id = prd.id)
                                           Buffers: shared hit=401 read=9672
                                           ->  Seq Scan on sol soi  (cost=0.00..13271.96 rows=335896 width=8) (actual ti
me=0.016..96.802 rows=335896 loops=1)
                                                 Buffers: shared hit=241 read=9672
                                           ->  Hash  (cost=330.38..330.38 rows=1873 width=21) (actual time=6.094..6.094 rows=1873 lo
ops=1)
                                                 Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                 Buffers: shared hit=160
                                                 ->  Hash Join  (cost=214.09..330.38 rows=1873 width=21) (actual time=3.417..5.399 r
ows=1873 loops=1)
                                                       Hash Cond: (pt.id = prd.product_tmpl_id)
                                                       Buffers: shared hit=160
                                                       ->  Seq Scan on product_template pt  (cost=0.00..86.71 rows=2171 width=8) (ac
tual time=0.007..0.696 rows=2171 loops=1)
                                                             Buffers: shared hit=65
                                                       ->  Hash  (cost=190.67..190.67 rows=1873 width=21) (actual time=3.402..3.402 
rows=1873 loops=1)
                                                             Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                             Buffers: shared hit=95
                                                             ->  Hash Join  (cost=123.85..190.67 rows=1873 width=21) (actual time=1.
477..2.790 rows=1873 loops=1)
                                                                   Hash Cond: (irch.product_id = prd.id)
                                                                   Buffers: shared hit=95
                                                                   ->  Seq Scan on rchmat irch  (cost=0.00..38.73 rows=187
3 width=13) (actual time=0.006..0.330 rows=1873 loops=1)
                                                                         Buffers: shared hit=20
                                                                   ->  Hash  (cost=96.71..96.71 rows=2171 width=8) (actual time=1.46
1..1.461 rows=2171 loops=1)
                                                                         Buckets: 1024  Batches: 1  Memory Usage: 85kB
                                                                         Buffers: shared hit=75
                                                                         ->  Seq Scan on product_product prd  (cost=0.00..96.71 rows
=2171 width=8) (actual time=0.006..0.969 rows=2171 loops=1)
                                                                               Buffers: shared hit=75
                                     ->  Hash  (cost=5490.03..5490.03 rows=131603 width=8) (actual time=94.827..94.827 rows=131603 l
oops=1)
                                           Buckets: 4096  Batches: 8  Memory Usage: 655kB
                                           Buffers: shared hit=102 read=4072, temp written=388
                                           ->  Seq Scan on sale_order so  (cost=0.00..5490.03 rows=131603 width=8) (actual time=0.02
8..55.813 rows=131603 loops=1)
                                                 Buffers: shared hit=102 read=4072
                 ->  Nested Loop  (cost=75079.78..78269.68 rows=261 width=67) (actual time=1128.282..1186.480 rows=1 loops=15)
                       Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
                       ->  Merge Left Join  (cost=75079.78..78193.19 rows=261 width=63) (actual time=1126.246..1184.443 rows=1 loops
=15)
                             Merge Cond: ((lbl.material_id = sl.material_id) AND (lb.partner_id = sl.partner_id))
                             Join Filter: ((sl.categ_id = lb.categ_id) OR (lbl.rsrv_qty > 0::numeric))
                             Rows Removed by Join Filter: 238
                             Filter: (COALESCE(sl.id, 0) = ANY (ARRAY[so.id, so.parent_id]))
                             Rows Removed by Filter: 95864
                             Buffers: shared hit=45263 read=126484, temp read=52083 written=41434
                             ->  Sort  (cost=25168.90..25234.02 rows=26051 width=31) (actual time=319.351..328.096 rows=26361 loops=
15)
                                   Sort Key: lbl.material_id, lb.partner_id
                                   Sort Method: external merge  Disk: 1104kB
                                   Buffers: shared hit=44760 read=112740, temp read=15315 written=14865
                                   ->  Hash Join  (cost=9138.06..22631.68 rows=26051 width=31) (actual time=121.886..284.656 rows=26
361 loops=15)
                                         Hash Cond: (lbl.laborder_id = lb.id)
                                         Buffers: shared hit=44760 read=112740, temp read=13215 written=12765
                                         ->  Seq Scan on labmat lbl  (cost=0.00..11803.29 rows=26051 width=27) (actu
al time=0.012..116.462 rows=26361 loops=15)
                                               Filter: ((rsrv_qty1 > 0::numeric) OR (rsrv_qty > 0::numeric))
                                               Rows Removed by Filter: 329925
                                               Buffers: shared hit=23985 read=72900
                                         ->  Hash  (cost=5902.36..5902.36 rows=186136 width=12) (actual time=121.768..121.768 rows=1
86136 loops=15)
                                               Buckets: 4096  Batches: 16  Memory Usage: 521kB
                                               Buffers: shared hit=20775 read=39840, temp written=10095
                                               ->  Seq Scan on lab lb  (cost=0.00..5902.36 rows=186136 width=12) (actual t
ime=0.026..70.470 rows=186136 loops=15)
                                                     Buffers: shared hit=20775 read=39840
                             ->  Materialize  (cost=49910.89..51359.64 rows=289751 width=48) (actual time=598.318..771.208 rows=2464
87 loops=15)
                                   Buffers: shared hit=5030 read=13744, temp read=36768 written=26569
                                   ->  Sort  (cost=49910.89..50635.27 rows=289751 width=48) (actual time=598.313..728.367 rows=19981
4 loops=15)
                                         Sort Key: sl.material_id, sl.partner_id
                                         Sort Method: external merge  Disk: 6752kB
                                         Buffers: shared hit=503 read=13744, temp read=36768 written=26569
                                         ->  CTE Scan on sl  (cost=0.00..5795.02 rows=289751 width=48) (actual time=68.103..146.808 
rows=203841 loops=15)
                                               Buffers: shared hit=503 read=13744, temp read=14523 written=4264
                       ->  Index Scan using product_product_pkey on product_product prd  (cost=0.00..0.28 rows=1 width=8) (actual ti
me=0.012..0.012 rows=1 loops=9)
                             Index Cond: (id = lbl.material_id)
                             Buffers: shared hit=27
 Total runtime: 17805.255 ms
(86 rows)
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39178979
Фотография vyegorov
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
NewBie77,

Просил отформатировать...
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
 Index Scan using swdo_order_pkey on swdo so  (cost=0.02..2378195.61 rows=15 width=8) (actual time=2591.836..17798.120 rows=15 loops=1)
   Index Cond: (id = ANY ('{9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997}'::integer[]))
   Buffers: shared hit=45347 read=126484, temp read=52083 written=41434
   SubPlan 2
     ->  Subquery Scan on my_view  (cost=158527.30..158541.00 rows=261 width=4) (actual time=1186.505..1186.506 rows=1 loops=15)
           Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
           ->  HashAggregate  (cost=158527.30..158538.39 rows=261 width=67) (actual time=1186.498..1186.499 rows=1 loops=15)
                 Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
                 CTE sl
                   ->  GroupAggregate  (cost=73007.32..80251.10 rows=289751 width=21) (actual time=1021.418..1327.415 rows=203841 loops=1)
                         Buffers: shared hit=503 read=13744, temp read=3491 written=3477
                         ->  Sort  (cost=73007.32..73731.70 rows=289751 width=21) (actual time=1021.406..1122.863 rows=316803 loops=1)
                               Sort Key: so.id, so.partner_id, pt.categ_id, irch.material_id
                               Sort Method: external merge  Disk: 9712kB
                               Buffers: shared hit=503 read=13744, temp read=3491 written=3477
                               ->  Hash Join  (cost=8003.86..34834.45 rows=289751 width=21) (actual time=101.055..694.974 rows=316803 loops=1)
                                     Hash Cond: (soi.order_id = so.id)
                                     Buffers: shared hit=503 read=13744, temp read=1666 written=1652
                                     ->  Hash Join  (cost=353.79..18202.74 rows=289751 width=17) (actual time=6.128..320.858 rows=316803 loops=1)
                                           Hash Cond: (soi.product_id = prd.id)
                                           Buffers: shared hit=401 read=9672
                                           ->  Seq Scan on sol soi  (cost=0.00..13271.96 rows=335896 width=8) (actual time=0.016..96.802 rows=335896 loops=1)
                                                 Buffers: shared hit=241 read=9672
                                           ->  Hash  (cost=330.38..330.38 rows=1873 width=21) (actual time=6.094..6.094 rows=1873 loops=1)
                                                 Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                 Buffers: shared hit=160
                                                 ->  Hash Join  (cost=214.09..330.38 rows=1873 width=21) (actual time=3.417..5.399 rows=1873 loops=1)
                                                       Hash Cond: (pt.id = prd.product_tmpl_id)
                                                       Buffers: shared hit=160
                                                       ->  Seq Scan on product_template pt  (cost=0.00..86.71 rows=2171 width=8) (actual time=0.007..0.696 rows=2171 loops=1)
                                                             Buffers: shared hit=65
                                                       ->  Hash  (cost=190.67..190.67 rows=1873 width=21) (actual time=3.402..3.402 rows=1873 loops=1)
                                                             Buckets: 1024  Batches: 1  Memory Usage: 103kB
                                                             Buffers: shared hit=95
                                                             ->  Hash Join  (cost=123.85..190.67 rows=1873 width=21) (actual time=1.477..2.790 rows=1873 loops=1)
                                                                   Hash Cond: (irch.product_id = prd.id)
                                                                   Buffers: shared hit=95
                                                                   ->  Seq Scan on rchmat irch  (cost=0.00..38.73 rows=1873 width=13) (actual time=0.006..0.330 rows=1873 loops=1)
                                                                         Buffers: shared hit=20
                                                                   ->  Hash  (cost=96.71..96.71 rows=2171 width=8) (actual time=1.461..1.461 rows=2171 loops=1)
                                                                         Buckets: 1024  Batches: 1  Memory Usage: 85kB
                                                                         Buffers: shared hit=75
                                                                         ->  Seq Scan on product_product prd  (cost=0.00..96.71 rows=2171 width=8) (actual time=0.006..0.969 rows=2171 loops=1)
                                                                               Buffers: shared hit=75
                                     ->  Hash  (cost=5490.03..5490.03 rows=131603 width=8) (actual time=94.827..94.827 rows=131603 loops=1)
                                           Buckets: 4096  Batches: 8  Memory Usage: 655kB
                                           Buffers: shared hit=102 read=4072, temp written=388
                                           ->  Seq Scan on sale_order so  (cost=0.00..5490.03 rows=131603 width=8) (actual time=0.028..55.813 rows=131603 loops=1)
                                                 Buffers: shared hit=102 read=4072
                 ->  Nested Loop  (cost=75079.78..78269.68 rows=261 width=67) (actual time=1128.282..1186.480 rows=1 loops=15)
                       Buffers: shared hit=45290 read=126484, temp read=52083 written=41434
                       ->  Merge Left Join  (cost=75079.78..78193.19 rows=261 width=63) (actual time=1126.246..1184.443 rows=1 loops=15)
                             Merge Cond: ((lbl.material_id = sl.material_id) AND (lb.partner_id = sl.partner_id))
                             Join Filter: ((sl.categ_id = lb.categ_id) OR (lbl.rsrv_qty > 0::numeric))
                             Rows Removed by Join Filter: 238
                             Filter: (COALESCE(sl.id, 0) = ANY (ARRAY[so.id, so.parent_id]))
                             Rows Removed by Filter: 95864
                             Buffers: shared hit=45263 read=126484, temp read=52083 written=41434
                             ->  Sort  (cost=25168.90..25234.02 rows=26051 width=31) (actual time=319.351..328.096 rows=26361 loops=15)
                                   Sort Key: lbl.material_id, lb.partner_id
                                   Sort Method: external merge  Disk: 1104kB
                                   Buffers: shared hit=44760 read=112740, temp read=15315 written=14865
                                   ->  Hash Join  (cost=9138.06..22631.68 rows=26051 width=31) (actual time=121.886..284.656 rows=26361 loops=15)
                                         Hash Cond: (lbl.laborder_id = lb.id)
                                         Buffers: shared hit=44760 read=112740, temp read=13215 written=12765
                                         ->  Seq Scan on labmat lbl  (cost=0.00..11803.29 rows=26051 width=27) (actual time=0.012..116.462 rows=26361 loops=15)
                                               Filter: ((rsrv_qty1 > 0::numeric) OR (rsrv_qty > 0::numeric))
                                               Rows Removed by Filter: 329925
                                               Buffers: shared hit=23985 read=72900
                                         ->  Hash  (cost=5902.36..5902.36 rows=186136 width=12) (actual time=121.768..121.768 rows=186136 loops=15)
                                               Buckets: 4096  Batches: 16  Memory Usage: 521kB
                                               Buffers: shared hit=20775 read=39840, temp written=10095
                                               ->  Seq Scan on lab lb  (cost=0.00..5902.36 rows=186136 width=12) (actual time=0.026..70.470 rows=186136 loops=15)
                                                     Buffers: shared hit=20775 read=39840
                             ->  Materialize  (cost=49910.89..51359.64 rows=289751 width=48) (actual time=598.318..771.208 rows=246487 loops=15)
                                   Buffers: shared hit=5030 read=13744, temp read=36768 written=26569
                                   ->  Sort  (cost=49910.89..50635.27 rows=289751 width=48) (actual time=598.313..728.367 rows=199814 loops=15)
                                         Sort Key: sl.material_id, sl.partner_id
                                         Sort Method: external merge  Disk: 6752kB
                                         Buffers: shared hit=503 read=13744, temp read=36768 written=26569
                                         ->  CTE Scan on sl  (cost=0.00..5795.02 rows=289751 width=48) (actual time=68.103..146.808 rows=203841 loops=15)
                                               Buffers: shared hit=503 read=13744, temp read=14523 written=4264
                       ->  Index Scan using product_product_pkey on product_product prd  (cost=0.00..0.28 rows=1 width=8) (actual time=0.012..0.012 rows=1 loops=9)
                             Index Cond: (id = lbl.material_id)
                             Buffers: shared hit=27
 Total runtime: 17805.255 ms
(86 rows)


1. У вас CTE внутри вьюшки, оно не позволяет оптимизатору пропихнуть ваш пердикат. Надо переписать с подзапросом
2. `work_mem` маленький, но если сделать #1, то может и поднимать не нужн обудет
3. на `labmat` индекс просится, т.к. `lbl.rsrv_qty > 0 OR lbl.rsrv_qty1 > 0` фильтрует 90% таблицы.
4. зачем столько скобок?
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39178993
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
vyegorov,
за план запроса извиняюсь не заметил. индекс поставил на `lbl.rsrv_qty`.
Запрос переписал :
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
SELECT min(lbl.id) AS id, COALESCE(sl.id, 0) AS order_id, CASE WHEN (sum(lbl.rsrv_qty1) > (0)::numeric) 
THEN sl.categ_id ELSE 0 END AS labcateg, lbl.material_id, (lbl.date_rchqty)::date AS date_qty, ((lbl.date_rchqty)::date + COALESCE(prd.life_time, 7)) AS date_exp,
sum(lbl.rsrv_qty) AS qty, sum(lbl.rsrv_qty1) AS labqty, sum(sl.qty) AS rqqty, COALESCE(prd.life_time, 7) AS life_time, 
(('now'::text)::date - (lbl.date_rchqty)::date) AS date_diff, (COALESCE(prd.life_time, 7) - (('now'::text)::date - (lbl.date_rchqty)::date)) AS left_days 
FROM (((lab lb 
    JOIN labmat lbl ON ((lb.id = lbl.laborder_id)))
    JOIN product_product prd ON ((lbl.material_id = prd.id))) 
    LEFT JOIN (SELECT so.id, pt.categ_id, so.partner_id, irch.material_id, sum(irch.material_qty) AS qty 
    FROM ((((swdo so 
        JOIN sol soi ON ((soi.order_id = so.id))) 
        JOIN product_product prd ON ((prd.id = soi.product_id))) 
        JOIN product_template pt ON((pt.id = prd.product_tmpl_id))) 
        JOIN rchmat irch ON ((irch.product_id = soi.product_id))) 
    GROUP BY so.id, so.partner_id,pt.categ_id, irch.material_id) sl ON ((((sl.partner_id = lb.partner_id) AND (sl.material_id = lbl.material_id)) AND ((sl.categ_id = lb.categ_id) 
        OR (lbl.rsrv_qty > (0)::numeric))))) 
WHERE ((lbl.rsrv_qty1 > (0)::numeric) OR (lbl.rsrv_qty > (0)::numeric)) 
GROUP BY sl.categ_id, COALESCE(sl.id, 0), lbl.material_id, lbl.date_rchqty, prd.life_time;



Но время выполнения тоже самре
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179000
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Насчет скобок запрос писал не я ,а программист мне просто сказали что работает медленно и нужно оптимизировать
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179019
Фотография vyegorov
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
NewBie77,

А нужно оптимизировать запрос который внутри представления? Или же запрос, который использует это представление?
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179030
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
vyegorov,

Запрос который использует представление.
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
select so.id,
array_to_string(
array(
select id from my_view where order_id in (so.id,so.parent_id)
),','
)  as sel_id
from swdo so
where so.id in (9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997)
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179042
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Например этот запрoс выполняется за 1 секунду :

Код: sql
1.
select id from my_view where order_id in (9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997)



Вроде представление работает нормально а вот этот запрс
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
select so.id,
array_to_string(
array(
select id from my_view where order_id in (so.id,so.parent_id)
),','
)  as sel_id
from swdo so
where so.id in (9987,99984,99985,99986,99987,99988,99989,99990,99991,99992,99993,99994,99995,99996,99997)


выполняется 17 секнд
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179146
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
NewBie77,

Если вывести запрос с представления на ружу то выполняется быстро
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179161
NewBie77
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
vyegorov,

Огромное спасибо ваш совет с переписанием запроса мне помог
...
Рейтинг: 0 / 0
Оптимизация запроса
    #39179170
Lonepsycho
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
NewBie77,

а переписать с INNER JOIN вместо IN (...) непробовали?
...
Рейтинг: 0 / 0
12 сообщений из 12, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Оптимизация запроса
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]