廷克图提供了最基本的索引功能,它使用它仍可以提高您的查询的整体的性能。两个方法createIndex 和 dropIndex 提供了创建索引和删除索引的方法。如果需要,顶点和边的属性也可以被索引。 第三个方法getIndexedKeys 可以查询什么索引已经被创建了。下面的例子运行了我们前边测试的一些查询,有顶点的code属性的索引的,和没有顶点的code属性的索引的。
TinkerGraph provides a rudimentary indexing capability but using it can still improve overall
performance of your queries. Two methods createIndex and dropIndex are provided for creating
and deleting indexes. Vertex and Edge properties can be indexed as needed. A third method,
getIndexedKeys can be used to query what indexes have been created. The example below runs the
query we used in some of our prior tests with and without an index being present for the code
vertex property.
现在我们给每个顶点的code属性创建一个索引,再运行查询一次。注意下面用到的Vertex是Vertex类的简写。
Let’s now create an index for the code property of every vertex and try the query again. Note that
Vertex as used below is shorthand for Vertex.class.
我们可以查询我们已经创建了什么索引,像下面这样。
We can query what indexes we have creates as follows.
现在让我们删除索引。
Now let’s drop the index.
因为航线图很小,一个顶点的属性对于整体的性能影响很小。因为有很大的边,或许创建边的索引可能对于一些查询帮助更大。我们一起做个实验。下面的查询是找到dist属性是1000的所有的边。
Because the air routes graph is small, a Vertex property index has little effect on overall
performance. However, as there are a lot more edges, perhaps creating an edge property index
could help some queries. Let’s try an experiment. The following query looks for all edges that have
a dist property of 1000.
现在为边的属性dist创建一个索引,再运行查询。我们也可像以前一样,查看什么边的索引已被创建。
Let’s now create an index for the edge property called dist and run the query again. We can also, as
before, check to see what edge indexes have been created.
这次您看到我们的索引产生了很大的不同。
This time you can see that our index has made a big difference.
两个查询时间巨大的差别归因于索引。在第一个例子中,图中的每条边大约有50000个,需要被检查。而在第二个例子中,用到了索引,可以直接找到dist是1000的边,不需要搜索所有的边。注意索引只对精确的比较有帮助。像下面的这样的查询就不会因索引而受益。
The timing difference between the two queries can be attributed to the index. In the first case every
edge in the graph (over 50,000 of them) had to be inspected. In the second case the index was used
to go directly to the edges with a dist of 1000 and no searching of all the edges was required. Note
that the index only helps with exact comparisons. A query such as the one below will not benefit
from the index.
稍后我们会看到在杰森图中使用索引,看一些外边的索引技术比如阿帕奇的Solr或者ES,它们会支持更复杂的比较断言的类型。
Later on we will take a look at using an index with JanusGraph and look at external indexing
technologies such as Apache Solr and Elasticsearch that do support more complex types of
comparison predicates.
浙公网安备 33010602011771号