----- both groups do not have any access: zabbix=> explain analyze SELECT e.* FROM events e WHERE EXISTS (SELECT NULL FROM functions f,items i,hosts_groups hgg JOIN rights r ON r.id=hgg.groupid AND r.groupid IN ('11','15') WHERE e.objectid=f.triggerid AND f.itemid=i.itemid AND i.hostid=hgg.hostid GROUP BY f.triggerid HAVING MIN(r.permission)>0 AND MAX(r.permission)>='2') AND e.object='0' AND e.source='0' ORDER BY e.clock LIMIT 1 OFFSET 0; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.28..18.24 rows=1 width=40) (actual time=35.057..35.057 rows=0 loops=1) -> Index Scan using events_2 on events e (cost=0.28..32748.20 rows=1824 width=40) (actual time=35.056..35.056 rows=0 loops=1) Index Cond: ((source = 0) AND (object = 0)) Filter: (SubPlan 1) Rows Removed by Filter: 4286 SubPlan 1 -> GroupAggregate (cost=0.83..8.93 rows=1 width=12) (actual time=0.008..0.008 rows=0 loops=4286) Group Key: f.triggerid Filter: ((min(r.permission) > 0) AND (max(r.permission) >= 2)) -> Nested Loop (cost=0.83..8.91 rows=1 width=12) (actual time=0.007..0.007 rows=0 loops=4286) -> Nested Loop (cost=0.70..8.72 rows=1 width=16) (actual time=0.003..0.006 rows=1 loops=4286) -> Nested Loop (cost=0.56..8.57 rows=1 width=16) (actual time=0.002..0.004 rows=1 loops=4286) -> Index Scan using functions_1 on functions f (cost=0.28..4.28 rows=1 width=16) (actual time=0.001..0.001 rows=1 loops=4286) Index Cond: (e.objectid = triggerid) -> Index Scan using items_pkey on items i (cost=0.28..4.29 rows=1 width=16) (actual time=0.001..0.001 rows=1 loops=4901) Index Cond: (itemid = f.itemid) -> Index Only Scan using hosts_groups_1 on hosts_groups hgg (cost=0.14..0.15 rows=1 width=16) (actual time=0.001..0.001 rows=1 loops=4901) Index Cond: (hostid = i.hostid) Heap Fetches: 0 -> Index Scan using rights_2 on rights r (cost=0.13..0.19 rows=1 width=12) (actual time=0.001..0.001 rows=0 loops=4909) Index Cond: (id = hgg.groupid) Filter: (groupid = ANY ('{11,15}'::bigint[])) Rows Removed by Filter: 1 Planning time: 0.545 ms Execution time: 35.113 ms (25 rows) ----- groupid=16 has some READ host access: zabbix=> explain analyze SELECT e.* FROM events e WHERE EXISTS (SELECT NULL FROM functions f,items i,hosts_groups hgg JOIN rights r ON r.id=hgg.groupid AND r.groupid IN ('11','16') WHERE e.objectid=f.triggerid AND f.itemid=i.itemid AND i.hostid=hgg.hostid GROUP BY f.triggerid HAVING MIN(r.permission)>0 AND MAX(r.permission)>='2') AND e.object='0' AND e.source='0' ORDER BY e.clock LIMIT 1 OFFSET 0; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.28..18.24 rows=1 width=40) (actual time=0.061..0.061 rows=1 loops=1) -> Index Scan using events_2 on events e (cost=0.28..32748.20 rows=1824 width=40) (actual time=0.061..0.061 rows=1 loops=1) Index Cond: ((source = 0) AND (object = 0)) Filter: (SubPlan 1) Rows Removed by Filter: 6 SubPlan 1 -> GroupAggregate (cost=0.83..8.93 rows=1 width=12) (actual time=0.006..0.006 rows=0 loops=7) Group Key: f.triggerid Filter: ((min(r.permission) > 0) AND (max(r.permission) >= 2)) -> Nested Loop (cost=0.83..8.91 rows=1 width=12) (actual time=0.004..0.005 rows=0 loops=7) -> Nested Loop (cost=0.70..8.72 rows=1 width=16) (actual time=0.004..0.004 rows=0 loops=7) -> Nested Loop (cost=0.56..8.57 rows=1 width=16) (actual time=0.003..0.003 rows=0 loops=7) -> Index Scan using functions_1 on functions f (cost=0.28..4.28 rows=1 width=16) (actual time=0.001..0.001 rows=0 loops=7) Index Cond: (e.objectid = triggerid) -> Index Scan using items_pkey on items i (cost=0.28..4.29 rows=1 width=16) (actual time=0.004..0.004 rows=1 loops=1) Index Cond: (itemid = f.itemid) -> Index Only Scan using hosts_groups_1 on hosts_groups hgg (cost=0.14..0.15 rows=1 width=16) (actual time=0.003..0.003 rows=1 loops=1) Index Cond: (hostid = i.hostid) Heap Fetches: 0 -> Index Scan using rights_2 on rights r (cost=0.13..0.19 rows=1 width=12) (actual time=0.003..0.004 rows=1 loops=1) Index Cond: (id = hgg.groupid) Filter: (groupid = ANY ('{11,16}'::bigint[])) Planning time: 0.559 ms Execution time: 0.115 ms (24 rows) zabbix=> select count(*) from events e where e.object='0' AND e.source='0'; count ------- 4286 (1 row)