0

我想创建一个表,表的名称是日期。当我收集那天的股票数据时,我想像这样存储它:

$date = date('Y-m-d');
$mysqli->query(
    "CREATE TABLE IF NOT EXISTS `$date`(ID INT Primary Key)"
);

这样我就会有一个像这样的数据库:

2013-5-1: AAPL | 400 | 400K
          MFST | 30  | 1M
          GOOG | 700 | 2M
2013-5-2: ...

我认为存储这样的信息会更容易,但我看到与此类似的问题已关闭。

如何将日期添加到 MySQL 表名?

“生成越来越多的表与“保持数据库清洁”完全相反。一个干净的数据库是一个具有合理、规范化、固定模式的数据库,您可以对其运行查询。

如果这不是正确的方法,有人可以提出建议吗?许多人评论这个问题表示这不是一个“干净”的解决方案?

4

1 回答 1

1

不要将数据拆分为多个表。这将成为维护的噩梦,尽管一开始这样做似乎很明智。

我建议您创建一个日期列,其中包含您当前要放入表名中的信息。数据库在有效存储日期方面非常聪明。只要确保使用正确的数据类型,而不是字符串。通过向该列添加索引,您在查询时也不会受到性能损失。

What you gain is full flexibility in querying. There will be virtually no limits to the data you can extract from a table like this. You can join with other tables based on date ranges etc. This will not be possible (or at least much more complicated and cumbersome) when you split the data by date into tables. For example, it will not even be easy to just get the average of some value over a week, month or year.

If - and that's depending on the real amount of data you will collect - some time in the future the data grows dramatically, to more than several million rows I would estimate - you can have a look at the data partitioning features MySQL offers out of the box. However, I would not advise to use them immediately, unless you already have a clearly cut growth model for the data.

In my experience there is very seldom a real need for this technique in most cases. I have worked with tables in the 100s of gigabytes range, with tables having millions of rows. It is all a matter of good indexing and carefully crafted queries when the data gets huge.

于 2013-05-02T22:19:42.433 回答