Working with Indexes
The Geode query engine supports indexing. An index can provide significant performance gains for query execution.
A query run without the aid of an index iterates through every object in the collection. If an index is available that matches part or all of the query specification, the query iterates only over the indexed set, and query processing time can be reduced.
Optimizing your queries with indexes requires a cycle of careful planning, testing, and tuning. Poorly-defined indexes can degrade the performance of your queries instead of improving it. This section gives guidelines for index usage in the query service.
QueryServiceAPI provides methods to create, list and remove the index. You can also use
gfshcommand-line interface to create, list and remove indexes, and use cache.xml to create an index.
Creating a key index is a good way to improve query performance when data is partitioned using a key or a field value. You can create key indexes by using the
createKeyIndexmethod of the QueryService or by defining the index in
cache.xml. Creating a key index makes the query service aware of the relationship between the values in the region and the keys in the region.
Hash indexes are deprecated. Geode supports the creation of hash indexes for the purposes of performing equality-based queries.
To assist with the quick lookup of multiple values in a Map (or HashMap) type field, you can create an index (sometimes referred to as a “map index”) on specific (or all) keys in that field.
In order to speed and promote efficiency when creating indexes, you can define multiple indexes and then create them all at once.
Indexes are automatically kept current with the region data they reference. The region attribute
IndexMaintenanceSynchronousspecifies whether the region indexes are updated synchronously when a region is modified or asynchronously in a background thread.
You can use the hint keyword to allow Geode’s query engine to prefer certain indexes.
Queries with one comparison operation may be improved with either a key or range index, depending on whether the attribute being compared is also the primary key.
Equi-join queries are queries in which two regions are joined through an equality condition in the WHERE clause.
You can use indexes when querying on overflow regions; however, there are caveats.
To query across multiple regions, identify all equi-join conditions. Then, create as few indexes for the equi-join conditions as you can while still joining all regions.
This topic provides code samples for creating query indexes.