# EXPLAIN ANALYZE SELECT DISTINCT i.hostid,e.* FROM events e,functions f,items i WHERE EXISTS (SELECT NULL FROM functions f,items i,hosts_groups hgg JOIN rights r ON r.id=hgg.groupid AND r.groupid IN ('45','23') WHERE e.objectid=f.triggerid AND f.itemid=i.itemid AND i.hostid=hgg.hostid AND e.object=0 GROUP BY f.triggerid HAVING MIN(r.permission)>=2) AND i.hostid IN ('10125') AND f.triggerid=e.objectid AND f.itemid=i.itemid AND e.source=0 ORDER BY e.eventid DESC LIMIT 10 OFFSET 0; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=6332820.25..6332820.27 rows=10 width=52) (actual time=57337.634..57337.641 rows=10 loops=1) -> Sort (cost=6332820.25..6332830.49 rows=4095 width=52) (actual time=57337.632..57337.634 rows=10 loops=1) Sort Key: e.eventid Sort Method: top-N heapsort Memory: 17kB -> HashAggregate (cost=6332690.81..6332731.76 rows=4095 width=52) (actual time=57331.349..57334.414 rows=5759 loops=1) -> Nested Loop (cost=0.00..6332588.43 rows=4095 width=52) (actual time=370.647..57315.624 rows=11542 loops=1) -> Nested Loop (cost=0.00..14250.65 rows=137 width=16) (actual time=0.116..1.356 rows=259 loops=1) -> Index Scan using items_1 on items i (cost=0.00..5020.34 rows=1160 width=16) (actual time=0.011..0.051 rows=26 loops=1) Index Cond: (hostid = 10125::bigint) -> Index Scan using functions_2 on functions f (cost=0.00..7.89 rows=5 width=16) (actual time=0.005..0.031 rows=10 loops=26) Index Cond: (itemid = i.itemid) -> Index Scan using events_1 on events e (cost=0.00..46117.82 rows=115 width=44) (actual time=200.592..221.250 rows=45 loops=259) Index Cond: (objectid = f.triggerid) Filter: ((source = 0) AND (SubPlan 1)) SubPlan 1 -> GroupAggregate (cost=0.00..32.39 rows=2 width=12) (actual time=0.076..0.076 rows=1 loops=11542) Filter: (min(r.permission) >= 2) -> Result (cost=0.00..32.35 rows=3 width=12) (actual time=0.023..0.073 rows=4 loops=11542) One-Time Filter: (e.object = 0) -> Nested Loop (cost=0.00..32.35 rows=3 width=12) (actual time=0.022..0.070 rows=4 loops=11542) -> Nested Loop (cost=0.00..30.51 rows=6 width=16) (actual time=0.010..0.033 rows=10 loops=11542) -> Nested Loop (cost=0.00..29.82 rows=2 width=16) (actual time=0.007..0.014 rows=2 loops=11542) -> Index Scan using functions_1 on functions f (cost=0.00..9.94 rows=2 width=16) (actual time=0.003..0.004 rows=2 loops=11542) Index Cond: (e.objectid = triggerid) -> Index Scan using items_pkey on items i (cost=0.00..9.93 rows=1 width=16) (actual time=0.002..0.003 rows=1 loops=23768) Index Cond: (itemid = f.itemid) -> Index Scan using hosts_groups_1 on hosts_groups hgg (cost=0.00..0.30 rows=3 width=16) (actual time=0.002..0.005 rows=5 loops=23768) Index Cond: (hostid = i.hostid) -> Index Scan using rights_2 on rights r (cost=0.00..0.29 rows=1 width=12) (actual time=0.002..0.002 rows=0 loops=118840) Index Cond: (id = hgg.groupid) Filter: (groupid = ANY ('{45,23}'::bigint[])) Total runtime: 57337.762 ms