проанализировать план: QUERY PLAN…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
проанализировать план: |QUERY PLAN | |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |Nested Loop (cost=0.00..69396485.43 rows=27792 width=610) (actual time=79647.670..79647.685 rows=0 loops=1) | | Join Filter: (t.talon_id = t_1.talon_id) | | Rows Removed by Join Filter: 1486516 | | -> Append (cost=0.00..69285315.26 rows=2779192 width=820) (actual time=0.973..79231.311 rows=1486516 loops=1) | | -> Seq Scan on eq_talon t_1 (cost=0.00..36107269.60 rows=1453181 width=2116) (actual time=0.972..59257.558 rows=1453181 loops=1) | | Filter: (talon_type_id = 1) | | SubPlan 1 | | -> Seq Scan on tdic_eq_status t_4 (cost=0.00..1.06 rows=1 width=20) (actual time=0.001..0.001 rows=1 loops=1453181) | | Filter: (eq_status_id = t_1.status) | | Rows Removed by Filter: 4 | | SubPlan 2 | | -> Seq Scan on tdict_eq_equeue_type tt (cost=0.00..1.32 rows=1 width=39) (actual time=0.001..0.002 rows=1 loops=1453181) | | Filter: (equeue_type_id = t_1.equeue_type_id) | | Rows Removed by Filter: 25 | | SubPlan 3 | | -> Index Scan using case_history_pkey on case_history ch (cost=0.43..3.04 rows=1 width=11) (actual time=0.002..0.002 rows=0 loops=1453181) | | Index Cond: (folder = t_1.case_history) | | SubPlan 4 | | -> Aggregate (cost=16.39..16.40 rows=1 width=8) (actual time=0.031..0.031 rows=1 loops=1453181) | | -> Index Scan using ix_oc_case_hist on order_control oc (cost=0.43..16.39 rows=1 width=8) (actual time=0.031..0.031 rows=0 loops=1453181) | | Index Cond: (case_history_id = t_1.case_history) | | Filter: ((status = 8) AND (date_trunc('day'::text, time_start_plan) = date_trunc('day'::text, (CURRENT_DATE)::timestamp with time zone))) | | Rows Removed by Filter: 8 | | SubPlan 5 | | -> Index Scan using staff_pkey on staff f (cost=0.28..2.90 rows=1 width=23) (actual time=0.001..0.001 rows=1 loops=1453181) | | Index Cond: (staff = t_1.staff) | | -> Subquery Scan on "SELECT 2_1" (cost=454.33..33164149.70 rows=1326011 width=664) (actual time=42.486..19817.724 rows=33335 loops=1) | | -> Merge Join (cost=454.33..33150889.59 rows=1326011 width=664) (actual time=42.483..19804.377 rows=33335 loops=1) | | Merge Cond: (t_2.talon_id = a.talon_id) | | Join Filter: (a.eq_type_id <> t_2.equeue_type_id) | | Rows Removed by Join Filter: 1433775 | | -> Index Scan using eq_talon_pkey on eq_talon t_2 (cost=0.43..56685.55 rows=1453181 width=94) (actual time=0.033..1044.512 rows=1453181 loops=1) | | Filter: (talon_type_id = 1) | | -> Index Scan using ix_talon_action on eq_talon_action a (cost=0.43..164924.84 rows=1456080 width=24) (actual time=0.396..16346.701 rows=1467110 loops=1)| | Index Cond: (action_id = ANY ('{0,4}'::bigint[])) | | SubPlan 6 | | -> Seq Scan on tdic_eq_status t_5 (cost=0.00..1.06 rows=1 width=20) (actual time=0.001..0.002 rows=1 loops=33335) | | Filter: (eq_status_id = t_2.status) | | Rows Removed by Filter: 4 | | SubPlan 7 | | -> Seq Scan on tdict_eq_equeue_type tt_1 (cost=0.00..1.32 rows=1 width=39) (actual time=0.001..0.002 rows=1 loops=33335) | | Filter: (equeue_type_id = a.eq_type_id) | | Rows Removed by Filter: 25 | | SubPlan 8 | | -> Index Scan using case_history_pkey on case_history ch_1 (cost=0.43..3.04 rows=1 width=11) (actual time=0.005..0.006 rows=1 loops=33335) | | Index Cond: (folder = t_2.case_history) | | SubPlan 9 | | -> Aggregate (cost=16.39..16.40 rows=1 width=8) (actual time=0.037..0.037 rows=1 loops=33335) | | -> Index Scan using ix_oc_case_hist on order_control oc_1 (cost=0.43..16.39 rows=1 width=8) (actual time=0.036..0.036 rows=0 loops=33335) | | Index Cond: (case_history_id = t_2.case_history) | | Filter: ((status = 8) AND (date_trunc('day'::text, time_start_plan) = date_trunc('day'::text, (CURRENT_DATE)::timestamp with time zone))) | | Rows Removed by Filter: 16 | | SubPlan 10 | | -> Index Scan using staff_pkey on staff f_1 (cost=0.28..2.90 rows=1 width=23) (actual time=0.002..0.003 rows=1 loops=33335) | | Index Cond: (staff = t_2.staff) | | -> Materialize (cost=0.00..2.49 rows=2 width=8) (actual time=0.000..0.000 rows=1 loops=1486516) | | -> Append (cost=0.00..2.48 rows=2 width=8) (actual time=0.422..0.464 rows=1 loops=1) | | -> Seq Scan on eq_window t (cost=0.00..1.23 rows=1 width=8) (actual time=0.411..0.412 rows=1 loops=1) | | Filter: (window_id = 10) | | Rows Removed by Filter: 17 | | -> Subquery Scan on "SELECT 2" (cost=0.00..1.25 rows=1 width=8) (actual time=0.038..0.040 rows=0 loops=1) | | -> Nested Loop (cost=0.00..1.24 rows=1 width=1588) (actual time=0.037..0.039 rows=0 loops=1) | | -> Seq Scan on eq_window t_3 (cost=0.00..1.23 rows=1 width=16) (actual time=0.019..0.019 rows=1 loops=1) | | Filter: (window_id = 10) | | Rows Removed by Filter: 11 | | -> Seq Scan on eq_window_add a_1 (cost=0.00..0.00 rows=1 width=8) (actual time=0.017..0.018 rows=0 loops=1) | | Filter: (window_id = 10) | |Planning Time: 18.216 ms | |Execution Time: 79648.148 ms |
Классический пример медленного запроса в PostgreSQL! Давайте разберем план запроса и найдем узкие места.
Общее впечатление
План запроса
План состоит из нескольких частей:
Append (слева) и Materialize (справа).t.talon_id = t_1.talon_id (Join Filter)Seq Scan on eq_talon t_1 и Subquery Scan on "*SELECT* 2_1"Append с двумя Seq Scan)Проблемные места
eq_talon (1 453 181 строка, rows=1453181)!talon_type_id = 1 - видимо, не очень селективный (мало фильтрует строки)t_1 - это дополнительные запросы к таблицам:
tdic_eq_status (1.06 стоимость, 0.001 время)tdict_eq_equeue_type (1.32 стоимость, 0.001 время)case_history (3.04 стоимость, 0.002 время) - индексное чтениеorder_control (16.39 стоимость, 0.031 время) - индексное чтение с фильтромstaff (2.90 стоимость, 0.001 время) - индексное чтениеeq_talon t_2 и eq_talon_action a по talon_idПричины тормозов
eq_talon(talon_type_id) - нет индекса, поэтому Seq Scan.eq_talon(talon_id) или eq_talon_action(talon_id).eq_talon (1 453 181 раз!).talon_type_id = 1 фильтрует недостаточно строк.order_control (status = 8 AND ...) тоже не очень эффективен.Рекомендации
CREATE INDEX idx_eq_talon_talon_type_id ON eq_talon(talon_type_id);CREATE INDEX idx_eq_talon_talon_id ON eq_talon(talon_id);CREATE INDEX idx_eq_talon_action_talon_id ON eq_talon_action(talon_id);Seq Scan на Index Scan или Index Only Scan.ANALYZE eq_talon; и ANALYZE eq_talon_action;, чтобы убедиться, что статистики актуальны.eq_talon(talon_type_id), возможно, данные не равномерно распределены.Пример переписывания запроса (псевдокод)
WITH
filtered_talon AS (
SELECT * FROM eq_talon
WHERE talon_type_id = 1
AND EXISTS (SELECT 1 FROM tdic_eq_status WHERE eq_status_id = status)
AND EXISTS (SELECT 1 FROM tdict_eq_equeue_type WHERE equeue_type_id = equeue_type_id)
),
joined_actions AS (
SELECT t2.talon_id, a.eq_type_id
FROM filtered_talon t2
JOIN eq_talon_action a ON t2.talon_id = a.talon_id AND a.action_id IN (0, 4)
WHERE a.eq_type_id <> t2.equeue_type_id
)
SELECT *
FROM filtered_talon t1
JOIN joined_actions ja ON t1.talon_id = ja.talon_id
WHERE EXISTS (SELECT 1 FROM eq_window WHERE window_id = 10 AND talon_id = t1.talon_id);
Такой подход:
После внесения изменений не забудьте:
ANALYZE затронутые таблицы.EXPLAIN (ANALYZE)).Если будут вопросы по переписыванию запроса или созданию индексов - пишите!