From 8727e0acf892aad6d78705111b61cb12503ce75f Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 2 Sep 2020 16:55:40 +0200 Subject: [PATCH 1/2] add an index to significantly speed up get_fresh_msg_cnt() --- src/chat.rs | 10 ++++++++++ src/sql.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/chat.rs b/src/chat.rs index 7d025cb6f..438aa010b 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -375,6 +375,16 @@ impl ChatId { } pub async fn get_fresh_msg_cnt(self, context: &Context) -> usize { + // this function is typically used to show a badge counter beside _each_ chatlist item. + // to make this as fast as possible, esp. on older devices, we added an combined index over the rows used for querying. + // so if you alter the query here, you may want to alter the index over `(state, hidden, chat_id)` in `sql.rs`. + // + // the impact if the index is significant once the database grows: + // - on an older android4 with 18k messages, query-time decreased from 110ms to 2ms + // - on an mid-class moto-g or iphone7 with 50k messages, query-time decreased from 26ms or 6ms to 0-1ms + // the times are average, no matter if there are fresh messages or not - + // and have to be multiplied by the number of items shown at once on the chatlist, + // so savings up to 2 seconds are possible on older devices - newer ones will feel "snappier" :) context .sql .query_get_value::( diff --git a/src/sql.rs b/src/sql.rs index 70d7d8d2c..effd41239 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1315,6 +1315,16 @@ async fn open( } sql.set_raw_config_int(context, "dbversion", 67).await?; } + if dbversion < 68 { + info!(context, "[migration] v68"); + // the index is used to speed up get_fresh_msg_cnt(), see comment there for more details + sql.execute( + "CREATE INDEX IF NOT EXISTS msgs_index7 ON msgs (state, hidden, chat_id);", + paramsv![], + ) + .await?; + sql.set_raw_config_int(context, "dbversion", 68).await?; + } // (2) updates that require high-level objects // (the structure is complete now and all objects are usable) From 8d9fa233c578255437d6c94c66fd43b02c8c3ec8 Mon Sep 17 00:00:00 2001 From: bjoern Date: Wed, 2 Sep 2020 21:36:12 +0200 Subject: [PATCH 2/2] Update src/chat.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Asiel Díaz Benítez --- src/chat.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat.rs b/src/chat.rs index 438aa010b..c2ccf5699 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -379,7 +379,7 @@ impl ChatId { // to make this as fast as possible, esp. on older devices, we added an combined index over the rows used for querying. // so if you alter the query here, you may want to alter the index over `(state, hidden, chat_id)` in `sql.rs`. // - // the impact if the index is significant once the database grows: + // the impact of the index is significant once the database grows: // - on an older android4 with 18k messages, query-time decreased from 110ms to 2ms // - on an mid-class moto-g or iphone7 with 50k messages, query-time decreased from 26ms or 6ms to 0-1ms // the times are average, no matter if there are fresh messages or not -