我们有一家电子商店,在这个电子商店中,类别和产品之间有许多复杂的联系。缓慢的Mysql查询,3个左连接
我正在使用分类标准表来存储产品 - 类别和产品 - 产品之间的关系作为子产品。
产品可能是多个类别的成员。 产品可能是其他产品的子产品。 (可能多于一个) 产品可以是其它产品的模块(可能多于一个)的查询的
别名: PR-产品 CT-类别 SP-子产品 MD-模块
Select pr.*,ifnull(sp.destination_id,0) as `top_id`,
ifnull(ct.destination_id,0) as `category_id`
from Products as pr
Left join Taxonomy as ct
on (ct.source_id=pr.id and ct.source='Products' and ct.destination='Categories')
Left join Taxonomy as sp
on (sp.source_id=pr.id and sp.source='Products' and sp.destination='Products' and sp.type='TOPID')
Left join Modules as md
on(pr.id = md.product_id)
where pr.deleted=false
and ct.destination_id='47'
and sp.destination_id is null
and md.product_id is null
order by pr.order,pr.sub_order
使用此查询;我试图让所有产品在Category_id = 47下,而不是任何产品的模块,也不是任何产品的子产品。
此查询需要23秒。 产品中有7.820条记录,模块中有3.200条记录,分类标准有19.000条记录