【发布时间】:2016-02-22 02:54:24
【问题描述】:
select COUNT(p.id) AS `num`, cat.id, cat.name, cat.parent_id AS `parent_id`
from products p
INNER JOIN `products_categories` AS `pc` ON p.id=pc.products_id
INNER JOIN `categories` AS `cat` ON pc.categories_id=cat.id
WHERE p.status = 1 AND p.gender IN ('female','neutral')
group by cat.id
解释查询:
1 SIMPLE p ref PRIMARY,gender,status status 1 const 139107 Using where; Using temporary; Using filesort
1 SIMPLE pc ref products_id,categories products_id 4 mydb.p.id 1 Using index
1 SIMPLE cat eq_ref PRIMARY,categoryname PRIMARY 4 mydb.pc.categories_id 1 Using where
相关索引:
products 0 PRIMARY 1 id A 299339 BTREE
products 1 title 1 title A 299339 BTREE
products 1 sku 1 sku A 299339 BTREE
products 1 body 1 body A 299339 200 BTREE
products 1 short_description 1 short_description A 299339 200 YES BTREE
products 1 keywords 1 keywords A 2 200 BTREE
products 1 gender 1 gender A 10 BTREE
products 1 status 1 status A 2 BTREE
products 1 brand_id 1 brand_id A 3741 YES BTREE
products 1 merchant 1 merchant_id A 52 BTREE
products 1 title_2 1 title,body,keywords 299339 FULLTEXT
products 1 title_3 1 title 299339 FULLTEXT
products 1 body_2 1 body 299339 FULLTEXT
products_categories 0 PRIMARY 1 id A 514054 BTREE
products_categories 1 products_id 1 products_id, categories_id A 514054 BTREE
products_categories 1 categories 1 categories_id A 266 BTREE
categories 0 PRIMARY 1 id A 154 BTREE
categories 1 categoryname 1 name A 154 BTREE
这是一个包含产品、类别以及它们之间的 N:N 关系的数据库。产品可以属于 1 个或多个类别。
我基本上需要一个查询来告诉我,对于我拥有的当前产品过滤器(在这种情况下是状态和性别),该类别是否有任何产品(因此我可以隐藏没有产品的类别)。目前我统计了每个类别中的产品来了解这一点。
查询的 WHERE 参数将根据用户选择的过滤器而变化,因此该部分在此优化中不是很重要。
我不需要一个类别的确切产品数量,只要他们有产品与否。 Products 表有很多索引,有 products_categories 和 categories 表。 Products 表有大约 400k 个产品,150 个类别和 500k 个 products_categories。
AWS RDS 上托管的 MySQL 5.6.22,InnoDB 中的所有表。
我知道我的解释查询显示了为什么这很慢(通过很多产品),但我不知道如何优化它......也许可以换一种方式来思考这个问题?
【问题讨论】:
标签: mysql sql sql-optimization