在MySQL表中存储历史价格表的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-23 23:20:44
本文介绍了在MySQL表中存储历史价格表的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本上,我的问题是-我有一个价格清单,其中一些是历史价格(即我希望能够搜索到产品X在3月11日价格为0.99美元,在4月1日价格为1.99美元,依此类推...) .存储此信息的最佳方法是什么?

Basically, my question is this - I have a list of prices, some of which are historical (i.e. I want to be able to search that product X was $0.99 on March 11, $1.99 on April 1, etc...). What is the best way to store this information?

我假设我可能会有一个Product表,该表具有价格表的外键.最初,我认为存储当前价格可能是最好的选择,但我想我希望能够存储历史价格数据,所以最好的存储方式是存储一个类似于以下价格表的表: /p>

I assumed I would probably have a Product table that has a foreign key to a price table. I initially thought that storing the current price would probably be the best bet, but I think I want to be able to store historical price data, so would the better route to go be to store a table like the following for the price list:

CREATE TABLE prices ( id BIGINT auto_increment not null, primary key (id), price DECIMAL(4,2) not null, effectiveStartDate DATETIME NOT NULL, effectiveEndDate DATETIME );

我在这里有点茫然.我希望能够高效地搜索产品,并查看该产品的价格随时间变化的情况.如何有效地将一组这些价格与产品相关联?我想我要问的是,为了对跨越特定日期集的查询提供有效的搜索,对它进行索引的最佳方法是什么?"

I'm at a bit of a loss here. I'd like to be able to search products efficiently and see how the price of that product changed over time. How can I efficiently associate a set of these prices with a product? I guess what I am asking is, 'What would be the best way to index this in order to be able to provide an efficient search for queries that span a specific set of dates?'

推荐答案

将历史数据的需求与当前价格的需求分开.这意味着:

Separate the need for historical data from the need for current price. This means:

1)将当前价格保存在产品表中.

1) Keep the current price in the products table.

2)价格更改时,将新价格插入带有开始日期的历史记录表中.您实际上不需要结束日期,因为您可以从上一行获取结束日期. (您仍然可以将其放入,这使查询更加容易)

2) When the price changes, insert the new price into the history table with only the start date. You don't really need the end date because you can get it from the previous row. (You can still put it in, it makes querying easier)

还请记住,您的订单历史记录提供了另一种历史记录,即随着时间的推移以给定价格进行的实际购买.

Also remember that your order history provides another kind of history, the actual purchases at a given price over time.

更多推荐

在MySQL表中存储历史价格表的最佳方法是什么?

本文发布于:2023-11-22 03:28:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615807.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价格表   方法   历史   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!