CREATE INDEX

To add an index definition to the store, use a CREATE INDEX statement. It can be used to create simple indexes and multi-key indexes. It can also be used to create JSON indexes.

Indexable Field Types

Fields can be indexed only if they are declared to be one of the following types. For all complex types (arrays, maps, and records), the field can be indexed if the ultimate target of the index is a scalar datatype. So a complex type that contains a nested complex type (such as an array of records, for example) can be indexed if the index’s target is a scalar datatype contained by the embedded record.

Simple Indexes

An index is simple if it does not index any maps or arrays. To create a simple index:

CREATE INDEX [IF NOT EXISTS] index-name ON table-name (path_list)

where:

For example, if table Users has a field called lastName, then you can index that field with the following statement:

CREATE INDEX surnameIndex ON Users (lastName)

Note that depending on the amount of data in your store, creating indexes can take a long time. This is because index creation requires Oracle NoSQL Database to examine all the data in the store.

Multi-Key Indexes

Multi-key indexes are used to index all the elements of an array. They are also used to index all of the elements and/or values of a map.

For each table row, a multi-key index contains as many entries as the number of elements/entries in the array/map that is being indexed (although duplicate entries are not represented in the index). To avoid an explosion in the number of index entries, only one array/map may be contained in a single multi-key index.

To create a multi-key index, use one of the following forms:

CREATE INDEX [IF NOT EXISTS] index-name ON table-name (name-path.keys())

or

CREATE INDEX [IF NOT EXISTS] index-name ON table-name (name-path.values())

or

CREATE INDEX [IF NOT EXISTS] index-name ON table-name \
(name-path.keys(),name-path.values())

or

CREATE INDEX [IF NOT EXISTS] index-name ON table-name (name-path[])

The syntax shown, above, is identical to that described in Simple Indexes, with the following additions:

For each of the previously identified forms, a comma-separated list of name-paths may be provided. Some restrictions apply.

Multi-Key Index Restrictions

The following restrictions apply to multi-key indexes:

JSON Indexes

An index is a JSON index if it indexes at least one field that is contained inside JSON data.

Because JSON is schema-less, it is possible for JSON data to differ in type across table rows. However, when indexing JSON data, the data type must be consistent across table rows or the index creation will fail. Further, once or more JSON indexes have been created, any attempt to write data of an incorrect type will fail.

Indexing JSON data and working with JSON indexes is performed in much the same way as indexing non-JSON data. To create the index, specify a path to the JSON field using dot notation.

When creating JSON indexes, you must specify the data’s type, using the AS keyword. The data type must be atomic, and cannot be a float. That is, only integer, long, double, number, string, and boolean are supported types for JSON indexes. Note that arrays and maps can be indexed so long as they contain these atomic values.

CREATE INDEX [IF NOT EXISTS] index-name ON table-name \
(JSONRow.JSONField AS data_type)

When creating a multi-key index on a JSON map, a type must not be given for the .keys() expression because the type will always be String. However, a type declaration is required for the .values() expression. Beyond that, all the constraints described in Multi-Key Index Restrictions also apply to a JSON multi-keyed index.

CREATE INDEX [IF NOT EXISTS] index-name ON table-name \
(JSONRow.JSONField.keys(),\
    JSONRow.JSONField.values() AS data_type)

For an example of using JSON indexes, see Indexing JSON Fields.

For additional examples of using JSON indexes, see Indexing JSON Data in the SQL Beginner’s Guide.