简体   繁体   English

Neo4j / Cypher中多个属性的索引

[英]Index on multiple properties in Neo4j / Cypher

Can I create an index with multiple properties in cypher? 我可以在cypher中创建具有多个属性的索引吗?

I mean something like 我的意思是

CREATE INDEX ON :Person(first_name, last_name)

If I understand correctly this is not possible, but if I want to write queries like: 如果我理解正确,这是不可能的,但如果我想编写如下查询:

MATCH (n:Person)
WHERE n.first_name = 'Andres' AND n.last_name = 'Doe'
RETURN n

Does these indexes make sense? 这些指数有意义吗?

CREATE INDEX ON :Person(first_name)
CREATE INDEX ON :Person(last_name)

Or should I try to merge "first_name" and "last_name" in one property? 或者我应该尝试在一个属性中合并“first_name”和“last_name”?

Thanks! 谢谢!

Indexes are good for defining some key that maps to some value or set of values. 索引适用于定义映射到某个值或值集的某些键。 The key is always a single dimension. 关键始终是单一维度。

Consider your example: 考虑你的例子:

CREATE INDEX ON :Person(first_name)
CREATE INDEX ON :Person(last_name)

These two indexes now map to those people with the same first name, and separately it maps those people with the same last name. 现在,这两个索引将映射到具有相同名字的人员,并分别映射具有相同姓氏的人员。 So for each person in your database, two indexes are created, one on the first name and one on the last name. 因此,对于数据库中的每个人,都会创建两个索引,一个在名字上,另一个在姓氏上。

Statistically, this example stinks. 从统计上来说,这个例子很臭。 Why? 为什么? Because the distribution is stochastic. 因为分布是随机的。 You'll be creating a lot of indexes that map to small clusters/groups of people in your database. 您将创建许多映射到数据库中的小群集/人群的索引。 You'll have a lot of nodes indexed on JOHN for the first name. 您将在JOHN为第一个名称编制许多索引节点。 Likewise you'll have a lot of nodes indexed on SMITH for the last name. 同样地,您将在SMITH为姓氏索引许多节点。

Now if you want to index the user's full name, then concatenate, forming JOHN SMITH . 现在,如果你想索引用户的全名,然后连接,形成JOHN SMITH You can then set a property of person as person.full_name . 然后,您可以将person的属性设置为person.full_name While it is redundant, it allows you to do the following: 虽然它是多余的,但它允许您执行以下操作:

  1. Create 创建

     CREATE INDEX ON :Person(full_name) 
  2. Match 比赛

     MATCH (n:Person) USING INDEX n:Person(full_name) WHERE n.full_name = 'JOHN SMITH' 

You can always refer to http://docs.neo4j.org/refcard/2.0/ for more tips and guidelines. 您可以随时参考http://docs.neo4j.org/refcard/2.0/获取更多提示和指南。

Cheers, 干杯,

Kenny 肯尼

As of 3.2, Neo4j supports composite indexes. 从3.2开始,Neo4j支持复合索引。 For your example: 对于你的例子:

CREATE INDEX ON :Person(first_name, last_name)

You can read more on composite indexes here . 您可以在此处阅读有关复合索引的更多信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM