Indexing your Data
Introduction
Querying documents using a pre-existing database index is much faster because an index narrows down the set of documents to examine — see: the Query Troubleshooting topic.
When planning the indexes you need for your database, remember that while indexes make queries faster, they may also:
-
Make writes slightly slower, because each index must be updated whenever a document is updated
-
Make your Couchbase Lite database slightly larger.
Too many indexes may hurt performance. Optimal performance depends on designing and creating the right indexes to go along with your queries.
|
Constraints
Couchbase Lite for net does not currently support partial value indexes; indexes with non-property expressions.
You should only index with properties that you plan to use in the query.
|
Creating a new index
You can use SQL++ or QueryBuilder syntaxes to create an index
Example 2 creates a new index for the type and name properties, shown in this data model:
{
"_id": "hotel123",
"type": "hotel",
"name": "The Michigander",
"overview": "Ideally situated for exploration of the Motor City and the wider state of Michigan. Tripadvisor rated the hotel ...",
"state": "Michigan"
}
SQL++
The code to create the index will look something like this:
string[] indexProperties = new string[] { "type", "name" };
var config = new ValueIndexConfiguration(indexProperties);
collection.CreateIndex("TypeNameIndex", config);
QueryBuilder
| See the QueryBuilder topic to learn more about QueryBuilder. |
The code to create the index will look something like this:
// For value types, this is optional but provides performance enhancements
var index = IndexBuilder.ValueIndex(
ValueIndexItem.Expression(Expression.Property("type")),
ValueIndexItem.Expression(Expression.Property("name"))); (1)
collection.CreateIndex("TypeNameIndex", index);