I have an app that uses the Realm Swift SDK and is synced to the cloud via MongoDB Atlas Device Sync. It has 900,000 model objects like this in the database:
final class Foo: Object
{
@Persisted var name: String = ""
}
Can I add an index to this property without any schema violations, migrations, or sync failures? Can I just change the declaration to this and have an index created?
@Persisted(indexed: true) var name: String = ""
If this does not index the property on existing objects, what is the process for adding an index to those? (The end goal, of course, is to speed up search.)
The app builds and runs just fine if I change the declaration, but because indexes are created/updated on insert, I'm not sure that merely changing the declaration actually does anything for existing objects in the database.
I did ask this question on the MongoDB forums first, but got no response.
Indexes are automatically created or destroyed when the Realm is opened. There's no ceremony around them because unlike with other schema changes, there's no potential for data loss. If you accidentally remove a property and then restore it in a later version the data for that property is just gone, while if you accidentally remove an index and then restore it everything is fine.
Indexes are a purely local concept that don't interact with sync in any way.
You should be able to measure a performance difference in queries by toggling the index on and off and making no other changes. If nothing does change, this suggests that you don't have any queries which benefit from an index.