存储和使用动态属性

发布于 2024-10-14 18:47:38 字数 551 浏览 1 评论 0原文

因此,我正在开发一个电子商务应用程序,我的客户希望能够创建类别和产品......显然。现在,假设客户有大约 100 个类别和 20,000 个产品。

客户端需要能够创建对过滤有意义的类别属性......因此,硬盘驱动器类别可能具有以下属性:

  • 容量
  • RPM
  • 外形尺寸

而投影仪类别可能具有以下属性:

  • 亮度
  • 对比度
  • 原始分辨率

我的问题是如何解决允许动态创建自定义属性,但能够使用这些属性进行搜索、过滤和报告的问题?

为每个类别创建单独的表和对象是不可能的,因为我不知道它们将创建什么类别(即 HardDriveProperties、ProjectorProperties)。

我想也许我可以在数据库中创建一个额外的列并将自定义属性序列化为 JSON,但这仍然需要我为每个类别创建一个特定的属性对象以将 JSON 反序列化为 ProductProperties 的通用列表... .我认为使用它可能会非常昂贵。

其他人是如何解决这个问题的?

So, I'm working on an eCommerce application and my customer wants the ability to create categories and products...obviously. Now, lets say the client will have about 100 categories and 20,000 products.

The client needs the ability to create category properties that make sense for filtering....so, the category Hard Drives might have properties like:

  • Capacity
  • RPMs
  • Form Factor

While the category Projectors might have properties like:

  • Brightness
  • Contrast Ratio
  • Native Resolution

My question is how does one solve the problem of allowing custom properties to be created dynamically, but the ability to use those properties to search, filter and report on?

Creating a seperate table and object for each category isn't possible since I have no idea what categories they will create (i.e. HardDriveProperties, ProjectorProperties).

I thought maybe I could create an extra column in the database and serialize the custom properties as JSON, but that would still require me to either create a specific properties object for each category to deserialize the JSON into or a generic list of ProductProperties....which I think could be pretty expensive to work with.

How are others solving this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城旧梦 2024-10-21 18:47:39

Entity-Attribute-Value 模型(又名“开放架构”)是一种方法来解决这个问题。

该模式的本质是将列变成行。而不是看起来像这样的 HardDrive(和 Projector)表:

HardDrive(HardDriveID, Capacity, RPMs, FormFactor)
------------------    
1 1TB 7200 External

您有 CategoryCategoryPropertiesCategoryPropertyValues 表:

Category(CategoryID, Description)
--------
1 Hard Drive
2 Projector

CategoryProperties(CategoryPropertyID, CategoryID, Description)
------------------
1 1 Capacity
2 1 RPMs
3 1 FormFactor
4 2 Brightness
5 2 Contrast Ratio
... etc.

CategoryPropertyValues(ItemID, CategoryPropertyID, PropertyValue)
------------------
1 1 1TB
1 2 7200
1 3 External

The Entity-Attribute-Value model (aka 'open schema') is one way to solve this problem.

The essence of the pattern is that you make columns into rows. Instead of HardDrive (and Projector) tables that look like this:

HardDrive(HardDriveID, Capacity, RPMs, FormFactor)
------------------    
1 1TB 7200 External

You have Category, CategoryProperties, and CategoryPropertyValues tables:

Category(CategoryID, Description)
--------
1 Hard Drive
2 Projector

CategoryProperties(CategoryPropertyID, CategoryID, Description)
------------------
1 1 Capacity
2 1 RPMs
3 1 FormFactor
4 2 Brightness
5 2 Contrast Ratio
... etc.

CategoryPropertyValues(ItemID, CategoryPropertyID, PropertyValue)
------------------
1 1 1TB
1 2 7200
1 3 External
谈场末日恋爱 2024-10-21 18:47:39

创建一个 Product 表,其中包含 ProductID 列。
创建一个 ProductAttribute 表,其中包含以下列:Product(引用的 ProductID)、Name、Value。

我看到的一个网站有几个“值”列,一个文本和一个数字,以便可以按一系列数值进行有效搜索。

Create a table Product which has a column ProductID.
Create a table ProductAttribute which has these columns: Product (referenced ProductID), Name, Value.

One site I saw had several Value columns, one text and one number, to allow efficiently searching by a range for numeric values.

说不完的你爱 2024-10-21 18:47:39

假设您使用关系数据库,我可能会使用类似这样的内容:

Category(Id, Name)
Property(Id, CategoryId, Description)
Product(Id, CategoryId, Name)
ProductsProperties(Id, ProductId, CategoryId, Value)

这样,您可以定义在添加/编辑属于特定类别的产品时哪些属性应该可用,并且产品可以具有无限的属性。

这比仅仅序列化 JSON 值更好,因为搜索/过滤序列化值可能非常痛苦。但是,这种方法确实使用相对规范化的数据库结构,因此如果您有很多产品、属性等,全文搜索可能会很慢。

Assuming that you use a relational database, I would probably use something like in the lines of this:

Category(Id, Name)
Property(Id, CategoryId, Description)
Product(Id, CategoryId, Name)
ProductsProperties(Id, ProductId, CategoryId, Value)

That way, you can define which properties should be available when adding/editing a product that belongs to a specific category and a product could have inifinite properties.

This would be better than just serializing JSON values, because searching/filtering for the serialized values may be really painful. However, this approach does use a relatively normalized database structure and thus may might be slow for a full-text search if you have a lot of products, properties, ...

娜些时光,永不杰束 2024-10-21 18:47:39

我认为你的最后一段将是我要走的路线。我主要关心的是大量记录的序列化(反序列化)的性能。

I think that your last paragraph would be the route that I would go. My main concern would be with the performance of (de)serialization of large numbers of records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文