Мне не кажется, я точно знаю что это делается одним запросом, но человек упорно не хочет признаваться какую версию использует... а в зависимости от версии есть небольшое отличие в реализации.Но индекс о котором я писал всё равно нужен... посмотри на explain'ы
mysql> EXPLAIN SELECT COUNT(DISTINCT p.product_id) AS total FROM oc_product p LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN oc_product_to_category p2c ON (p.product_id = p2c.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0' AND p2c.category_id = '20';
+----+-------------+-------+--------+----------------+---------+---------+------- ----------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+----------------+---------+---------+------- ----------------------+------+--------------------------+
| 1 | SIMPLE | p2s | index | PRIMARY | PRIMARY | 8 | NULL | 7512 | Using where; Using index |
| 1 | SIMPLE | p | eq_ref | PRIMARY,status | PRIMARY | 4 | ocsfru.p2s.product_id | 1 | Using where |
| 1 | SIMPLE | pd | eq_ref | PRIMARY | PRIMARY | 8 | ocsfru.p2s.product_id,const | 1 | Using where; Using index |
| 1 | SIMPLE | p2c | eq_ref | PRIMARY | PRIMARY | 8 | ocsfru.p.product_id,const | 1 | Using where; Using index |
+----+-------------+-------+--------+----------------+---------+---------+------- ----------------------+------+--------------------------+
4 rows in set (0.00 sec)
mysql> ALTER TABLE `oc_product_to_category` ADD INDEX (`category_id`);
Query OK, 10022 rows affected (0.14 sec)
Records: 10022 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT COUNT(DISTINCT p.product_id) AS total FROM oc_product p LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN oc_product_to_category p2c ON (p.product_id = p2c.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0' AND p2c.category_id = '20';
+----+-------------+-------+--------+---------------------+-------------+-------- -+-----------------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------------+-------------+-------- -+-----------------------------+------+--------------------------+
| 1 | SIMPLE | p2c | ref | PRIMARY,category_id | category_id | 4 | const | 4 | Using where |
| 1 | SIMPLE | p2s | eq_ref | PRIMARY | PRIMARY | 8 | ocsfru.p2c.product_id,const | 1 | Using where; Using index |
| 1 | SIMPLE | p | eq_ref | PRIMARY,status | PRIMARY | 4 | ocsfru.p2c.product_id | 1 | Using where |
| 1 | SIMPLE | pd | eq_ref | PRIMARY | PRIMARY | 8 | ocsfru.p2s.product_id,const | 1 | Using where; Using index |
+----+-------------+-------+--------+---------------------+-------------+-------- -+-----------------------------+------+--------------------------+
4 rows in set (0.00 sec)