简体   繁体   English

mysql表设计建议

[英]Mysql table design advice

I have a general question about MySQL database table design. 我对MySQL数据库表设计有一个一般性问题。 I have a table that contains ~ 650 thousand records, with approximately 100 thousand added per year. 我有一个表,包含约65万条记录,每年大约增加10万条记录。 The data is requested quite frequently, 1.6 times per second on average. 数据请求非常频繁,平均每秒请求1.6次。

It has the following structure right now 现在具有以下结构

id   port_id         date        product1_price    product2_price    product3_price
 1         1   2012-01-01                100.00            200.00            155.00
 2         2   2012-01-01                  NULL            150.00            255.00
 3         3   2012-01-01                300.00              NULL            355.00
 4         1   2012-01-02                200.00            250.00            355.00
 5         2   2012-01-02                400.00            230.00            255.00

Wouln't it be better to store the data in this manner? 以这种方式存储数据会更好吗?

id     port_id         date    product   price
1            1   2012-01-01          1     100
1            2   2012-01-01          1     200
1            3   2012-01-01          1     300
1            1   2012-01-02          1     240

Advantages of the alternative design: 替代设计的优点:

  • with the second design we don't have to store NULL values (if there is no such product in the port) 在第二种设计中,我们不必存储NULL值(如果端口中没有此类产品)
  • we can add new products easily - comparing to the first design, where each new product requires a new column 我们可以轻松添加新产品-与第一个设计相比,每个新产品都需要一个新列

Disadvantages of the alternative design: 替代设计的缺点:

  • The number of records will increase from 650 000 to 650 000 * number_of_products minus all NULL records; 记录数将从65万个增加到65万个*产品数量减去所有NULL记录; that will be approximately 2.1 million records. 大约有210万条记录。

In both cases we have id column as PRIMARY_KEY and UNIQUE key on combination of port_id and date . 在这两种情况下,我们在port_iddate组合上都有id列作为PRIMARY_KEY和UNIQUE键。

So the question is: which way to go? 所以问题是:走哪条路? Disk space does not matter, the speed of the queries is the most important aspect. 磁盘空间无关紧要,查询速度是最重要的方面。

Thank you for your attention. 感谢您的关注。

It seams, that will depend on definition of product table. 它接缝,这将取决于product表的定义。
If product table is statically compound of maximum three parts, then changing the current design won't help much. 如果产品表由最多三部分组成的静态组合,那么更改当前设计将无济于事。

Although the current design smells bad but that will be a business dependent analysis. 尽管当前的设计很难闻,但这将是与业务相关的分析。
BTW change must be done with caution on the side effects with product table and its usages. BTW更改必须谨慎对待产品表及其用法的副作用。

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

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