MariaDB [zabbix]> explain SELECT DISTINCT t.triggerid,t.lastchange FROM triggers t,functions f,items i,hosts_groups hg WHERE NOT EXISTS (SELECT NULL FROM functions f,items i,hosts_groups hgg LEFT JOIN rights r ON r.id=hgg.groupid AND r.groupid IN ('121','125','126','128','129') WHERE t.triggerid=f.triggerid AND f.itemid=i.itemid AND i.hostid=hgg.hostid GROUP BY i.hostid HAVING MAX(permission)<'2' OR MIN(permission) IS NULL OR MIN(permission)=0) AND hg.hostid=i.hostid AND f.triggerid=t.triggerid AND f.itemid=i.itemid AND hg.groupid='295' AND NOT EXISTS (SELECT NULL FROM functions f,items i,hosts h WHERE t.triggerid=f.triggerid AND f.itemid=i.itemid AND i.hostid=h.hostid AND (i.status<>0 OR h.status<>0)) AND t.status=0 AND EXISTS (SELECT NULL FROM events e WHERE t.triggerid=e.objectid AND e.source=0 AND e.object=0 AND e.value=1 AND e.acknowledged=0) AND t.flags IN ('0','4') AND ((t.value=1) OR ((t.value=0) AND (t.lastchange>1424688389))) ORDER BY t.lastchange DESC; +------+--------------------+-------------+--------+-----------------------------------------------------------+----------------+---------+--------------------+---------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+--------------------+-------------+--------+-----------------------------------------------------------+----------------+---------+--------------------+---------+---------------------------------+ | 1 | PRIMARY | hg | ref | hosts_groups_1,hosts_groups_2 | hosts_groups_2 | 8 | const | 2060 | Using temporary; Using filesort | | 1 | PRIMARY | i | ref | PRIMARY,items_1 | items_1 | 8 | zabbix.hg.hostid | 39 | Using index | | 1 | PRIMARY | f | ref | functions_1,functions_2 | functions_2 | 8 | zabbix.i.itemid | 1 | Using where | | 1 | PRIMARY | t | eq_ref | PRIMARY,triggers_1,triggers_2 | PRIMARY | 8 | zabbix.f.triggerid | 1 | Using where | | 1 | PRIMARY | | eq_ref | distinct_key | distinct_key | 8 | func | 1 | Distinct | | 4 | MATERIALIZED | e | ref | events_1,events_2 | events_1 | 8 | const,const | 3868792 | Using where; Distinct | | 3 | MATERIALIZED | h | index | PRIMARY,hosts_1,hosts_2,hosts_3,hosts_4,hosts_5,c_hosts_3 | hosts_2 | 4 | NULL | 5600 | Using index | | 3 | MATERIALIZED | i | ref | PRIMARY,items_1,items_3,items_4,items_5,items_6 | items_1 | 8 | zabbix.h.hostid | 39 | Using where | | 3 | MATERIALIZED | f | ref | functions_1,functions_2 | functions_2 | 8 | zabbix.i.itemid | 1 | | | 2 | DEPENDENT SUBQUERY | f | ref | functions_1,functions_2 | functions_1 | 8 | zabbix.t.triggerid | 1 | Using temporary; Using filesort | | 2 | DEPENDENT SUBQUERY | i | eq_ref | PRIMARY,items_1 | PRIMARY | 8 | zabbix.f.itemid | 1 | | | 2 | DEPENDENT SUBQUERY | hgg | ref | hosts_groups_1 | hosts_groups_1 | 8 | zabbix.i.hostid | 1 | Using index | | 2 | DEPENDENT SUBQUERY | r | ref | rights_1,rights_2 | rights_2 | 8 | zabbix.hgg.groupid | 1 | Using where | +------+--------------------+-------------+--------+-----------------------------------------------------------+----------------+---------+--------------------+---------+---------------------------------+ 13 rows in set (0.00 sec)