Indexes can be defined in SurrealDB for a number of ways. The main reason for an index is faster data retrieval. This is used most often when you need to use the WHERE clause in a query, which results in a full table scan. But with an index, this is much faster.
CREATEpersonSETname="Billy"; -- Create 25000 more records, each with a random string for a name CREATE |person:25000| SETname=rand::string(10) RETURNNONE;
-- Lots of 'person' records to look through... SELECT * FROMpersonWHEREname="Billy";
-- Add an index DEFINEINDEXname_indexONpersonFIELDSname; -- The same query is much faster now SELECT * FROMpersonWHEREname="Billy";
Our database has nowhere near 25,000 records so an index for faster lookup times won't be necessary at this point. However, an index has other uses such as being able to ensure that records are unique. To do this, just add the keyword UNIQUE to the end.
One place we can use a unique index is the address field on the place table.
"Database index `unique_address` already contains '2025 Statement Street, Riverdale', with record `place:surreal_library`"
A unique field can be defined on multiple fields too, such as this one defined on the in and out fields of a graph edge. This is used quite often to make sure that a RELATE statement can't be used more than once on the same two records.