From 599358623ed8259e38e507a07bc872c5b50160f5 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 3 Dec 2024 14:39:53 +0100 Subject: [PATCH] fix: add time index, order-by time desc --- src/db.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db.rs b/src/db.rs index 26e8539..8a490db 100644 --- a/src/db.rs +++ b/src/db.rs @@ -25,8 +25,8 @@ impl Database { )?; db.execute( - "CREATE INDEX IF NOT EXISTS event_per_service - ON events (service)", + "CREATE INDEX IF NOT EXISTS ordered_event_per_service + ON events (service, time DESC)", params![], )?; @@ -67,7 +67,7 @@ impl Database { pub async fn get(&self, sid: i64, limit: i64) -> rusqlite::Result> { let db = self.0.lock().await; - let mut stmt = db.prepare("SELECT time, value FROM events WHERE service = :sid LIMIT :limit")?; + let mut stmt = db.prepare("SELECT time, value FROM events WHERE service = :sid LIMIT :limit ORDER BY time DESC")?; let results = stmt.query_map( named_params! { ":sid": sid, ":limit": limit }, |row| Ok((row.get(0)?, row.get(1)?)), @@ -103,7 +103,7 @@ impl Database { pub async fn up(&self, sid: i64, since: i64) -> rusqlite::Result> { let db = self.0.lock().await; - let mut stmt = db.prepare("SELECT value FROM events WHERE service = :sid AND time > :time")?; + let mut stmt = db.prepare("SELECT value FROM events WHERE service = :sid AND time > :time ORDER BY time DESC")?; stmt.query_row( named_params! { ":sid": sid, ":time": since }, |row| row.get::>(0)