use sqlite grouping rather than subquery (#181)
This commit is contained in:
parent
d36ff138ab
commit
8d215060a1
1 changed files with 18 additions and 4 deletions
|
@ -296,10 +296,8 @@ impl Database for Sqlite {
|
||||||
format!(
|
format!(
|
||||||
"select * from history h
|
"select * from history h
|
||||||
where command like ?1 || '%'
|
where command like ?1 || '%'
|
||||||
and timestamp = (
|
group by command
|
||||||
select max(timestamp) from history
|
having max(timestamp)
|
||||||
where h.command = history.command
|
|
||||||
)
|
|
||||||
order by timestamp desc {}",
|
order by timestamp desc {}",
|
||||||
limit.clone()
|
limit.clone()
|
||||||
)
|
)
|
||||||
|
@ -326,6 +324,7 @@ impl Database for Sqlite {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
async fn new_history_item(db: &mut impl Database, cmd: &str) -> Result<()> {
|
async fn new_history_item(db: &mut impl Database, cmd: &str) -> Result<()> {
|
||||||
let history = History::new(
|
let history = History::new(
|
||||||
|
@ -427,4 +426,19 @@ mod test {
|
||||||
results = db.search(None, SearchMode::Fuzzy, "").await.unwrap();
|
results = db.search(None, SearchMode::Fuzzy, "").await.unwrap();
|
||||||
assert_eq!(results.len(), 2);
|
assert_eq!(results.len(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
|
async fn test_search_bench_dupes() {
|
||||||
|
let mut db = Sqlite::new("sqlite::memory:").await.unwrap();
|
||||||
|
for _i in 1..10000 {
|
||||||
|
new_history_item(&mut db, "i am a duplicated command")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
let start = Instant::now();
|
||||||
|
let _results = db.search(None, SearchMode::Fuzzy, "").await.unwrap();
|
||||||
|
let duration = start.elapsed();
|
||||||
|
|
||||||
|
assert!(duration < Duration::from_secs(15));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue