Skip to main content

Indexes

Understanding how indexes work is essential to optimize query performance. Isar lets you choose which index you want to use and how you want to use it. We'll start with a quick introduction what indexes are.

What are indexes?#

When a collection is unindexed, the order of the rows will likely not be discernible by the query as optimized in any way, and your query will therefore have to search through the objects linearly. In other words, the queries will have to search through every object to find the objects matching the conditions. As you can imagine, this can take a long time. Looking through every single object is not very efficient.

For example, the fictional collection is completely unordered.

@Collection()
class Product {
int? id;
late String name;
late int price;
}

Data:#

idnameprice
1Book15
2Table55
3Chair25
4Pencil3
5Lightbulb12
6Carpet60
7Pillow30
8Computer650
9Soap

A query that tries to find all products that cost more than 30€ has to search through all nine rows. That's not an issue for nine rows but it will become a problem for 100k rows.

final expensiveProducts = await isar.products.where()
.filter()
.priceGreaterThan(30)
.findAll();

To improve the performance of this query we index the price propery. An index is like a sorted lookup table:

@Collection()
class Product {
int? id;
late String name;
@Index()
late int price;
}

Generated index:#

priceid
29
34
125
151
253
307
552
606
6508

Now the query can be executed a lot faster. The executer can directly jump to the last three index rows and find the corresponding objects by their id.

Sorting#

Another cool thing indexes can do is super fast sorting. Sorted queries are very expensive because the database has to load all results in memory before sorting them. Even if you specify an offset or limit because they are applied after sorting.

Let's imagine we want to find the four cheapest products. We could use the following query:

final cheapest = await isar.products.where()
.sortByPrice()
.limit(4)
.findAll();

In this example the database would have to load all (!) objects, sort them by price and return the four products with the lowest price.

As you can probably imagine, this can be done a lot more efficient with the index from before. The database takes the first four rows of the index and returns the corresponding objects.

To use the index for sorting we would write the query like this:

final cheapestFast = await isar.products.where()
.anyPrice()
.limit(4)
.findAll();

The .anyX() where clause tells Isar to use an index just for sorting. You can also use a where clause like .priceGreaterThan() and still get sorted results.

Composite indexes#

A composite index is an index on multiple properties. Isar allows you to create composite indexes that consist of up to three properties.

A composite index is also known as a multiple-column index.

It's probably best to start with an example. We create a person collection and define a composite index on the age and name properties:

@Collection()
class Person {
int? id;
late String name;
@Index(composite: [CompositeIndex('name')])
late int age;
late String hometown;
}

Data:#

idnameagehometown
1Daniel20Berlin
2Anne20Paris
3Carl24San Diego
4Simon24Munich
5David20New York
6Carl24London
7Audrey30Prague
8Anne24Paris

Generated index#

agenameid
20Anne2
20Daniel1
20David5
24Anne8
24Carl3
24Carl6
24Simon4
30Audrey7

The generated composite index contains all persons sorted by their age and then by their name.

Obviously composite indexes are great if you want to create efficient queries that are sorted by multiple properties.

But composite indexes also allow advanced where clauses with multiple properties:

final result = await isar.where()
.ageNameEqualTo(24, 'Carl')
.hometownProperty()
.findAll() // -> ['San Diego', 'London']

The last property of a composite index also supports conditions like startsWith() or lessThan():

final result = await isar.where()
.ageEqualToNameStartsWith(20, 'Da')
.findAll() // -> [Daniel, David]

Unique indexes#

A unique index ensures the index does not contain any duplicate values.

A unique index may consist of one or multiple properties. If a unique index has one property, the values in this property will be unique. In case the unique index has multiple property, the combination of values in these property is unique.

Any attempt to insert or update data into the unique index that causes a duplicate will result in an error.

Replace indexes#

A replace index is always unique. The only difference to a regular unique index is what happens if you try to insert a duplicate value. Rather than throwing an exception the replace index will replace existing objects with the same value.

Index type#

There are multiple different type for indexes:

Value index#

This is the default type and also the only allowed type for all properties that don't hold Strings. Property values are used to build the index.

Hash index#

Strings are hashed to reduce the storage required by the index. The disadvantage of hash indexes is that they can't be used for prefix scans (startsWith where clauses.

Word index#

Strings are splitted on Grapheme Clusters or word boundaries, according to the Unicode Standard Annex #29 rules and stored individually. Can be used for full-text search.