通过使用JanusGraph索引提高性能
翻译整理:纪玉奇
Extending JanusGraph Server
Graph Index
- g.V().has('name','hercules')
- g.E().has('reason', textContains('loves'))
- JanusGraphManagement.buildIndex(String,Class)
Composite Index
- // 在graph中有事务执行时绝不能创建索引(否则可能导致死锁)
- graph.tx().rollback()
- mgmt = graph.openManagement()
- name = mgmt.getPropertyKey('name')
- age = mgmt.getPropertyKey('age')
- // 构建根据name查询vertex的组合索引
- mgmt.buildIndex('byNameComposite',Vertex.class).addKey(name).buildCompositeIndex()
- // 构建根据name和age查询vertex的组合索引
- mgmt.buildIndex('byNameAndAgeComposite',Vertex.class).addKey(name).addKey(age).buildCompositeIndex()
- mgmt.commit()
- //等待索引生效
- mgmt.awaitGraphIndexStatus(graph,'byNameComposite').call()
- mgmt.awaitGraphIndexStatus(graph,'byNameAndAgeComposite').call()
- //对已有数据重新索引
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("byNameComposite"),SchemaAction.REINDEX).get()
- mgmt.updateIndex(mgmt.getGraphIndex("byNameAndAgeComposite"),SchemaAction.REINDEX).get()
- mgmt.commit()
Index Uniqueness
- graph.tx().rollback()//Never create new indexes while a transaction is active
- mgmt = graph.openManagement()
- name = mgmt.getPropertyKey('name')
- mgmt.buildIndex('byNameUnique',Vertex.class).addKey(name).unique().buildCompositeIndex()
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitGraphIndexStatus(graph,'byNameUnique').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("byNameUnique"),SchemaAction.REINDEX).get()
- mgmt.commit()
Mixed Index
- graph.tx().rollback()//Never create new indexes while a transaction is active
- mgmt = graph.openManagement()
- name = mgmt.getPropertyKey('name')
- age = mgmt.getPropertyKey('age')
- mgmt.buildIndex('nameAndAge',Vertex.class).addKey(name).addKey(age).buildMixedIndex("search")
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitGraphIndexStatus(graph,'nameAndAge').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("nameAndAge"),SchemaAction.REINDEX).get()
- mgmt.commit()
- mgmt.buildIndex('nameAndAge',Vertex.class).addKey(name,Mapping.TEXT.getParameter()).addKey(age,Mapping.TEXT.getParameter()).buildMixedIndex("search")
- g.V().has('name', textContains('hercules')).has('age', inside(20,50))
- g.V().has('name', textContains('hercules'))
- g.V().has('age', lt(50))
Adding Property Keys
- //Never create new indexes while a transaction is active
- graph.tx().rollback()
- mgmt = graph.openManagement()
- //创建一个新的属性
- location = mgmt.makePropertyKey('location').dataType(Geoshape.class).make()
- nameAndAge = mgmt.getGraphIndex('nameAndAge')
- //修改索引
- mgmt.addIndexKey(nameAndAge, location)
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitGraphIndexStatus(graph,'nameAndAge').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("nameAndAge"),SchemaAction.REINDEX).get()
- mgmt.commit()
Mapping Parameters
Ordering
- 排序依据的属性名称
- 升降序,incr和decr
- g.V().has('name', textContains('hercules')).order().by('age', decr).limit(10)
- composite graph index原生不支持对返回结果排序,数据会被先加载到内存中再进行排序,对于大数据集合来讲成本非常高
- Mixed graph index本身支持排序返回,但排序中要使用的property key需要提前被加到mix index中去,如果要排序的property key不是index的一部分,将会导致整个数据集合加载到内存。
Label Constraint
- //Never create new indexes while a transaction is active
- graph.tx().rollback()
- mgmt = graph.openManagement()
- name = mgmt.getPropertyKey('name')
- god = mgmt.getVertexLabel('god')
- //只索引有god这一label的顶点
- mgmt.buildIndex('byNameAndLabel',Vertex.class).addKey(name).indexOnly(god).buildCompositeIndex()
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitGraphIndexStatus(graph,'byNameAndLabel').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("byNameAndLabel"),SchemaAction.REINDEX).get()
- mgmt.commit()
Composite versus Mixed Indexes
Vertex-centric Indexs
- h = g.V().has('name','hercules').next()
- g.V(h).outE('battled').has('time', inside(10,20)).inV()
- //Never create new indexes while a transaction is active
- graph.tx().rollback()
- mgmt = graph.openManagement()
- //找到一个property key
- time = mgmt.getPropertyKey('time')
- // 找到一个label
- battled = mgmt.getEdgeLabel('battled')
- // 创建vertex-centric index
- mgmt.buildEdgeIndex(battled,'battlesByTime',Direction.BOTH,Order.decr, time)
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitGraphIndexStatus(graph,'battlesByTime').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getGraphIndex("battlesByTime"),SchemaAction.REINDEX).get()
- mgmt.commit()
- graph.tx().rollback()//Never create new indexes while a transaction is active
- mgmt = graph.openManagement()
- time = mgmt.getPropertyKey('time')
- rating = mgmt.makePropertyKey('rating').dataType(Double.class).make()
- battled = mgmt.getEdgeLabel('battled')
- mgmt.buildEdgeIndex(battled,'battlesByRatingAndTime',Direction.OUT,Order.decr, rating, time)
- mgmt.commit()
- //Wait for the index to become available
- mgmt.awaitRelationIndexStatus(graph,'battlesByRatingAndTime','battled').call()
- //Reindex the existing data
- mgmt = graph.openManagement()
- mgmt.updateIndex(mgmt.getRelationIndex(battled,'battlesByRatingAndTime'),SchemaAction.REINDEX).get()
- mgmt.commit()
- h = g.V().has('name','hercules').next()
- g.V(h).outE('battled').property('rating',5.0)//Add some rating properties
- g.V(h).outE('battled').has('rating', gt(3.0)).inV()
- g.V(h).outE('battled').has('rating',5.0).has('time', inside(10,50)).inV()
- g.V(h).outE('battled').has('time', inside(10,50)).inV()
Ordering Traversals
- h = g..V().has('name','hercules').next()
- g.V(h).local(outE('battled').order().by('time', decr).limit(10)).inV().values('name')
- g.V(h).local(outE('battled').has('rating',5.0).order().by('time', decr).limit(10)).values('place')
通过使用JanusGraph索引提高性能的更多相关文章
- 通过在Oracle子表外键上建立索引提高性能
根据我的经验,导致死锁的头号原因是外键未加索引(第二号原因是表上的位图索引遭到并发更新).在以下两种情况下,Oracle在修改父表后会对子表加一个全表锁: 1)如果更新了父表的主键(倘若遵循关系数据库 ...
- SQL Server 性能优化之——系统化方法提高性能
SQL Server 性能优化之——系统化方法提高性能 阅读导航 1. 概述 2. 规范逻辑数据库设计 3. 使用高效索引设计 4. 使用高效的查询设计 5. 使用技术分析低性能 6. 总结 1. 概 ...
- 使用SQL Server 2000索引视图提高性能
什么是索引视图? 许多年来,Microsoft? SQL Server? 一直都提供创建虚拟表(称为视图)的功能.在过去,这些视图主要有两种用途: 提供安全机制,将用户限制在一个或多个基表中的数据的某 ...
- 探究SQL添加非聚集索引,性能提高几十倍之谜
上周,技术支持反映:客户的一个查询操作需要耗时6.1min左右,在跟进代码后,简化了数据库的查询后仍然收效甚微.后来,技术总监分析了sql后,给其中的一个表添加的一个非聚集索引(三个字段)后,同样的查 ...
- 千万级MySQL数据库建立索引,提高性能的秘诀
实践中如何优化MySQL 实践中,MySQL的优化主要涉及SQL语句及索引的优化.数据表结构的优化.系统配置的优化和硬件的优化四个方面,如下图所示: SQL语句及索引的优化 SQL语句的优化 SQL语 ...
- FMDB官方使用文档-GCD的使用-提高性能(翻译)
FMDB官方使用文档-GCD的使用-提高性能(翻译) 发布于:2013-08-19 10:01阅读数:13395 由于FMDB是建立在SQLite的之上的,所以你至少也该把这篇文章从头到尾读一遍.与此 ...
- Oracle 学习总结 - 表和索引的性能优化
表的性能 表的性能取决于创建表之前所应用的数据库特性,数据库->表空间->表,创建数据库时确保为每个用户创建一个默认的永久表空间和临时表空间并使用本地管理,创建表空间设为本地管理并且自动段 ...
- SQL 提高性能
参考博客:http://www.cnblogs.com/jiekzou/p/5988099.html 非常感谢博主分享. 1.set nocount on 关闭行基数信息,减少网络通信,提高程序性能 ...
- 22 mysql有那些”饮鸩止渴”提高性能的方法?
22 mysql有那些”饮鸩止渴”提高性能的方法? 正常的短连接模式是连接到数据库后,执行很少的SQL语句就断开,下次需要的时候再重新连接.如果使用的是短连接,在业务高峰期的时候,就可能出现连接数突然 ...
随机推荐
- cocos2d-x解析xml时的Bug
cocos2d-x中使用tinyxml解析xml配置.如下: tinyxml2::XMLDocument doc; if (tinyxml2::XML_SUCCESS != doc.LoadFile( ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力
D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...
- MYSQL复习笔记6-字符集
Date: 20100101 Auth: Jin 参考http://blog.sina.com.cn/s/blog_9707fac301016wxm.html 一.字符集介绍 计算机只处理二进制代码 ...
- Oracle的sqlnet.ora文件配置
DBA对这个文件一定不会陌生,大家了解最多的也一定是sqlnet.ora用来决定oracle怎么解析一个连接中出现的字符串,例如: sqlplus sys/oracle@orcl 那么这个orcl怎么 ...
- 【iOS开发】canOpenURL: failed for URL
控制台输出 如图是在我启动一个 Xcode 7 + iOS 9 的 App 之后,控制台的输出. 这在 Xcode 6.4 + iOS 8 时,是不会有的情况,原因是[为了强制增强数据访问安全, iO ...
- List 中的最大最小值
以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值: /* author by w3cschool.cc Main.java */ imp ...
- SecureRandom产生强随机数简介
SecureRandom是强随机数生成器,主要应用的场景为:用于安全目的的数据数,例如生成秘钥或者会话标示(session ID),弱随机数生成器会产生严重的安全问题,而使用SecureRandom这 ...
- How to publish a WordPress blog to a static GitLab Pages site
https://opensource.com/article/18/8/publish-wordpress-static-gitlab-pages-site A long time ago, I se ...
- 《深入理解Java虚拟机》笔记5
Java虚拟机可以执行的语言并不是只有Java语言,比如jython也可以 运行在Java虚拟机上.不明白字节码之前觉得挺疑惑,为什么和Java 完全不同语法的程序语言也可以运行在虚拟机上呢? 不得不 ...