JanusGraph 0.2.0双索引场景下查询的索引使用疑问
JanusGraph 0.2.0 多条件查询的索引使用问题解答
咱们先明确你的场景:你基于JanusGraph 0.2.0创建了两个复合索引,执行了一条多条件顶点查询,想确认索引的使用情况。
你的索引创建与查询代码
mgmt = graph.openManagement() keyName = mgmt.getPropertyKey('propertyKeyA') labelName = mgmt.getVertexLabel('labelA') mgmt.buildIndex('labelA_keyAIndex', Vertex.class).addKey(keyName).indexOnly(labelName).buildCompositeIndex() mgmt.commit() mgmt.awaitGraphIndexStatus(graph, 'labelA_keyAIndex').call() mgmt = graph.openManagement() mgmt.updateIndex(mgmt.getGraphIndex("labelA_keyAIndex"), SchemaAction.REINDEX).get() mgmt.commit() mgmt.awaitGraphIndexStatus(graph, 'labelA_keyAIndex').status(SchemaStatus.ENABLED).call() mgmt = graph.openManagement() keyName = mgmt.getPropertyKey("propertyKeyB") mgmt.buildIndex("keyBIndex",Vertex.class).addKey(keyName).buildCompositeIndex() mgmt.commit() mgmt.awaitGraphIndexStatus(graph, "keyBIndex").call(); mgmt = graph.openManagement() mgmt.updateIndex(mgmt.getGraphIndex("keyBIndex"), SchemaAction.REINDEX).get() mgmt.commit() mgmt.awaitGraphIndexStatus(graph, 'keyBIndex').status(SchemaStatus.ENABLED).call() g.V().hasLabel("labelA").has("propertyKeyB","value").has("propertyKeyA","value").valueMap()
索引说明
labelA_keyAIndex:绑定labelA顶点标签的复合索引,仅针对propertyKeyA字段keyBIndex:全局复合索引,针对propertyKeyB字段(该字段关联多个顶点标签)
问题结论
这条查询只会使用labelA_keyAIndex,不会同时用到两个索引。
原因分析
JanusGraph的查询优化器在处理多条件查询时,只会选择单个最优索引来启动查询流程,不支持多个复合索引的联合使用。咱们拆解下你的查询逻辑:hasLabel("labelA") + has("propertyKeyB","value") + has("propertyKeyA","value")
labelA_keyAIndex是带标签绑定的索引,能直接定位到labelA且propertyKeyA=value的顶点,初始结果集范围极小keyBIndex是全局索引,会先找出所有propertyKeyB=value的顶点(跨多个标签),再过滤labelA和propertyKeyA的条件,初始结果集范围远大于前者
优化器会判断labelA_keyAIndex的过滤效率更高,因此会优先用它获取顶点集合,之后再在这个小集合里过滤propertyKeyB=value的条件,完全不会触发keyBIndex的使用。
优化建议
如果想让查询更高效,可以创建一个同时绑定labelA、包含propertyKeyA和propertyKeyB的组合复合索引,这样查询能直接命中该索引,一步到位获取目标顶点。
内容的提问来源于stack exchange,提问作者User12345
相关产品推荐
相关产品推荐

