开发者

MySQL Database - Performance Design

I'm currently redesign a heavy loaded website, and I would appreciate any opinion about a specific database design issue.

The concept is to keep in the db a number of products (500K of them). Every product can have a number of dynamic properties (around 1K), and every property a number of predefined but dynamic values (lets say 10 on average for every property, so around 10K)

At this point of time this is the simplified db structure:

Products (Products Table)

+--------+--------------+
| ProdID | Product Name |  
+--------+--------------+
| 1      | T-Shirt XYZ  |
+--------+--------------+
| 2      | Dress ABC    |
+--------+--------------+
| ...    | ...          |
+--------+--------------+
| 500000 | Something    |
+--------+--------------+

Properties Definition (Props Table) (it holds the Property Types)

+--------+--------------+
| PropID | Property Name|  
+--------+--------------+
| 1      | color        |
+--------+--------------+
| 2      | size         |
+--------+--------------+
| ...    | ...          |
+--------+--------------+
| 100    | Some Prop    |
+--------+--------------+

Properties Values Definition (Values Table)

+-----------+--------+-------+
| PropValID | PropID | Value |  
+-----------+--------+-------+
|         1 |      1 | red   |
+-----------+--------+-------+
|         2 |      1 | blue  |
+-----------+--------+-------+
|         3 |      2 | m     |
+-----------+--------+-------+
|         4 |      2 | xl    |
+-----------+--------+-------+
|         5 |      2 | xxl   |
+-----------+--------+-------+
| ...       | ...    | ...   |
+-----------+--------+-------+
|      1000 |    100 | xyz   |
+-----------+--------+-------+

This way we can add any number of properties and values in any product. The table below holds this info.

Product Properties & Values (ProdPropVal Table)

+--------+--------+--------+-----------+
| InfoID | ProdID | PropID | PropValID |
+--------+--------+--------+-----------+
|      1 |      1 |      1 |         1 |
+--------+--------+--------+-----------+
|      2 |      1 |      2 |         3 |
+--------+--------+--------+-----------+
|      3 |      2 |      1 |         2 |
+--------+--------+--------+-----------+
|      4 |      2 |      2 |         5 |
+--------+--------+--------+-----------+
| ...    | ...    | ...    |           |
+--------+-开发者_Python百科-------+--------+-----------+

In the example above we know that "T-Shirt XYZ" has blue color and its size is medium.

And now the tricky part... if we want to find all products that have a common property values set (all products of blue color and medium size) which is the best approach?

My ideas:

  1. Search one time the ProdPropVal Table for each PropValID and compare the results in code. This can be fine tuned by starting from the most rare PropValIDs and limiting ProdIDs using a WHERE ProdID IN (previous IDs) in the next queries.

  2. Use an Inner Join in the ProdPropVal Table for each PropValID wanted. Something like: SELECT ProdID FROM ProdPropVal ppv1 INNER JOIN ProdPropVal ppv2 ON ppv1.ProdID = ppv2.ProdID INNER JOIN ProdPropVal ppv3 ON ppv1.ProdID = ppv3.ProdID INNER JOIN ProdPropVal ppv4 ON ppv1.ProdID = ppv4.ProdID WHERE ppv1.PropValID = 10 AND ppv2.PropValID = 20 AND ppv3.PropValID = 30 AND ppv4.PropValID = 150

These are my ideas so far. The fact that ProdPropVal tablet has some millions rows doesn't leave any room for error.

Any suggestion is most welcomed!


To find all products with blue colour and medium size I would do this:

SELECT ProdID
  FROM ProdPropVal
  WHERE (PropID = 1 AND PropValID = 2)
  OR (PropID = 2 AND PropValID = 3)
  GROUP BY ProdID
  HAVING COUNT(*) = 2

Better still, if PropValID is unique in the Values table, then you would remove the PropID column from the ProdPropVal table, and simplify the query to this:

SELECT ProdID
  FROM ProdPropVal
  WHERE PropValID IN (2, 3)
  GROUP BY ProdID
  HAVING COUNT(*) = 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜