【问题标题】:Which indexes do I need for following query to avoid a full-graph scan?我需要哪些索引来跟踪查询以避免全图扫描?
【发布时间】:2017-08-03 08:27:49
【问题描述】:

以下查询最多应返回标签为REPOSITORYlimit 顶点,这些顶点最后一次更新是在minLastUpdated 之前,并且不是FILE_UPLOAD 类型,除非设置了NEEDS_UPDATE 标志。

g.V()
    .hasLabel(VertexLabel.REPOSITORY.name())
    .has(PropertyKey.INDEXED_LABEL.name(), VertexLabel.REPOSITORY.name())
    .has(PropertyKey.LAST_UPDATED.name(), P.lt(minLastUpdated))
    .or(__.not(__.has(PropertyKey.TYPE.name(), RepositoryType.FILE_UPLOAD.name())),
        __.has(PropertyKey.NEEDS_UPDATE.name(), true))
    .limit(limit);

为了避免全图扫描,我在属性INDEXED_LABELTYPENEEDS_UPDATE 上创建了以下索引,这是一个结合了这三个属性的复合索引和一个混合索引:

//By Label
mgmt.buildIndex("byIndexedLabel", Vertex.class)
    .addKey(indexedLabelKey)
    .buildCompositeIndex();

//By Type
mgmt.buildIndex("byType", Vertex.class)
    .addKey(typeKey)
    .buildCompositeIndex();

//By Needs Update
mgmt.buildIndex("byNeedsUpdate", Vertex.class)
    .addKey(needsUpdateKey)
    .buildCompositeIndex();

//Combination of the three
mgmt.buildIndex("byIndexedLabelTypeAndNeedsUpdate", Vertex.class)
    .addKey(indexedLabelKey)
    .addKey(typeKey)
    .addKey(needsUpdateKey)
    .buildCompositeIndex();

//Mixed Index
mgmt.buildIndex("repositoryByTypeAndLastUpdated", Vertex.class)
    .addKey(indexedLabelKey, Mapping.STRING.asParameter())
    .addKey(lastUpdatedKey)
    .indexOnly(repositoryLabel)
    .buildMixedIndex("search");

但在执行查询时,我收到以下警告:

WARN  - StandardTitanTx$6: Query requires iterating over all vertices [()]. For better performance, use indexes

旁注

  • 顶点标签与索引在同一事务中定义,这意味着所有索引都应立即可用。
  • PropertyKeyVertexLabel 是我自己的 enums
  • 索引设置期间使用的键都是我之前添加的com.thinkaurelius.titan.core.PropertyKey 的所有实例。
  • 所有属性的数据类型均为 String,但 NEEDS_UPDATE 除外,它是 Boolean

环境

  • 泰坦 1.0.0
  • TinkerPop 3.0.1
  • 弹性搜索 1.0.0
  • 伯克利存储后端

感谢您提出的任何建议。

【问题讨论】:

    标签: indexing titan gremlin


    【解决方案1】:

    只有PropertyKey.INDEXED_LABEL.name()PropertyKey.LAST_UPDATED.name() 相关,其他属性不能用于索引查找。也就是说,创建一个搜索索引是有意义的:a)您有多个属性,b)其中一个具有范围条件:P.lt(minLastUpdated)(没有其他索引可以回答范围查询,并且具有复合覆盖的多个属性众所周知,索引迟早会造成麻烦)。创建一个涵盖这两个属性的单一索引以获得最佳性能。

    mgmt.buildIndex('repositoryByTypeAndLastUpdated', Vertex.class).
        addKey(indexedLabelKey, Mapping.STRING.asParameter()).
        addKey(lastUpdatedKey).indexOnly(repositoryLabel).buildMixedIndex("search")
    

    更新:

    INDEXED_LABEL 实际上是不可索引的,或者不应该被索引,因为它似乎只是作为属性存储的顶点标签的副本。下面是一个完整的工作示例,它不会给您任何关于完整扫描的警告。

    gremlin> graph = TitanFactory.open("conf/titan-berkeleyje-es.properties")
    ==>standardtitangraph[berkeleyje:/projects/aurelius/titan/conf/../db/berkeley]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[standardtitangraph[berkeleyje:/projects/aurelius/titan/conf/../db/berkeley], standard]
    gremlin> m = graph.openManagement()
    ==>com.thinkaurelius.titan.graphdb.database.management.ManagementSystem@10a0a1e
    gremlin> repository = m.makeVertexLabel("repository").make()
    ==>repository
    gremlin> lastUpdated = m.makePropertyKey("lastUpdated").dataType(Long.class).make()
    ==>lastUpdated
    gremlin> needsUpdate = m.makePropertyKey("needsUpdate").dataType(Boolean.class).make()
    ==>needsUpdate
    gremlin> type = m.makePropertyKey("type").dataType(String.class).make()
    ==>type
    gremlin> m.buildIndex("repositoryByLastUpdated", Vertex.class).
    gremlin>   addKey(lastUpdated).indexOnly(repository).buildMixedIndex("search")
    ==>repositoryByLastUpdated
    gremlin> m.commit()
    ==>null
    
    gremlin> g.V().has("repository", "lastUpdated", lt(System.currentTimeMillis())).
    gremlin>   or(has("type", neq("FILE UPLOAD")), has("needsUpdate", true)).limit(10)
    gremlin> 
    

    我的图表中没有数据,但警告将显示为 w/ 或 w/o 数据。

    【讨论】:

    • 非常感谢您的建议,我迫不及待地尝试了一下。令我失望的是,它不能解决问题。很奇怪,对吧?查看我更新的问题,我什至添加了elasticsearch 作为索引后端。对此的任何更多想法将不胜感激!
    • 实际上我不太确定 INDEXED_LABEL 在您的示例中是什么。无论如何,我将在几分钟内更新我的答案并提供一个完整的示例。
    • 原来底层的SubgraphStrategy 导致了全图扫描。我认为它连接到this。谢谢你的简洁查询。至于“索引标签”,见this discussion
    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多