Query optimization with conditional group by and where
e and wp are not used, so remove the `LEFT JOINs to them.

Some composite indexes needed:

letture:  INDEX(id_event, entrata, qrcode, id_sala, datalectura)
ep:  INDEX(qrcode, id_event)  -- in either order


My suggested index for letture helps with all 3 uses of that table, and in different ways.  The order of the columns is critical, except that the first two can be swapped.

If AND  i2.datalettura &gt; i1.datalettura is part of doing a "groupwise max", then it is especially inefficient.  This discusses much faster ways.

AND  i3.datalettura IS NULL and its LEFT JOIN can probably be replaced by NOT EXISTS ( SELECT 1 ... )

The CROSS JOIN is technically an INNER JOIN, but since MySQL ignores CROSS and INNER, either of those works the same.  Ditto for plain JOIN.

The index recommendations come from looking at the WHERE clauses, and noting that nothing can be done about the GROUP BY.  More discussion here.

If you need to discuss this further, please provide SHOW CREATE TABLE and EXPLAIN SELECT ....