Musíte se zbavit GROUP BY
zde.
MySQL
není dobré ji optimalizovat.
Přepište svůj dotaz takto:
SELECT *
FROM product_table
WHERE EXISTS
(
SELECT NULL
FROM product_table__category_1__mm mm_1
JOIN category cat_1
ON cat_1.uid = mm_1.uid_foreign
WHERE mm_1.uid_local = product_table.uid
AND mm_1.uid_foreign IN (7, 8)
)
AND
…
Vytvořte indexy:
product_table__category_*__mm (uid_local, uid_foreign)
nebo lépe, deklarujte je PRIMARY KEYs
na product_table__category_*__mm
:
ALTER TABLE product_table__category_*__mm ADD CONSTRAINT pk_pc*mm_local_foreign (uid_local, uid_foreign)