我的查询非常慢,执行时间为2秒。我试图优化它,但我无法找出问题所在,我优化了表并为相关表添加了索引,但它仍然很慢。查询是这样的:
EXPLAIN SELECT
p.id
FROM product p
LEFT JOIN brand b ON p.brand_id = b.id
LEFT JOIN supplier s ON p.supplier_id = s.id
LEFT JOIN category_product cp ON p.id = cp.product_id
LEFT JOIN product_option_value pov ON p.id = pov.product_id
LEFT JOIN media m ON p.id = m.product_id AND m.type = 1
LEFT JOIN shop_product sp ON p.id = sp.product_id AND sp.shop_id = '75'
LEFT JOIN category c ON cp.category_id = c.id
GROUP BY p.master_model;
此查询的输出为:
+----+-------------+-------+------------+--------+--------------------+-------------------+---------+----------------+------+----------+---------------------------------+
| Id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-------+------------+--------+--------------------+-------------------+---------+----------------+------+----------+---------------------------------+
| 1 | SIMPLE | p | NULL | ALL | master_model | NULL | NULL | NULL | 4063 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | b | NULL | eq_ref | PRIMARY | PRIMARY | 4 | p.brand_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | s | NULL | eq_ref | PRIMARY | PRIMARY | 4 | p.supplier_id | 1 | 100.00 | Using index |
| 1 | SIMPLE | cp | NULL | ref | product_id | product_id | 4 | p.id | 3 | 100.00 | NULL |
| 1 | SIMPLE | pov | NULL | ref | product_option_id | product_option_id | 4 | p.id | 4 | 100.00 | Using index |
| 1 | SIMPLE | m | NULL | ref | product_id,type | product_id | 4 | p.id | 5 | 100.00 | Using where |
| 1 | SIMPLE | sp | NULL | ref | shop_id,product_id | product_id | 4 | p.id | 2 | 2.45 | Using where |
| 1 | SIMPLE | c | NULL | eq_ref | PRIMARY | PRIMARY | 4 | cp.category_id | 1 | 100.00 | Using index |
+----+-------------+-------+------------+--------+--------------------+-------------------+---------+----------------+------+----------+---------------------------------+
有人可能知道其他可能出错的事情。