我遇到了Postgres查询的问题我试图运行 - 我已经尝试了很多方法来解决问题,但到目前为止没有任何喜悦。
我设法编写了一些有效的查询,但关键是性能之一 - 有效的查询太慢而无法使用。
我有一个名为events_hub
的表,它链接到包含有关不同事件的信息的单独表。不同的事件由不同的event_types
区分。这些事件也分组为聚合,聚合由aggregate_id
区分。
我的基本问题是我希望找到与每个聚合组相关的事件1的最早时间,然后在导致该时间的时间窗口中计算事件2的发生次数(例如,计算次事件2发生在最早出现聚合组之前的24小时内。)
事件中心表看起来如下所示:
| aggregate_id | event_id | event_type | event_time |
-------------------------------------------------------
| 1 | 1 | 1 | 1st Jan |
| 1 | 2 | 1 | 2nd Jan |
| 2 | 3 | 1 | 2nd Jan |
| 2 | 4 | 1 | 3rd Jan |
| null | 5 | 2 | 30th Dec |
| null | 6 | 2 | 31st Dec |
| null | 7 | 2 | 1st Jan |
| null | 8 | 2 | 1st Jan |
-------------------------------------------------------
在上面的玩具示例中,我想要返回:
| aggregate_id | count_of_event2 |
----------------------------------
| 1 | 3 |
| 2 | 2 |
----------------------------------
因为最早出现的aggregate_id 1在前一天发生了3次event_type 2,而aggregate_id 2只发生了2次。
方法1
我的第一次尝试涉及使用由group by包围的连接。以下查询运行速度非常快,但不能完全返回我想要的内容:
SELECT
aggregate_id,
count(aggregate_id)
FROM
(SELECT
aggregate_id,
min(event_time) as time_of_event1
FROM events_hub WHERE event_type = 1
GROUP BY aggregate_id) as t1
LEFT JOIN
(SELECT event_time as time_of_event2
FROM events_hub WHERE event_type = 2) as t2
ON t2.time_of_event2 BETWEEN t1.time_of_event1 - INTERVAL '24 hours'
AND t1.time_of_event1
GROUP BY aggregate_id
对此运行EXPLAIN ANALYZE
将返回以下内容(请注意,此问题中的SQL查询是我要运行的实际查询的简化版本 - 因此对表格有一些额外限制在解释计划中):
HashAggregate (cost=1262545.21..1262547.21 rows=200 width=15) (actual time=536.206..539.222 rows=2824 loops=1)
Group Key: events_hub_1.aggregate_id
-> Nested Loop Left Join (cost=9137.36..1191912.59 rows=14126523 width=15) (actual time=15.419..395.895 rows=111948 loops=1)
-> HashAggregate (cost=9136.80..9141.42 rows=462 width=23) (actual time=15.387..19.316 rows=2824 loops=1)
Group Key: events_hub_1.aggregate_id
-> Index Only Scan using comp_index1 on events_hub events_hub_1 (cost=0.56..9110.87 rows=5186 width=23) (actual time=2.669..9.750 rows=4412 loops=1)
Index Cond: ((event_type_code = 5) AND (event_datetime >= '2013-01-01 00:00:00'::timestamp without time zone) AND (event_datetime <= '2013-01-02 00:00:00'::timestamp without time zone) AND (aggregate_id IS NOT NULL))
Heap Fetches: 4412
-> Index Only Scan using comp_index on events_hub (cost=0.56..2254.33 rows=30577 width=8) (actual time=0.005..0.049 rows=40 loops=2824)
Index Cond: ((event_type_code = 3) AND (event_datetime <= (min(events_hub_1.event_datetime))) AND (event_datetime >= ((min(events_hub_1.event_datetime)) - '12:00:00'::interval)))
Heap Fetches: 0
Planning time: 0.326 ms
Execution time: 542.020 ms
这并不特别令人惊讶,因为我在事件中心上有一个复合索引(event_type, event_time)
,因此基于2个事件的相对时间的相对复杂的连接条件快速运行。
然而,当我尝试根据事件2的某些属性为查询添加另一个条件时(为了得到我需要的结果),查询速度大大减慢(如上面的查询在闪存中完成) ,而以下将运行几分钟):
SELECT
aggregate_id,
count(aggregate_id)
FROM
(SELECT
aggregate_id,
min(event_time) as time_of_event1
FROM events_hub WHERE event_type = 1
GROUP BY aggregate_id) as t1
LEFT JOIN
(SELECT event_id, event_time as time_of_event2
FROM events_hub WHERE event_type = 2) as t2
ON t2.time_of_event2 BETWEEN t1.time_of_event1 - INTERVAL '24 hours'
AND t1.time_of_event1
INNER JOIN
(SELECT event_id FROM event_2_attributes WHERE some_flag = TRUE) as t3
ON t2.event_id = t3.event_id
GROUP BY aggregate_id
对于此查询,EXPLAIN ANALYZE
查询返回:
HashAggregate (cost=33781.17..33783.17 rows=200 width=15) (actual time=479888.736..479891.819 rows=2824 loops=1)
Group Key: events_hub_1.aggregate_id
-> Nested Loop (cost=9625.94..33502.10 rows=55815 width=15) (actual time=346721.414..479857.494 rows=26164 loops=1)
Join Filter: ((events_hub.event_datetime <= (min(events_hub_1.event_datetime))) AND (events_hub.event_datetime >= ((min(events_hub_1.event_datetime)) - '12:00:00'::interval)))
Rows Removed by Join Filter: 209062796
-> Merge Join (cost=489.14..14311.03 rows=1087 width=8) (actual time=1.360..1571.387 rows=74040 loops=1)
Merge Cond: (events_hub.event_id = arrests.event_id)
-> Index Scan using comp_index4 on events_hub (cost=0.44..290158.71 rows=275192 width=12) (actual time=1.344..512.787 rows=282766 loops=1)
Index Cond: (event_type_code = 3)
-> Index Scan using arrests_events_id_index on arrests (cost=0.42..11186.59 rows=73799 width=4) (actual time=0.008..456.550 rows=74040 loops=1)
Filter: felony_flag
Rows Removed by Filter: 210238
-> Materialize (cost=9136.80..9148.35 rows=462 width=23) (actual time=0.001..3.002 rows=2824 loops=74040)
-> HashAggregate (cost=9136.80..9141.42 rows=462 width=23) (actual time=10.963..14.006 rows=2824 loops=1)
Group Key: events_hub_1.aggregate_id
-> Index Only Scan using comp_index1 on events_hub events_hub_1 (cost=0.56..9110.87 rows=5186 width=23) (actual time=0.018..5.405 rows=4412 loops=1)
Index Cond: ((event_type_code = 5) AND (event_datetime >= '2013-01-01 00:00:00'::timestamp without time zone) AND (event_datetime <= '2013-01-02 00:00:00'::timestamp without time zone) AND (aggregate_id IS NOT NULL))
Heap Fetches: 4412
Planning time: 12.548 ms
Execution time: 479894.888 ms
请注意,当包含内部联接时,实际上会返回 less 数据。然而它仍然运行得慢得多。
我已经摆弄了将这些联接嵌套在彼此内部并切换周围的东西,以便有RIGHT JOIN
而不是LEFT JOIN
,但它没有任何区别。
我也尝试过每个子查询的CTE表达式来尝试强制执行顺序,但也没有运气。
方法2
作为第二种方法,我尝试使用返回事件2计数的子查询:
SELECT
t1.aggregate_id,
(SELECT count(t3.event_id)
FROM (SELECT event_id FROM events_hub AS t2 WHERE t2.event_type = 2
AND t2.event_time BETWEEN t1.time_of_event1 - INTERVAL '24 hours'
AND t1.time_of_event1) as t3
INNER JOIN event_2_attributes as t4
ON t3.event_id = t4.event_id
WHERE t4.some_flag = TRUE) as count_column
FROM
(SELECT
aggregate_id,
min(event_time) as time_of_event1
FROM events_hub WHERE event_type = 1
GROUP BY aggregate_id) as t1
这很好用,大约15秒就可以运行。但是,当我尝试获取结果并将它们插入到另一个表中时(这是我接下来要做的),查询需要花费大量时间来运行:
CREATE TABLE tbl AS
< query above >
这令我感到困惑!
我尝试在此查询上运行EXPLAIN ANALYZE
,但在退出之前已经达到了2000秒。如上所述,没有EXPLAIN ANALYZE
,这将在15秒内运行。
方法3
作为最后的方法,我尝试使用横向连接如下(没有这里的组):
WITH t1 AS
(SELECT
aggregate_id,
min(event_time) as time_of_event1
FROM events_hub WHERE event_type = 1
GROUP BY aggregate_id)
SELECT
t1.aggregate_id,
t2.event_time
FROM t1
LEFT JOIN LATERAL
(SELECT event_time FROM
(SELECT event_id, event_time FROM events_hub WHERE event_type = 2) as t3
INNER JOIN
(SELECT event_id FROM event_2_attributes WHERE some_flag = TRUE) as t4
ON t3.event_id = t4.event_id
WHERE t3.event_time BETWEEN t1.time_of_event1 - INTERVAL '24 hours'
AND t1.time_of_event1
) as t2
ON TRUE
此查询运行,但同样非常非常缓慢 - 即使没有按操作分组。
你可以在这些(可能不相关的?)上留下的任何亮光都会非常感激。值得一提的是,事件中心的每个列都已编入索引。
非常感谢!
答案 0 :(得分:1)
好的,我已经弄明白了。
虽然不是最好的&#39;解决方案的最后一招是创建一个包含初始GROUP BY
操作结果的表,该操作返回与aggregate_id
相关的最早时间:
CREATE TABLE earliest_time AS
(SELECT
aggregate_id,
min(event_time) as time_of_event1
FROM events_hub WHERE event_type = 1
GROUP BY aggregate_id)
然后在aggregate_id
和time_of_event1
列上添加索引。
然后按照上述方法1使用该表。
使子查询已经实现有助于计划者选择最有效的路径,并且执行时间下降2个数量级。
答案 1 :(得分:0)
不确定这是否有用,因为你没有包含EXPLAIN ANALIZE
,但是当你创建子查询然后加入时,你通常会失去使用索引。
尝试像这样重写
SELECT e.event_id, e.event_time, ea.event_id -- but dont think you need it repeat event_id
FROM events e
INNER JOIN event_2_attributes ea
ON e.event_id = ea.event_id
WHERE e.event_type = 2
AND ea.some_flag = TRUE