【发布时间】:2014-02-15 20:14:57
【问题描述】:
我们有一个电子商店,在这个电子商店中,类别和产品之间有许多复杂的链接。
我正在使用分类表将 Products-Categories 和 Products-Products 之间的关系存储为子产品。
产品可能属于多个类别。 产品可能是其他产品的子产品。 (可能不止一个) 产品可能是其他产品的一个模块(可能不止一个)
查询的别名: 公关产品 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 条记录
【问题讨论】: