PostgreSQL慢速运行查询

时间:2018-05-28 01:11:34

标签: sql database postgresql indexing query-optimization

以下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 +记录。查询或任何索引建议是否有任何更改可以帮助您执行此查询?

0 个答案:

没有答案