即使使用 INNER JOIN 而不是 IN,MySQL 查询也很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20917717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL queries are very slow even using INNER JOIN instead of IN
提问by user3138073
I have three tables:product (10k records), product_attribute (4k records), and product_value (2m records). product and product_attribute are connected via product_value. I want to retrieve a specific product (e.g., product_id = 001) and its respective attribute name and attribute value. At first, I try
我有三个表:product(10k 条记录)、product_attribute(4k 条记录)和product_value(2m 条记录)。product 和 product_attribute 通过 product_value 连接。我想检索特定产品(例如,product_id = 001)及其各自的属性名称和属性值。起初,我尝试
SELECT product.product_id, product_attribute.attribute_name, product_value.attribute_value
FROM product, product_attribute, product_value
WHERE product.product_id = 001 AND product.product_id = product_value.product_id AND product_attribute.product_attribute_id IN (SELECT product_value.product_attribute_id FROM product_value WHERE product_value.product_id = 001)
But it is extremely slow. Then I used INNER JOIN
instead of IN
但它非常缓慢。然后我用INNER JOIN
而不是IN
SELECT product.product_id, product_attribute.attribute_name, product_value.attribute_value FROM product
INNER JOIN product_value ON product.product_id = 001 AND product.product_id = product_value.product_id
INNER JOIN product_attribute ON product_attribute.product_attribute_id = product_value.product_attribute_id WHERE product.product_id = 001
But it is still very slow: the query returns 31 rows in 36 minutes!
但是还是很慢:查询在 36 分钟内返回 31 行!
Is there any better solution for this problem?
这个问题有没有更好的解决方案?
Explaining the query gives:
解释查询给出:
*************************** 1. row ***********
id: 1
select_type: SIMPLE
table: product_attribute
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 5247
Extra:
*************************** 2. row ***********
id: 1
select_type: SIMPLE
table: product
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 91818
Extra: Using where; Using join buffer
*************************** 3. row ***********
id: 1
select_type: SIMPLE
table: product_value
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1731016
Extra: Using where; Using join buffer
采纳答案by Saharsh Shah
Try this:
尝试这个:
SELECT p.product_id, pa.attribute_name, pv.attribute_value
FROM product p
INNER JOIN product_value pv ON p.product_id = pv.product_id
INNER JOIN product_attribute pa ON pa.product_attribute_id = pv.product_attribute_id
WHERE p.product_id = 001
Run this query and if you still facing the query is slow than add you EXPLAIN
plan of above query
运行此查询,如果您仍然面临查询比添加EXPLAIN
上述查询计划慢
You have to create a INDEX on columns to improve performance.
您必须在列上创建一个 INDEX 以提高性能。
Create index on
product_id
column ofproduct_value
tableCreate index on
product_attribute_id
column ofproduct_value
table
在表的
product_id
列上创建索引product_value
在表的
product_attribute_id
列上创建索引product_value