php - 如何在保持查询清晰有效的同时实现动态属性

标签 php sql database-design join attributes

我正在尝试为产品和相关的动态产品属性设计数据库/模型。然而,在建模部分,我担心我做错了什么,因为我最终使用了很多 JOINS。基本上,我希望就如何实现所需的功能获得一些意见,也许使用更简单的设计。 如果有经验的人觉得我错过了一些重要的东西 - 那么请赐教 :)

动态产品属性示例:

  • 尺寸
  • 颜色
  • 制造商
  • 体重

dynamic”属性涵盖两个方面,一个是单一值,例如等于某个值的权重,另一个是包含许多不同值的示例颜色。 除了属性值之外,每个属性还具有独特的语言设置。例如。属性键/值可以根据客户语言更改。

我的表格结构:

Product

- id
- created
- modified

Product_variants

- id
- product_id
- quantity
- price

Attribute

- id
- visible
- required
- comparable

Attribute_group

- id
- type

Attribute_groups_attributes

- attribute_group_id
- attribute_id

Attribute_group_languages

- id
- attribute_group_id
- name
- locale

Attribute_languages

- id
- attribute_id
- name
- value
- locale

Product_attribute

- product_id
- attribute_id
- group
- variant

查询示例

为了获取最新的产品和匹配的属性,我执行了以下讨厌的查询:

SELECT pv.price, pv.special_price, pv.overlay_id, p.id, pv.id variant, pl.url_key, pl.name, UNIX_TIMESTAMP(p.created) created, al.value color_value, a.id color_id,al.name color_name
FROM x_product_attribute pa
INNER JOIN x_attribute_group ag
ON pa.attribute_id = ag.id AND pa.group = 1 AND pa.variant = 0 AND ag.type LIKE 'color%'
INNER JOIN x_attribute_group_languages agl
ON agl.attribute_group_id = ag.id AND agl.locale = :locale
INNER JOIN x_attribute_groups_attributes aga
ON aga.attribute_group_id = pa.attribute_id
INNER JOIN x_product_attribute pa2
ON aga.attribute_id = pa2.attribute_id AND pa2.variant = 1 AND pa2.group = 0
INNER JOIN product_variants pv
ON pa2.product_id = pv.id
INNER JOIN x_attribute_languages al
ON al.attribute_id = pa2.attribute_id AND al.locale = :locale
INNER JOIN x_attribute a
ON a.id = pa2.attribute_id
INNER JOIN products p
ON p.id = pa.product_id
INNER JOIN product_languages pl
ON pl.product_id = p.id
INNER JOIN shop_products sp
ON sp.shop_id = :shop_id AND sp.product_id = pa.product_id
GROUP BY p.id,a.id
ORDER BY p.created DESC,a.sort ASC, p.id DESC 

如前所述,查询中有很多 JOINS 应该相对简单。我可以将其拆分为更小的查询,但我不确定什么是最有效的 - 同时仍然映射并保持属性的“动态”特性。

最佳答案

我会建议 NoSQL解决方案,例如 MongoDB .这将使您的应用程序设计变得更加容易,因为您可以使用简单的名称-值对(就像 JSON)来表示您的产品。

如果您坚持使用 SQL,我认为您会发现您的应用程序和数据库变得难以管理。

关于php - 如何在保持查询清晰有效的同时实现动态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9253352/

相关文章:

sql - 在多列和其他列上选择 DISTINCT

mysql - 内存消耗和正确的数据库设计

mysql - Material list 和版本控制

android - 由于 Firebase 规则不是过滤器,那么我们如何过滤呢?

php - 将计算列添加到 Laravel Eloquent 模型并对该列进行排序和/或过滤

php - 如何将用户名变量传递到第三个 php 页面?

php - 在 MySQL 和 PHP 的更新语句中使用 COALESCE 接受 "Null"值

sql - 指定的架构无效。错误 : 'System.Data.Spatial.DbGeography' which cannot be mapped to a primitive type

c# - Entity Framework 和默认值

php - sUrl 和 fUrl 应该是什么 - PayU Money iOS 集成