MariaDB [zabbix_infra]> explain SELECT DISTINCT g.graphid,g.name,g.flags,h.name as host FROM  graphs g,hosts h WHERE  EXISTS (SELECT ggi.graphid FROM graphs_items ggi,items ii WHERE ggi.graphid=g.templateid AND ii.itemid=ggi.itemid AND ii.hostid='100252') AND g.flags IN ('0','1');
+------+--------------+-------+-------+---------------------------------------------------------+----------------+---------+------------------------+--------+------------------------------+
| id   | select_type  | table | type  | possible_keys                                           | key            | key_len | ref                    | rows   | Extra                        |
+------+--------------+-------+-------+---------------------------------------------------------+----------------+---------+------------------------+--------+------------------------------+
|    1 | PRIMARY      | h     | index | NULL                                                    | hosts_4        | 386     | NULL                   |  33957 | Using index; Using temporary |
|    1 | PRIMARY      | g     | ALL   | NULL                                                    | NULL           | NULL    | NULL                   | 314946 | Using where                  |
|    2 | MATERIALIZED | ii    | ref   | PRIMARY,items_1,items_3,items_4,items_5,items_6,items_7 | items_1        | 8       | const                  |      1 | Using index                  |
|    2 | MATERIALIZED | ggi   | ref   | graphs_items_1,graphs_items_2                           | graphs_items_1 | 8       | zabbix_infra.ii.itemid |      1 |                              |
+------+--------------+-------+-------+---------------------------------------------------------+----------------+---------+------------------------+--------+------------------------------+
4 rows in set (0.00 sec)