Updating an entity's index within your model

You can download the code for this example here:

OnyxDevTools/onyx-database-samples/model-updates/ ... UpdateIndexDemo.java.

There are 2 parts to this example. The first part is to create an existing database containing the original structure of your model. The second is to modify specific parts of the model and observe how the database handles the updates.

This example consist of both adding and removing an index. In Onyx Database any attribute can be indexed. Removing an index is easy enough but what happens when you add an index to an already existing data model?

Onyx Database supports the adding of an index to an already existing data model. After adding it, it may take some time for the index to re-build. That should happen automatically but you can also trigger it manually.

  1. Define a model including the Payment entity.

    The first iteration of our data model contains an @Index annotation above the amount attribute.

  2. Create a script to seed some test data

    In order to observe how the database reacts to changes we need to create a script to load sample data before updating the relationship model of the Invoice and Payment com.onyxdevtools.entities.

    Notes:
    Prior to running this script, ensure the database is deleted so that we can start with a clean slate.
    If the script runs successfully you have created several payments.
  3. Run the Main class

    Connect to a new database and seed the data.

    Notes:
    Take note of the database location as you will need it later to use in another script.
  4. Modify the Payment entity

    The amount index has been removed and we have added an index to the notes attribute

  5. Re-Connect to the Database

    Create a script to reconnect to the database.

    Notes:
    The database location should be the same as what was declared in the first script.
  6. Verify we can Query on the non-indexed attribute

    Verify we can Query the non indexed amount attribute.

    Notes:
    Since the index has been removed, this Query may not have good performance when executed on large data sets.
  7. Verify we can Query on the indexed attribute

    Verify we can Query the indexed notes attribute.

    Notes:
    Since the index has added to the notes field, this Query should perform well.
    When Querying an Index, you must use the QueryCriteriaOperator.EQUAL operator to check for exact matches; otherwise it will not take advantage of the index.
  8. Manually Re-Build Index

    It is not necessarily to manually re-build an index. This should be performed upon initializing the database. The database should automatically detect the change but if you choose to re-build it, the following example will show you how.

  9. Manual Migration