{"1000001":[{"代码":1,"价格":1.661,"日期":"2023-06- 06 12:46:32","最新":1},{"代码":2,"价格":1.867,"日期":"2023-06-06 12:46:32", "最新":1},{"代码":3,"价格":1.05,"日期":"2023-06-06 12:46:32","最新":1}, {"code":5,"price":1.818,"date":"2023-06-06 12:46:32","latest":1},{"code":6, “价格”:1.879,“日期”:“2023-06-06 12:46:32”,“最新”:1}],“1000002”:[{“代码”:1,” ;价格”:1.65,“日期”:“2023-06-03 08:53:26”,“最新”:1},{“代码”:2,“价格”:1.868,”日期”:“2023-06-03 08:53:26”,“最新”:1},{“代码”:6,“价格”:1.889,“日期”:“2023-06 -03 08:53:27","最新":1}],…</pre></p>
P粉1414555122023-09-02 11:48:59
我想您需要以下索引才能使查询良好执行(作为数据库设计的一部分,您只需执行一次)。
CREATE INDEX IX ON price (code, stationID, date DESC, price)
前两列可以按任意顺序排列。
P粉2974349092023-09-02 00:29:02
只要同一 code, stationID
对不能有两行具有相同的日期时间,使用窗口函数就有点像使用大锤敲开坚果。
select p.stationID, p.code, p.price, p.date from ( select code, stationID, max(date) as max_date from price group by code, stationID ) max join price p on max.code = p.code and max.stationID = p.stationID and max.max_date = p.date;
它需要以下索引:
alter table price add index (code, stationID, date desc);
此查询应该花费不到 1 毫秒的时间,因为可以从索引构建派生表,然后它只从表中读取所需的行。
或者,如果您知道每个 code, stationID
对都会在特定时间段(1 小时、1 天、1 周)内收到更新的价格,那么您可以显着减少工作量窗口函数需要添加一个 where 子句:
with cte as ( select stationID as ind, code, price, date, row_number() over(partition by code, stationID order by date desc) as latest from price where date >= now() - interval 1 week ) select * from cte where latest = 1;