我不是 MySQL 高手,但我明白了,我刚刚继承了一个相当大的表(600,000 行和大约 90 列(请杀了我...)),并且我创建了一个较小的表来将其链接到类别表。
我正在尝试使用左连接查询所述表,因此我在一个对象中拥有两组数据,但它运行速度非常慢,而且我还不够热,无法对其进行排序;我真的很感激一些关于为什么这么慢的指导和解释。
SELECT
`products`.`Product_number`,
`products`.`Price`,
`products`.`Previous_Price_1`,
`products`.`Previous_Price_2`,
`products`.`Product_number`,
`products`.`AverageOverallRating`,
`products`.`Name`,
`products`.`Brand_description`
FROM `product_categories`
LEFT OUTER JOIN `products`
ON `products`.`product_id`= `product_categories`.`product_id`
WHERE COALESCE(product_categories.cat4, product_categories.cat3,
product_categories.cat2, product_categories.cat1) = '123456'
AND `product_categories`.`product_id` != 0
这两个表是MyISAM,产品表在Product_number和Brand_Description上有索引,而product_categories表在所有组合列上都有唯一索引;如果此信息有任何帮助的话。
继承了这个系统后,我需要在我用核武器攻击它之前尽快让它工作并正确地完成它,所以现在的任何帮助都将赢得你我最大的尊重!
[编辑]
这是解释扩展的输出:
+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+
| 1 | SIMPLE | product_categories | index | NULL | cat1 | 23 | NULL | 1224419 | 100.00 | Using where; Using index |
| 1 | SIMPLE | products | ALL | Product_id | NULL | NULL | NULL | 512376 | 100.00 | |
+----+-------------+--------------------+-------+---------------+------+---------+------+---------+----------+--------------------------+