Indexes

An object type or an abstract link may define indexes. An index is a special expression declaration which indicates to the database that, for a given set of objects or links, a particular expression must be indexed to allow for faster evaluation of queries which use that expression.

The subject of an index is either the object or the abstract link on which the index is defined. It can be referred to in the index expression as __subject__.

The simplest form of an index is one which references one or more properties directly:

Copy
type User {
    required property name -> str;
    index on (__subject__.name);
}

With the above, User lookups by the name property will be faster, as the database will not have to scan an entire set of objects sequentially to find the matching objects:

Copy
SELECT User FILTER User.name = 'Alice';

Typically the explicit __subject__ can be omitted in favor of a short-form expression, since the index always appears nested inside its subject:

Copy
type User {
    required property name -> str;
    index on (.name);
}

Indexes may be defined using an arbitrary expression that references multiple properties of the host object type:

Copy
type User {
    required property firstname -> str;
    required property lastname -> str;
    index on (str_lower(
        .firstname + ' ' + .lastname));
}

Similarly indexes may refer to the link properties if the subject is a link:

Copy
abstract link friends_base {
    property nickname -> str;
    index on (__subject__@nickname);
}

The index expression must not reference any variables other than the properties of the index subject. All functions used in the expression must not be set-returning.

There’s no need to create an index on just the link itself, as indexes are already created for links implicitly. Also, as a special case, adding the exclusive constraint to a property implicitly creates an index for it as well.

While being beneficial to the speed of queries, indexes increase the database size and make insertion and updates slower, and creating too many indexes may be detrimental.

Index SDL, DDL, and introspection.

Light
Dark
System