Neo4j系列导航:
neo4j安装及简单实践
cypher语法基础
cypher插入语法
cypher插入语法
cypher查询语法
cypher通用语法
cypher函数语法
neo4j索引及调优
索引是Neo4j数据库中指定主数据的副本,例如节点、关系或属性。存储在索引中的数据为主存储中的数据提供了访问路径,并允许用户更有效地评估查询过滤器(在某些情况下,还可以从语义上解释查询过滤器)。简而言之,就像书中的索引一样,它们在Neo4j图形数据库中的作用是使数据检索更有效。(与传统关系型数据库索引类似)
创建索引后,DBMS将自动填充和更新索引。
neo4j的索引分为两类:
- 搜索性能索引: 用于加速基于精确匹配的数据检索。此类别包括范围、文本、点和标记查找索引。
- 语义索引: 用于近似匹配和计算查询字符串与匹配数据之间的相似性分数。此类别包括全文索引和矢量索引。
1.简介
搜索性能索引可以更快地检索索引和主数据存储之间的精确匹配。Neo4j中有四种不同的搜索性能索引:
- 范围索引(range indexes): Neo4j的默认索引,支持大多数类型的谓词。
- 文本索引(text indexes): 解决在string值上操作的谓词。针对字符串操作符contains和ends with的查询过滤进行了优化。
- 点索引(point indexes): 解决空间点值上的谓词,针对距离或边界框内的查询进行了优化。
- 令牌查找索引(token lookup indexes): 仅解决节点标签和关系类型谓词(即它们不能解决属性上的任何谓词过滤)。在Neo4j中创建数据库时,会出现两个令牌查找索引(一个用于节点标签,另一个用于关系类型)。
自动使用搜索性能索引,如果有多个索引可用,Cypher计划器将尝试使用最有效地解决特定谓词的索引。但是,可以使用using关键字显式地强制查询使用特定索引。
2. create, show, 和删除索引
2.1.create索引
创建命令:creare...index...
注意:
- 如果create命令中没有指定索引类型,则创建一个范围索引。
- 索引名在索引和约束中必须是唯一的。
- 创建索引需要create index权限。
2.1.1.创建范围索引(range index)
范围索引支持大多数类型的谓词:
谓词 | 语法 |
---|---|
等于check | n.prop = value |
列表成员check | n.prop in list |
存在check | n.prop is not null |
范围check | n.prop > value |
前缀check | starts with |
-
在单个属性上创建节点范围索引:
create index node_range_index_name for(n:Person) on (n.surname)
-
在单个属性上创建关系范围索引:
create index rel_range_index_name for()-[r:KNOWS]-() on (r.since)
-
在多个属性上创建复合节点范围索引:
create index composite_range_node_index_name for(n:Person) on (n.age, n.country)
注意: 对于节点范围索引,只有具有指定标签且包含所有指定属性的节点才会被添加到索引中。
-
在多个属性上创建复合关系范围索引:
create index composite_range_rel_index_name for ()-[r:Friend]-() on (r.date, r.amount)
注意: 对于关系范围索引,只有具有指定类型且包含所有指定属性的关系才会被添加到索引中。
-
添加If not exists以确保存在:
create point index node_point_index if not exists for(n:Person) on (n.sublocation)
注意: 如果已经存在具有相同模式和类型、相同名称或两者的索引,则不会创建索引。从Neo4j 5.17开始,会返回一个信息通知。
2.1.2.创建文本索引(text index)
命令:create text index
文本索引不支持索引配置,从Neo4j 5.1开始,它们有两个可用的索引提供程序,Text -2.0(默认)和Text -1.0(已弃用)。
文本索引支持以下谓词:
谓词 | 语法 |
---|---|
等于check | n.prop = 'example_string' |
列表成员check | n.prop in ['abc', 'example_string', 'neo4j'] |
前缀check | starts with |
后缀check | ends with |
包含check | contains |
文本索引仅用于精确的查询匹配。要执行近似匹配(例如,包括变化和拼写错误),并计算string值之间的相似度评分,请使用语义全文索引。
-
在单个属性上创建节点文本索引:
create text index node_text_index_nickname for(n:Person) on (n.nickname)
-
在单个属性上创建关系文本索引:
create text index rel_text_index_name for ()-[r:KNOWS]-() on (r.interest)
-
添加If not exists以确保存在:
create text index node_index_name if not exists for (n:Person) on (n.nickname)
-
使用特定索引提供程序创建文本索引:
options
子句,索引提供程序的有效值是text-2.0和text-1.0(已弃用)。默认提供程序是text-2.0。create text index text_index_with_indexprovider for()-[r:TYPE]-() on (r.prop1) options {indexProvider: 'text-2.0'}
2.1.3.创建点索引(point index)
命令: create point index
点索引支持索引配置,但只有一个索引提供程序可用,即Point -1.0。
点索引支持以下谓词:
谓词 | 语法 |
---|---|
属性点值 | n.prop = point({x: value, y: value}) |
在边界框内 | point.withinBBox(n.prop, lowerLeftCorner, upperRightCorner) |
距离 | point.distance(n.prop, center) <= distance |
从Neo4j 5.11开始,可以使用类型约束扩展上述谓词集。
-
在单个属性上创建节点点索引:
create point index node_point_index_name for(n:Person) on (n.sublocation)
-
在单个属性上创建关系点索引:
create point index rel_point_index_name for()-[r:STREET]-() on (r.intersection)
-
添加If not exists以确保存在:
create point index node_point_index if not exists for(n:Person) on (n.sublocation)
-
创建具有特定索引配置的点索引:
options
子句中的indexConfig
设置//wgs-84和3D直角坐标设置(在本例中未指定)将使用各自的默认值进行设置。 create point index point_index_with_config for(n:Label) on(n.prop2) options { indexConfig: { `spatial.cartesian.min`: [-100.0, -100.0], `spatial.cartesian.max`: [100.0, 100.0] } }
有效的配置设置为:
配置 默认值 spatial.cartesian.min [ -1000000.0
,-1000000.0
]spatial.cartesian.max [ 1000000.0
,1000000.0
]spatial.cartesian-3d.min [ -1000000.0
,-1000000.0
,-1000000.0
]spatial.cartesian-3d.max [ 1000000.0
,1000000.0
,1000000.0
]spatial.wgs-84.min [ -180.0
,-90.0
]spatial.wgs-84.max [ -180.0
,-90.0
]spatial.wgs-84-3d.min [ -180.0
,-90.0
,-1000000.0
]spatial.wgs-84-3d.max [ 180.0
,90.0
,1000000.0
]
2.1.3.创建令牌查找索引(token lookup index)
命令: create lookup index
在创建Neo4j数据库时默认创建两个令牌查找索引(一个节点标签查找索引和一个关系类型查找索引)。同时只能存在一个节点标签和一个关系类型查找索引。
令牌查找支持以下谓词: 令牌查找索引默认存在,只解决节点标签和关系类型谓词
谓词 | 语法 |
---|---|
节点标签谓词 | match(n:Label) match(n) where n:Label |
关系类型谓词 | match()-[r:REL]->() match()-[r]->() where r:REL |
令牌查找索引提高了Cypher查询的性能和其他索引的填充。删除这些索引可能会导致严重的性能下降。
-
创建节点标签查找索引:
create loopup index node_label_lookup_index for(n) on each labels(n)
一次只能存在一个节点标签查找索引。
-
创建关系类型查找索引:
create loopup index rel_type_lookup_index for()-[r]-() on each type(r)
一次只能存在一个关系类型查找索引。
-
添加If not exists以确保存在:
create loopup index node_label_lookup if not exists for (n) on each labels(n)
2.2.show索引
命令: show indexes
- show indexes默认输出列的索引如果需要所有列,则使用show indexes yield *
- show index需要show index权限
- show index命令有多种过滤方式
-
列出所有默认输出列的索引:
show indexes
-
返回所有索引的特定列:
show indexes yield name, type, indexProvider as provider, options, createStatement return name, type, provider, options.indexConfig as config, createStatement
注意: 如果使用return子句,yield 是强制性的。但是,当使用yield 子句时,return不是强制性的。
-
显示范围索引:
只返回默认输出列:
show range indexes where owningConstraint is null
返回所有列:
show range indexes yield * where owningConstraint is null
show range indexes yield *
命令返回的所有列的完整信息:列 描述 是否默认输出 类型 id 索引id yes integer name 索引name yes string state 索引当前状态 yes string populationPercent 指数总体的%。 yes string type 索引类型(FULLTEXT, LOOKUP, POINT, RANGE, or TEXT) yes string entityType 该索引表示的实体类型(节点或关系) yes string labelsOrTypes 该索引的标签或关系类型 yes List properties 索引的属性 yes List indexProvider 此索引的索引提供程序 yes string owningConstraint 索引关联的约束的名称,如果索引没有关联任何约束,则为空 yes string lastRead 最后一次索引被用来查找的时间,如果在trackedSince之后没有读取索引,或者没有跟踪统计信息,则返回null。在5.8中引入 yes zoned datetime readCount 自trackedSince以来向该索引发出的读查询数,如果不跟踪统计信息,则为空。在5.8中引入 yes integer trackedSince 该索引的使用统计信息跟踪开始的时间,如果没有跟踪统计信息,则为空。在5.8中引入 no zoned datetime options 从OPTIONS映射中检索到的关于索引的提供程序和配置设置的信息。如果在创建索引时都没有指定,则该列将返回默认值。 no map failureMessage 索引失败的失败描述。 no string createStatement 用于创建索引的语句 no string
2.3.删除索引
命令: drop index index_name [if exists]
- 删除索引需要drop index权限
- drop index命令可以是幂等的()
-
删除一个索引:
drop index example_index
-
添加If not exists以确保存在:
drop index missing_index_name if exists