PostgreSQL медленный запрос - PullRequest
0 голосов
/ 28 мая 2018

Следующий SQL-запрос выполнялся за несколько секунд, но внезапно начал выполняться долго.Я уже сделал несколько улучшений для более старого запроса, но все еще думаю, что его можно ускорить:

SELECT COUNT(*) AS count_all, flows.user_id AS flows_user_id, events.metadata->? AS events_metadata_event_kind FROM "events" 
INNER JOIN "flow_recipients" ON "events"."eventable_id" = "flow_recipients"."id" 
INNER JOIN "flows" ON "flow_recipients"."flow_id" = "flows"."id" 
INNER JOIN users ON flows.user_id = users.id 
WHERE "events"."deleted_at" IS ? 
AND "flow_recipients"."deleted_at" IS ? 
AND "flows"."deleted_at" IS ? 
AND "flows"."company_id" = $? 
AND "events"."eventable_type" = $? 
AND "users"."deleted_by_user_at" IS ? 
AND ("events"."created_at" BETWEEN $? AND $?) 
AND "events"."type" = $? 
AND (events.metadata->>? IN (?,?,?)) 
GROUP BY flows.user_id, events.metadata->?

Вот результат EXPLAIN для этого запроса:

1   Query plan  GroupAggregate (cost=7763.68..7763.68 rows=1 width=44)
2   Query plan  Group Key: ?
3   Query plan  -> Sort (cost=7763.68..7763.68 rows=1 width=36)
4   Query plan  Sort Key: ?
5   Query plan  -> Nested Loop (cost=0.28..7763.67 rows=1 width=36)
6   Query plan  -> Nested Loop (cost=0.23..7762.90 rows=1 width=65)
7   Query plan  -> Nested Loop (cost=0.17..7761.86 rows=10 width=65)
8   Query plan  -> Index Scan using index_events_on_created_at on events (cost=0.09..7700.48 rows=15 width=65)
9   Query plan  Index Cond: ?
10  Query plan  Filter: ?
11  Query plan  -> Index Scan using flow_recipients_pkey on flow_recipients (cost=0.08..4.09 rows=1 width=8)
12  Query plan  Index Cond: ?
13  Query plan  Filter: ?
14  Query plan  -> Index Scan using flows_pkey on flows (cost=0.06..0.10 rows=1 width=8)
15  Query plan  Index Cond: ?
16  Query plan  Filter: ?
17  Query plan  -> Index Scan using users_pkey on users (cost=0.06..0.77 rows=1 width=4)
18  Query plan  Index Cond: ?
19  Query plan  Filter: ?

Вот существующие индексы для таблицы событий:

Indexes:
    "events_pkey" PRIMARY KEY, btree (id)
    "index_events_on_created_at" btree (created_at)
    "index_events_on_eventable_type_and_eventable_id" btree (eventable_type, eventable_id)
    "index_events_on_flow_action_id" btree (flow_action_id)
    "index_events_on_task_id" btree (task_id)
Foreign-key constraints:
    "fk_rails_4059ef8baf" FOREIGN KEY (flow_action_id) REFERENCES flow_actions(id)
    "fk_rails_43fb91855e" FOREIGN KEY (task_id) REFERENCES tasks(id)

Индексы для таблицы flow_recipients:

Indexes:
    "flow_recipients_pkey" PRIMARY KEY, btree (id)
    "index_flow_recipients_on_flow_id_and_contact_id" UNIQUE, btree (flow_id, contact_id) WHERE deleted_at IS NULL AND status::text <> 'finished'::text
    "index_flow_recipients_on_contact_id" btree (contact_id)
    "index_flow_recipients_on_flow_id" btree (flow_id)
Foreign-key constraints:
    "fk_rails_294cb3f9d3" FOREIGN KEY (contact_id) REFERENCES contacts(id)
    "fk_rails_74571fa23b" FOREIGN KEY (flow_id) REFERENCES flows(id)

Индексы для потоков:

Indexes:
    "flows_pkey" PRIMARY KEY, btree (id)
    "index_flows_on_company_id" btree (company_id)
    "index_flows_on_user_id" btree (user_id)
Foreign-key constraints:
    "fk_rails_42038c5d78" FOREIGN KEY (user_id) REFERENCES users(id)
    "fk_rails_f90fb17a30" FOREIGN KEY (company_id) REFERENCES companies(id)

Есть 3kk + записейпо запросу событий.Существуют ли какие-либо изменения в запросе или предложения по индексам, которые могут помочь в выполнении этого запроса?

...