fix: add time index, order-by time desc

This commit is contained in:
əlemi 2024-12-03 14:39:53 +01:00
parent aad20c8637
commit 599358623e
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -25,8 +25,8 @@ impl Database {
)?; )?;
db.execute( db.execute(
"CREATE INDEX IF NOT EXISTS event_per_service "CREATE INDEX IF NOT EXISTS ordered_event_per_service
ON events (service)", ON events (service, time DESC)",
params![], params![],
)?; )?;
@ -67,7 +67,7 @@ impl Database {
pub async fn get(&self, sid: i64, limit: i64) -> rusqlite::Result<Vec<Event>> { pub async fn get(&self, sid: i64, limit: i64) -> rusqlite::Result<Vec<Event>> {
let db = self.0.lock().await; 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( let results = stmt.query_map(
named_params! { ":sid": sid, ":limit": limit }, named_params! { ":sid": sid, ":limit": limit },
|row| Ok((row.get(0)?, row.get(1)?)), |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<Option<i64>> { pub async fn up(&self, sid: i64, since: i64) -> rusqlite::Result<Option<i64>> {
let db = self.0.lock().await; 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( stmt.query_row(
named_params! { ":sid": sid, ":time": since }, named_params! { ":sid": sid, ":time": since },
|row| row.get::<usize, Option<i64>>(0) |row| row.get::<usize, Option<i64>>(0)