数据表:
goods                        商品表
attribute                    属性库表
goods_attribute       商品属性对应表
goods_combine       商品关联表
这样建表是为了实现sku(一个商品,不同的属性就分成多个商品了)
在搜索商品的时候就涉及到了,把某些属性的关联商品给合并的问题,比如红色 L、红色 S 、红色 M就在搜索页只显示一件。我这里把goods_combine,goods_attribute和attribute建为视图,以下是mysql优化后的语句:select min(`goods_combine`.`goods_id`) AS `minID` from (`goods_combine` left join (`goods_attribute` join `attribute` on(((`goods_attribute`.`attribute_id` = `attribute`.`id`) and (`attribute`.`combine` = 2)))) on((`goods_attribute`.`goods_id` = `goods_combine`.`goods_id`))) group by `goods_combine`.`sn`,(case when isnull(`goods_attribute`.`attribute_id`) then `goods_combine`.`goods_id` else `goods_attribute`.`attribute_id` end),(case when `goods_attribute`.`value` then `goods_combine`.`goods_id` else `goods_attribute`.`value` end);执行时间是0.822secExplain:
id, select_type, table, type, possible_key, key, key_len, rows, extra
'1', 'PRIMARY', '<derived2>', 'ALL', NULL, NULL, NULL, NULL, '11508', ''
'2', 'DERIVED', 'goods_combine', 'ALL', NULL, NULL, NULL, NULL, '12790', 'Using temporary; Using filesort'
'2', 'DERIVED', 'goods_attribute', 'ref', 'i_goods_attribute_goods_id,i_goods_attribute_attribute_id_value', 'i_goods_attribute_goods_id', '4', 'goods_combine.goods_id', '3', ''
'2', 'DERIVED', 'attribute', 'eq_ref', 'PRIMARY,i_attribute_combine', 'PRIMARY', '4', 'goods_attribute.attribute_id', '1', ''我想请教一下各位大神,这样的还能不能继续优化,要如何优化?
(还要跟商品表、图片表等式关联,速度很慢)