Indexes​

An index is a data structure used internally by a database to speed up filtering and sorting operations. Most commonly, indexes are declared within object type declarations and reference a particular property; this will speed up any query that references that property in a filter or order by clause.

While improving query performance, indexes also increase disk and memory usage and slow down insertions and updates. Creating too many indexes may be detrimental; only index properties you often filter or order by.

Index on a property​

Below, we are referencing the User.name property with the dot notation shorthand: .name.

  1. type User {
  2. required property name -> str;
  3. index on (.name);
  4. }

By indexing on User.name, queries that filter by the name property will be faster, as the database can lookup a name in the index instead of scanning through all Users sequentially.

Index on an expression​

Indexes may be defined using an arbitrary singleton expression that references multiple properties of the enclosing object type.

A singleton expression is an expression that’s guaranteed to return at most one one element. As such, you can’t index on a multi property.

  1. type User {
  2. required property first_name -> str;
  3. required property last_name -> str;
  4. index on (str_lower(.firstname + ' ' + .lastname));
  5. }

Index on multiple properties​

A composite index is an index that references multiple properties. This will speed up queries that filter or sort on both properties. In EdgeDB, this is accomplished by indexing on a tuple of properties.

  1. type User {
  2. required property name -> str;
  3. required property email -> str;
  4. index on ((.name, .email));
  5. }

Link properties can also be indexed.

  1. abstract link friendship {
  2. property strength -> float64;
  3. index on (__subject__@strength);
  4. }
  5. type User {
  6. multi link friends extending friendship -> User;
  7. }

Annotate an index​

Indexes can be augmented with annotations.

  1. type User {
  2. property name -> str;
  3. index on (.name) {
  4. annotation description := 'Indexing all users by name.';
  5. };
  6. }

Foreign and primary keys

In SQL databases, indexes are commonly used to index primary keys and foreign keys. In EdgeDB, these fields are automatically indexed; there’s no need to manually declare them. Moreover, any property with an exclusive constraint is also automatically indexed.

See also

SDL > Indexes

DDL > Indexes

Introspection > Indexes