mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
perf(electron): add index for updates (#6951)
 Above image shows the performance on querying a 20k rows of updates table, which is super slow at 150+ms. After adding index for doc_id the performance should be greatly improved. After:  fix TOV-866
This commit is contained in:
@@ -73,6 +73,7 @@ impl SqliteConnection {
|
||||
.await
|
||||
.map_err(anyhow::Error::from)?;
|
||||
self.migrate_add_doc_id().await?;
|
||||
self.migrate_add_doc_id_index().await?;
|
||||
connection.detach();
|
||||
Ok(())
|
||||
}
|
||||
@@ -145,6 +146,25 @@ impl SqliteConnection {
|
||||
Ok(updates)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn delete_updates(&self, doc_id: Option<String>) -> napi::Result<()> {
|
||||
match doc_id {
|
||||
Some(doc_id) => {
|
||||
sqlx::query!("DELETE FROM updates WHERE doc_id = ?", doc_id)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(anyhow::Error::from)?;
|
||||
}
|
||||
None => {
|
||||
sqlx::query!("DELETE FROM updates WHERE doc_id is NULL")
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(anyhow::Error::from)?;
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_updates_count(&self, doc_id: Option<String>) -> napi::Result<i32> {
|
||||
let count = match doc_id {
|
||||
@@ -361,4 +381,17 @@ impl SqliteConnection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn migrate_add_doc_id_index(&self) -> napi::Result<()> {
|
||||
// ignore errors
|
||||
match sqlx::query("CREATE INDEX IF NOT EXISTS idx_doc_id ON updates(doc_id);")
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
Err(anyhow::Error::from(err).into()) // Propagate other errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user