BlueBubblesApp / bluebubbles-app

A cross-platform app ecosystem, bringing iMessage to Android, PC (Windows, Linux, & even macOS), and Web!
https://bluebubbles.app
Apache License 2.0
674 stars 92 forks source link

Add index to Message model to speed up queries? #2632

Open gsxdsm opened 6 months ago

gsxdsm commented 6 months ago

I ran a quick profiling of the Android app after noticing a lot of cpu/battery use when loading chats - about 50% of the CPU time was spent in Chat.getMessages. I took a look at the model (io/message.dart) and noticed there wasn't an index on the id or guid - maybe I'm missing something (not super familiar with ObjectBox), but I'm thinking an index here could improve query performance?

I'll run a few more tests locally but wanted to see if this was already investigated or I missed anything.

Thanks!

gsxdsm commented 6 months ago

So it looks like the index doesn't really matter - but Chat.latestMessage (chat.dart:309) appears to be a very expensive query

zlshames commented 3 months ago

I think we need to add the index on the created date column.

Since the date created date is ultimately what we query most by, we can annotate the model with @Index to make queries faster: https://docs.objectbox.io/entity-annotations

Right here: https://github.com/BlueBubblesApp/bluebubbles-app/blob/d0257c9080e82140602340b48f85fa148721553c/lib/models/io/message.dart#L241

We can also just do this to any column in the DB that we tend to use when querying. It uses more RAM, but increases read speeds