首页 > 解决方案 > JanusGraph - 创建索引后有关所有顶点扫描的警告

问题描述

我正在使用 Janusgraph 0.2.0 并定义了以下顶点(在 Python 中):

class Airport(TypedVertex):
    type = goblin.VertexProperty(goblin.String, card=Cardinality.single)
    airport_code = goblin.VertexProperty(goblin.String, 
        card=Cardinality.single)
    airport_city = goblin.VertexProperty(goblin.String, 
        card=Cardinality.single)
    airport_name = goblin.VertexProperty(goblin.String, 
        card=Cardinality.single)
    airport_region = goblin.VertexProperty(goblin.String, 
        card=Cardinality.single)
    airport_runways = goblin.VertexProperty(goblin.Integer, 
        card=Cardinality.single)
    airport_longest_runway = goblin.VertexProperty(goblin.Integer, 
        card=Cardinality.single)
    airport_elev = goblin.VertexProperty(goblin.Integer, 
        card=Cardinality.single)
    airport_country = goblin.VertexProperty(goblin.String, 
        card=Cardinality.single)
    airport_lat = goblin.VertexProperty(goblin.Float, 
        card=Cardinality.single)
    airport_long = goblin.VertexProperty(goblin.Float, 
        card=Cardinality.single)

然后,我使用以下命令在机场代码字段上为该节点定义了一个索引(排除了一些命令以保持简短)。

mgmt.makePropertyKey('type').dataType(String.class).cardinality(Cardinality.SINGLE).make()
mgmt.makePropertyKey('airport_city').dataType(String.class).cardinality(Cardinality.SINGLE).make()
mgmt.makePropertyKey('airport_code').dataType(String.class).cardinality(Cardinality.SINGLE).make()
mgmt.makePropertyKey('airport_country').dataType(String.class).cardinality(Cardinality.SINGLE).make()
airport_code = mgmt.getPropertyKey('airport_code')
airport_city = mgmt.getPropertyKey('airport_city')
airport_country = mgmt.getPropertyKey('airport_country')
mgmt.buildIndex('by_airport_code_unique', Vertex.class).addKey(airport_code).unique().buildCompositeIndex()
mgmt.buildIndex('by_airport_city', Vertex.class).addKey(airport_city).buildCompositeIndex()
mgmt.buildIndex('by_airport_country', Vertex.class).addKey(airport_country).buildCompositeIndex()
mgmt.awaitGraphIndexStatus(graph, 'by_airport_code_unique').call()
mgmt.awaitGraphIndexStatus(graph, 'by_airport_city').call()
mgmt.awaitGraphIndexStatus(graph, 'by_airport_country').call()

创建后,我使用脚本来描述 :schema 并且我看到所有索引都已注册:

| Graph Index .          | Type .    | Element          | Unique | Backing  | PropertyKey  | Status    |
|-----------------------:|:-----|:--------|:-------|:--------|:-----------|:--------|
| by_airport_code_unique | Composite | JanusGraphVertex |   true | internalindex | airport_code | REGISTERED |
| by_airport_city | Composite | JanusGraphVertex | false | internalindex | airport_city | REGISTERED |
| by_airport_country | Composite | JanusGraphVertex |  false | internalindex | airport_country | REGISTERED |

当我尝试插入具有相同 airport_code 的第二个顶点时,如预期的那样,我得到了违反约束的异常。但是,如果我进入 gremlin 控制台并运行遍历以通过其 airport_code 检索顶点:

g.V().has('airport_code').values()

我收到警告:WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - 查询需要遍历所有顶点 [()]。为了获得更好的性能,请使用索引

几周前我遇到了类似的问题,问题是我试图根据标签定义索引,当时有人告诉我,janusgraph 不支持标签上的索引。但是,我认为情况并非如此。关于为什么我的索引不起作用或未被使用的任何建议或想法?提前感谢您的帮助。--MD

标签: gremlintinkerpop3janusgraph

解决方案


您看到警告是因为您的查询未使用索引。复合索引用于相等匹配。

复合索引非常快速和高效,但仅限于对特定的、先前定义的属性键组合进行相等查找。混合索引可用于对索引键的任意组合进行查找,并支持多个条件谓词以及取决于后备索引存储的相等性。

为了利用复合索引,您需要提供要匹配的属性和值。例如:

g.V().has('airport_code', 'JFK').toList()

我不确定为什么索引不是ENABLED在创建之后,也许是你遗漏的步骤。如果您在与属性键相同的管理事务中创建索引,则它应该是ENABLED而不是REGISTERED. 查看索引生命周期wiki。


推荐阅读