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.
Define a model including the Payment entity.
The first iteration of our data model contains an @Index annotation above the amount attribute.
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
before updating the relationship model of the Invoice and Payment com.onyxdevtools.entities.
Prior to running this script, ensure the database is deleted so that we can
with a clean slate.
If the script runs successfully you have created several payments.
Run the Main class
Connect to a new database and seed the data.
Take note of the database location as you will need it later to use in another
Modify the Payment entity
The amount index has been removed and we have added an index to the notes attribute
Re-Connect to the Database
Create a script to reconnect to the database.
The database location should be the same as what was declared in the first
Verify we can Query on the non-indexed attribute
Verify we can Query the non indexed amount attribute.
Since the index has been removed, this Query may not have good performance when executed on large data sets.
Verify we can Query on the indexed attribute
Verify we can Query the indexed notes attribute.
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.
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.