Hi,
I’m struggling to understand how indexes work in CBL.
I have the following indexes :
database.createIndex("type", IndexBuilder.valueIndex(ValueIndexItem.property("type")));
database.createIndex("medicalFile",
IndexBuilder.valueIndex(
ValueIndexItem.expression(Expression.property("type").equalTo(Expression.string("pulseMedicalFile")))
) );
database.createIndex("chat",
IndexBuilder.valueIndex(
ValueIndexItem.expression(Expression.property("type").equalTo(Expression.string("pulseChat")))));
database.createIndex("chatMessages",
IndexBuilder.valueIndex(
ValueIndexItem.expression(Expression.property("type").equalTo(Expression.string("pulseChatMessage")))));
Then, when I use query.explain(), I can see CBL is using only the index named “type”, even when I have something like
Query query = QueryBuilder
.select(SelectResult.expression(Expression.property("id")),
SelectResult.expression(Expression.property("type")),
SelectResult.expression(Expression.property("chaId")),
SelectResult.expression(Expression.property("usrId")),
SelectResult.expression(Expression.property("msgAuthor")),
SelectResult.expression(Expression.property("msgType")),
SelectResult.expression(Expression.property("msgRecordingDate")),
SelectResult.expression(Expression.property("msgValue")),
SelectResult.expression(Expression.property("msgDocumentType")),
SelectResult.expression(Expression.property("preId")))
.from(DataSource.database(database))
.where(
Expression.property("type").equalTo(Expression.string("pulseChatMessage"))
.and(Expression.property("chaId").equalTo(Expression.string(chaId)))
.and(Expression.property("sysActive").equalTo(Expression.booleanValue(true)))
)
.orderBy(Ordering.expression(Expression.property("msgRecordingDate")).ascending())
.limit(Expression.intValue(500));
I don’t understand what index I need to create for this request to be optimized.
Then I tried
database.createIndex("chatMessages2",
IndexBuilder.valueIndex(ValueIndexItem.property("type"),
ValueIndexItem.expression(Expression.property("type").equalTo(Expression.string("pulseChatMessage")))));
Now, every request using “where type = ‘…’” is using this index.
Can anyone give me some advice ? thanks.