索引の作成

索引は、表の行を取得する代替方法を表します。通常は、行の主キーを使用して表の行を取得します。索引を作成することで、主キー値が異なるが、他の特性を共有する行を取得できます。

索引付けが可能なデータ型を持つすべてのフィールド(主キー・フィールドを含む)で、索引を作成できます。表のIDENTITYフィールドに索引付けできます。索引付けが可能なフィールドの型の詳細は、索引付けが可能なフィールドの型を参照してください。

たとえば、自動車のタイプを表す表がある場合、各行の主キーは自動車の製造業者およびモデル・タイプになります。ただし、製造業者またはモデル・タイプに関係なく、すべての赤色の自動車を問い合せることができるようにするには、色情報を含むフィールドに索引を作成します。

ノート:

Oracle NoSQL Databaseはストアの関連表に含まれるすべてのデータを調べる必要があるため、索引の作成には時間がかかる場合があります。表に含まれるデータが小さいほど、索引の作成が高速になります。逆に、表に多数のデータが含まれる場合、索引の作成には時間がかかる可能性があります。

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

索引を作成するには、CREATE INDEX文を使用します。CREATE INDEXを参照してください。次に例を示します。

CREATE INDEX inventoryIdx on myInventory.itemDetails(inventoryCount)

同様に、索引を削除するには、DROP INDEX文を使用します。DROP INDEXを参照してください。次に例を示します。

DROP INDEX inventoryIdx on myInventory.itemDetails

索引の追加および削除には時間がかかる可能性があることに注意してください。KVStore.execute()メソッドを使用して、索引の削除操作を非同期に実行することもできます。

package kvstore.basicExample;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import oracle.kv.ExecutionFuture;
import oracle.kv.FaultException;
import oracle.kv.StatementResult;
import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.table.TableAPI;

...
// Store open skipped
... 

public void createIndex() {
    TableAPI tableAPI = store.getTableAPI();
    ExecutionFuture future = null;
    StatementResult result = null;
    String statement = null;

    try {

        statement = "CREATE INDEX inventoryIdx on " +
                    "myInventory.itemDetails(inventoryCount)"
        future = store.execute(statement);
        displayResult(future.getLastStatus(), statement);

        /*
         * Limit the amount of time to wait for the
         * operation to finish.
         */
        result = future.get(3, TimeUnit.SECONDS);
        displayResult(result, statement);

    } catch (IllegalArgumentException e) {
        System.out.println("Invalid statement:\n" + e.getMessage());
    } catch (FaultException e) {
        System.out.println
            ("Statement couldn't be executed, please retry: " + e);
        cleanupOperation(future);
    } catch (ExecutionException e) {
        System.out.println
            ("Problem detected while waiting for a DDL statement: " +
             e.getCause());
        cleanupOperation(future);
    } catch (InterruptedException e) {
        System.out.println
            ("Interrupted while waiting for a DDL statement: " + e);
        cleanupOperation(future);
    } catch (TimeoutException e) {
        System.out.println("Statement execution took too long: " + e);
        cleanupOperation(future);
    }
}

private void cleanupOperation(ExecutionFuture future) {
    if (future == null) {
        /* nothing to do */
        return;
    }

    System.out.println("Statement:");
    System.out.println(future.getStatement());
    System.out.println("has status: ");
    System.out.println(future.getLastStatus());

    if (!future.isDone()) {
        future.cancel(true);
        System.out.println("Statement is cancelled");
    }
}

private void displayResult(StatementResult result, String statement) {
    System.out.println("===========================");
    if (result.isSuccessful()) {
        System.out.println("Statement was successful:\n\t" + 
                            statement);
        System.out.println("Results:\n\t" + result.getInfo());
    } else if (result.isCancelled()) {
        System.out.println("Statement was cancelled:\n\t" + 
                            statement);
    } else {
        /*
         * statement wasn't successful: may be in error, or may still be
         * in progress.
         */
        if (result.isDone()) {
            System.out.println("Statement failed:\n\t" + statement);
            System.out.println("Problem:\n\t" + result.getErrorMessage());
        } else {
            System.out.println("Statement in progress:\n\t" + statement);
            System.out.println("Status:\n\t" + result.getInfo());
        }
    }
} 

サポートされている非スカラー型に索引付けする方法の例は、非スカラー・データ型の索引付けを参照してください。