Creating Indexes

Indexable Field Types

Indexes represent an alternative way of retrieving table rows. Normally you retrieve table rows using the row's primary key. By creating an index, you can retrieve rows with dissimilar primary key values, but which share some other characteristic.

Indexes can be created on any field which is an indexable datatype, including primary key fields. See Indexable Field Types for information on the types of fields that can be indexed.

For example, if you had a table representing types of automobiles, the primary keys for each row might be the automobile's manufacturer and model type. However, if you wanted to be able to query for all automobiles that are painted red, regardless of the manufacturer or model type, you could create an index on the table's field that contains color information.

Note

Indexes can take a long time to create because Oracle NoSQL Database must examine all of the data contained in the relevant table in your store. The smaller the data contained in the table, the faster your index creation will complete. Conversely, if a table contains a lot of data, then it can take a long time to create indexes for it.

CREATE TABLE myInventory.itemDetails (
    itemSKU STRING,
    itemDescription STRING,
    price FLOAT,
    inventoryCount INTEGER,
    PRIMARY KEY (itemSKU)
) 

To create an index, use the CREATE INDEX statement. See CREATE INDEX for details. For example:

CREATE INDEX inventoryIdx on myInventory.itemDetails(inventoryCount)

Similarly, to remove an index, use the DROP INDEX statement. See DROP INDEX for details.

DROP INDEX inventoryIdx on myInventory.itemDetails

Be aware that adding and dropping indexes can take a long time. You might therefore want to run these operations asynchronously using the Store.executeFuture() method.

...
// Store handle configuration and open skipped for brevity
...

store.on('open', function () {
    console.log('Store opened');

    store.executeFuture('CREATE INDEX inventoryIdx on ' +
                  'myInventory.itemDetails(inventoryCount)',
      function(err){
        if (err)
          throw err;
        else {
           console.log('Index creation succeeded.');
           store.close();
        }
      });
    }).on('close', function() {
        console.log('Store closed.');
    }).on('error', function(error) {
        console.log(error);
    });
store.open();

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.

  • Integer

  • Long

  • Float

  • Double

  • String

  • Enum

  • Array

    In the case of arrays, the field can be indexed only if the array contains values that are of one of the other indexable scalar types. For example, you can create an index on an array of Integers. You can also create an index on a specific record in an array of records. Only one array can participate in an index, otherwise the size of the index can grow exponentially because there is an index entry for each array entry.

  • Maps

    As is the case with Arrays, you can index a map if the map contains scalar types, or if the map contains a record that contains scalar types.

  • Records

    Like Arrays and Maps, you can index fields in an embedded record if the field contains scalar data.

See Indexing Non-Scalar Data Types for examples of how to index supported non-scalar types.