将所有这些数据存储在数据库中的最佳方式是什么?

What's the best way to store all these data in db?

我的客户给了我大约 14k 个各种产品的 url,他希望我存储该产品每天的所有价格变化。我认为这将需要大量的数据库存储和大量优化。我以前从未这样做过。我正在使用 mysql 数据库。我应该将每个产品的所有这些价格变化存储在 JSON 列中还是作为单独的行?寻找有关此的提示。谢谢!

JSON 列的效率不如普通的 SQL 列,应该在您不确定要拥有哪些数据时保留。你很确定你将拥有什么数据。

这是一个非常简单的两个 table 架构。 1 个 table 用于产品,1 个用于它的价格变化。

create table product (
    id integer primary key auto_increment,
    name varchar,
    url varchar unique,
    ...any other information about the product you might want to store...

    index(url)
);

通过给它一个主键,它使您免受 URL 更改的影响,并且它减少了必须存储在引用它的 table 中的数量。他们只需要存储整数主键,而不是整个URL。 URL 已编入索引以加快搜索速度。

现在您有一个产品 table 其他 table 可以参考它。就像 table 的价格变化。

create table product_price_changes (
    product_id integer references product(id),
    price numeric(9,2) not null,
    change_time datetime not null,

    index(change_time)
);

此 table 存储产品价格何时发生变化,以及该价格是多少。这就是如何将数据列表附加到 SQL 中的事物。 change_time 已编入索引以加快搜索速度。

一个简单的联接让您可以高效地按顺序查看特定产品的所有更改。

select price, change_time
from product_price_changes ppc
join product prod on ppc.product_id = prod.id
where prod.url = ?
order by change_time