如何在 Hazelcast 中创建复合索引

发布于 2025-01-11 06:52:01 字数 1543 浏览 3 评论 0原文

我正在尝试使用复合键来提高 Hazelcast 查找的性能。我有一个类实体,

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

我在 hazelcast-server.xml 中添加了一个由以上 3 个字段组成的复合索引

...
<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>
...

查询 Hazelcast 地图

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));

无论有没有索引,代码都可以正常工作。

问题

  1. 这是创建和使用复合索引的正确方法吗?
  2. 有没有办法检查索引是否确实被查询使用? (我无法在 hazelcast 管理中心控制台上获得任何索引相关信息)

我扫描了很多 hazelcast 文档和互联网论坛,但找不到具体答案。 Hazelcast 版本:3.12; Java版本:8

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2025-01-18 06:52:01

我发现的唯一方法是 IMap.getLocalMapStats().getIndexStats() ,如下所述:https://docs.hazelcast.org/docs/3.12.1/manual/html-single/index.html#map-index-statistics

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

帅哥哥的热头脑 2025-01-18 06:52:01

无论有没有索引,代码都可以正常工作。

这是显而易见的,因为不需要索引。

我没有测试,但在这种情况下应该使用索引,所有三列都应该使用。没有公共 API 可以查看索引是否实际使用。您可以做的就是将大量条目放入映射中,并且使用索引查询应该更快。另一种方法是调试查询执行,例如在 Indexes.matchIndex() 中放置断点,但我不确定这个类在 3.12 中是否相同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文