简体   繁体   English

RDBMS-存储与一条记录相关的多个键值的最佳方法

[英]RDBMS - Best way to store several key value related to one record

I'm working on a website which will be like a marketplace where a registered seller could sell different kind of items. 我正在一个网站上工作,该网站就像一个市场,注册卖家可以出售其他种类的商品。 For each item there are common attributes and optional attributes. 对于每个项目,都有公共属性和可选属性。 Take a look to the following, I'll try to explain. 看下面的内容,我会尽力解释。

Scenario 脚本

  1. The seller add a new item (eg iPhone 6 16 gb black ) 卖方添加新商品(例如, iPhone 6 16 GB黑色
  2. He builds the insertion specifying item attributes (eg price , shipping price , condition , images , description , etc..). 他构建指定项目属性的插入(例如, 价格运输价格条件图像描述等)。 This kind of attributes are required and common for any item. 对于任何项目,这种属性都是必需的和通用的。
  3. Once all required attributes are filled, the seller have the ability to specify other kind of attributes that are related only with that item (eg RAM , capacity , size , weight , model year , OS , number of cores , etc..). 填写完所有必需的属性后,卖方便可以指定仅与该商品相关的其他类型的属性(例如, RAM容量大小重量型号年份OS核心数等)。 This kind of attributes are optional. 这种属性是可选的。 The seller specify key (eg capacity ) and value (eg 16 gb ) and them are related only for that single item. 卖方指定密钥(例如容量 )和值(例如16 gb ),并且它们仅与该单个项目相关。 Another iPhone 6 16 gb black sold by another seller may have different attributes. 另一位卖家出售的另一款iPhone 6 16 GB黑色可能具有不同的属性。

Actually we have a table called items which contains all the items for sale, and another table called item_attr which contains common item attributes. 实际上,我们有一个名为items的表,其中包含所有待售商品,还有一个名为item_attr的表,其中包含常见商品属性。 So an item could be related to 0, 1 or more optional attributes. 因此,一项可能与0、1或多个可选属性相关。

We are working on two kind of approaches to store optional values for each item, but both could bring problems. 我们正在研究两种方法来存储每个项目的可选值,但这两种方法都可能带来问题。

Case A 案例A

Create a new table called item_additional_attr where each record will represents an additional attribute for a single item. 创建一个名为item_additional_attr的新表,其中每个记录将代表单个项的附加属性。 There will be a one-to-many relationship between items and item_additional_attr . itemitem_additional_attr之间将存在一对多关系 This seems to be the most "database-friendly" solution, but I'm worried about the size of this table could have. 这似乎是最“数据库友好”的解决方案,但我担心此表的大小可能会如此。 If items contains 100.000 records and each item is related to an average of 5 optional attributes, item_additional_attr will contains 500.000 records. 如果项目包含100.000条记录,并且每个项目平均与5个可选属性相关,则item_additional_attr将包含500.000条记录。 Of course that will be a huge table. 当然,那将是一个巨大的表。

Case B 情况B

Create a new field type TEXT or BLOB into item_attr called optional_attributes . item_attr中创建一个名为optional_attributes的新字段类型TEXTBLOB This field will contains an array of optional attributes and will be handled in PHP. 该字段将包含可选属性数组,并将在PHP中进行处理。 Of course the array will be stored as serialized or json encoded. 当然,该数组将以序列化或json编码的形式存储。 I think this kind of approach could bring problems with some queries, but it could be handled without problems in PHP. 我认为这种方法可能会给某些查询带来问题,但可以在PHP中毫无问题地进行处理。

I'm giving priority to webserver/db performance, but I would also avoid problems with queries. 我优先考虑webserver / db的性能,但同时也避免了查询问题。 Moreover additional attributes will be used only to show technical specs in a table, never for filtering/sorting. 此外,附加属性将仅用于显示表格中的技术规格,而不会用于过滤/排序。 So, in your opinion, what is the best way to achieve that? 那么,您认为实现此目标的最佳方法是什么?

You may want to try using EAVs (entity attribute value) tables. 您可能想尝试使用EAV(实体属性值)表。 Basically you will maintain several tables. 基本上,您将维护几个表。 One table should store the list of items. 一个表应存储项目列表。 Other tables should maintain attributes that all have similar data types. 其他表应保留所有具有相似数据类型的属性。 I created a simple schema to demonstrate: 我创建了一个简单的架构来演示:

+---------+------------+
| item_id | item_name  |
+---------+------------+
|       1 | Cell Phone |
|       2 | Shirt      |
+---------+------------+
2 rows in set (0.00 sec)

+---------+--------------+----------------+-----------------+
| item_id | attribute_id | attribute_name | attribute_value |
+---------+--------------+----------------+-----------------+
|       1 |            2 | storage        | 8GB             |
|       1 |            3 | color          | Gray            |
|       2 |            4 | size           | XL              |
|       2 |            6 | shirt_color    | Red             |
+---------+--------------+----------------+-----------------+
4 rows in set (0.00 sec)

+---------+--------------+----------------+-----------------+
| item_id | attribute_id | attribute_name | attribute_value |
+---------+--------------+----------------+-----------------+
|       1 |            2 | price          |              49 |
+---------+--------------+----------------+-----------------+
1 row in set (0.00 sec)

The first table is a list of items. 第一个表是项目列表。 The second table is a list of the items' attributes of type varchar. 第二张表是varchar类型的项目属性的列表。 The third table list items' attributes of type int. 第三张表列出了int类型的项目的属性。 This will allow a scalable database that disperses attributes to multiple tables. 这将允许可伸缩的数据库将属性分散到多个表。 The only draw back is the amount of join you will need to do in order to get an item and all of its attributes. 唯一的缺点是您需要获得一个项目及其所有属性的联接量。 A textual caching scheme could be used via php in order to store item information for an increase in performance. 可以通过php使用文本缓存方案,以存储项目信息以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM