我的情况有点奇怪,我正在尝试理解它。
我有两个表,我将列出必要的字段。
products [MyIsam -- due to full text search]
product_id [primary index]
其中大约有 1,000,000 行。
website_products [InnoDB]
website_id [primary]
product_id [primary]
其中大约有 6,000,000 行。
<小时/>现在当我进行查询时...
SELECT
COUNT(`product_id`)
FROM `products`
0.109 秒后返回结果“1030650”
SELECT
COUNT(wp.`product_id`)
FROM `website_products` AS wp
WHERE wp.`website_id` = 1133
0.577 秒后返回结果“104150”
SELECT
COUNT(wp.`product_id`)
FROM `website_products` AS wp
LEFT JOIN `products` AS p ON p.product_id = wp.product_id
WHERE wp.`website_id` = 1133
38.173 秒后返回结果“104150”
EXPLAIN SELECT 的结果是:
1 SIMPLE wp ref PRIMARY,website_id PRIMARY 4 const 204392 Using index
1 SIMPLE p eq_ref PRIMARY PRIMARY 4 imaginer_system.wp.product_id 1 Using index
为什么我加入 table 后时间变长了?我怎样才能减少这个?
最佳答案
对于此查询:
SELECT COUNT(wp.`product_id`)
FROM `website_products` wp LEFT JOIN
`products` p
ON p.product_id = wp.product_id
WHERE wp.`website_id` = 1133;
最佳索引为:website_products(website_id, Product_id)
和 products(product_id)
。
关于MySQL——索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306108/