How to create Composite index in Hazelcast

362 views Asked by At

I am trying to improve performance of Hazelcast lookup by using composite key. I have a class entity

Class Entity {
    private Long id;
    private String field1;
    private String field2;
    private String field3;
    // getter and setters
}

I have added a composite index comprising of above 3 fields in hazelcast-server.xml

...
<map name="Entity">
    <max-idle-seconds>2678400</max-idle-seconds>
    <time-to-live-seconds>2678400</time-to-live-seconds>
    <backup-count>3</backup-count>
    <async-backup-count>3</async-backup-count>
    <read-backup-data>true</read-backup-data>
        <indexes>
        <index ordered="false">field1, field2, field3</index>
    </indexes>
</map>
...

Querying Hazelcast map

EntryObject entryObject = new PredicateBuilder().getEntryObject();
PredicateBuilder predicate = entryObject.get("field1").equal("value1")
    .and(entryObject.get("field2").equal("value2"))
    .and(entryObject.get("field3").equal("value3"));
IMap<Long, Entity> entityCache = hazelcastInstance.getMap("Entity")
List<Entity> routings = new ArrayList<>(entityCache.values(predicate));

The code is working fine with and without the index.

Questions

  1. Is this the correct way of creating and using composite index?
  2. Is there a way to check if the index is actually being used by the query? (I could not get any index related info on hazelcast management-center console)

I have scanned a lot hazelcast documentation and internet forums but could not find concrete answers. Hazelcast version: 3.12; Java version: 8

2

There are 2 answers

0
lpettinato On BEST ANSWER

The only way I've found is IMap.getLocalMapStats().getIndexStats() as described here: https://docs.hazelcast.org/docs/3.12.1/manual/html-single/index.html#map-index-statistics

0
Oliv On

The code is working fine with and without the index.

That's obvious, because the index isn't required.

I didn't test, but index should be used in this case, all three columns should be used. There's no public API to see if an index is actually used. What you can do is to put a large number of entries into the map and the query should be much faster with the index. The other way is debugging the query execution, e.g. put a breakpoint in Indexes.matchIndex(), but I'm not sure this class was the same in 3.12.