Attribute Index

Indexes are used to provide optimized query speed for entities in large data sets. Attributes are indexed by adding a simple annotation above the attribute declaration. Any kind of attribute can be indexed. Note: This will create a small overhead when persisting data.

You can download the code for this example here:

OnyxDevTools/onyx-database-samples/indexes.

  1. Define a Book entity with an indexed field genre.
    Notes:
    The indexed field must also have the @Attribute annotation as well as the @Index annotation.
  2. Create and persist some test data.

    For this example, we have created some Books with genres. Notice, we have created several books with the same genre. An index does not have to be unique.

  3. Define a Query that has a predicate to filter Books by their genre
    Notes:
    In order to optimize your query, the indexed attributes should be queries so that the cardinality is improved. It is key to organize your predicates using the lowest cost first.
    There were 3 Books with the genre of CHILDREN
  4. Kotlin Query Builder Syntax