fix: add time index, order-by time desc
This commit is contained in:
parent
aad20c8637
commit
599358623e
1 changed files with 4 additions and 4 deletions
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue