use unlikely() hint instead of forcing an index

This commit is contained in:
link2xt
2025-03-01 16:10:54 +00:00
parent 7ef3884ced
commit 59dce259b3

View File

@@ -1313,11 +1313,13 @@ impl Message {
/// UI can use this to show a symbol beside the message, indicating it was saved. /// UI can use this to show a symbol beside the message, indicating it was saved.
/// The message can be un-saved by deleting the returned message. /// The message can be un-saved by deleting the returned message.
pub async fn get_saved_msg_id(&self, context: &Context) -> Result<Option<MsgId>> { pub async fn get_saved_msg_id(&self, context: &Context) -> Result<Option<MsgId>> {
// We force usage of `msgs_index5` here // We force usage of `msgs_index5`
// which indexes `msgs.starred` column. // which indexes `msgs.starred` column
// using `unlikely()` here.
// //
// Otherwise with SQLite 3.49.1 // Otherwise with SQLite 3.49.1
// if you run ANALYZE on the database, // if you run ANALYZE on the database
// that has no starred messages,
// query planner decides not to use the index: // query planner decides not to use the index:
// //
// sqlite> EXPLAIN QUERY PLAN SELECT id FROM msgs WHERE starred=? AND chat_id!=?; // sqlite> EXPLAIN QUERY PLAN SELECT id FROM msgs WHERE starred=? AND chat_id!=?;
@@ -1335,13 +1337,13 @@ impl Message {
// as of 2025-02-28, ANALYZE is supposed to improve performance // as of 2025-02-28, ANALYZE is supposed to improve performance
// and this demonstrates a bug in the query planner. // and this demonstrates a bug in the query planner.
// //
// Note that usage of INDEXED BY is generally discouraged, // See <https://sqlite.org/queryplanner-ng.html#howtofix>
// see <https://sqlite.org/lang_indexedby.html> // and <https://sqlite.org/lang_corefunc.html#unlikely>
// and <https://sqlite.org/queryplanner-ng.html#howtofix>. // for details.
let res: Option<MsgId> = context let res: Option<MsgId> = context
.sql .sql
.query_get_value( .query_get_value(
"SELECT id FROM msgs INDEXED BY msgs_index5 WHERE starred=? AND chat_id!=?", "SELECT id FROM msgs WHERE unlikely(starred=?) AND chat_id!=?",
(self.id, DC_CHAT_ID_TRASH), (self.id, DC_CHAT_ID_TRASH),
) )
.await?; .await?;