简体   繁体   中英

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 )
  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..). 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. Another iPhone 6 16 gb black sold by another seller may have different attributes.

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. So an item could be related to 0, 1 or more optional attributes.

We are working on two kind of approaches to store optional values for each item, but both could bring problems.

Case A

Create a new table called item_additional_attr where each record will represents an additional attribute for a single item. There will be a one-to-many relationship between items and item_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. Of course that will be a huge table.

Case B

Create a new field type TEXT or BLOB into item_attr called optional_attributes . This field will contains an array of optional attributes and will be handled in PHP. Of course the array will be stored as serialized or json encoded. I think this kind of approach could bring problems with some queries, but it could be handled without problems in PHP.

I'm giving priority to webserver/db performance, but I would also avoid problems with queries. 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. 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. The third table list items' attributes of type 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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