{"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;