Neo4j/Cypher中多个属性的索引 [英] Index on multiple properties in Neo4j / Cypher

查看:763
本文介绍了Neo4j/Cypher中多个属性的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在cypher中创建具有多个属性的索引吗?

Can I create an index with multiple properties in cypher?

我的意思是

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

这些索引有意义吗?

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

还是我应该在一个属性中合并"first_name"和"last_name"?

Or should I try to merge "first_name" and "last_name" in one property?

谢谢!

推荐答案

索引非常适合定义一些映射到某个值或一组值的键.关键永远是一个单一的维度.

Indexes are good for defining some key that maps to some value or set of values. The key is always a single dimension.

考虑您的示例:

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.

从统计上看,这个例子很臭.为什么?因为分布是随机的.您将创建许多索引,这些索引映射到数据库中的小型集群/人群.您将在JOHN上为第一个名字建立索引的许多节点.同样,您会在SMITH上为您的姓氏索引许多节点.

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. Likewise you'll have a lot of nodes indexed on SMITH for the last name.

现在,如果要索引用户的全名,则进行连接,形成JOHN SMITH.然后,您可以将人员的属性设置为person.full_name.虽然它是多余的,但它允许您执行以下操作:

Now if you want to index the user's full name, then concatenate, forming JOHN SMITH. You can then set a property of person as person.full_name. While it is redundant, it allows you to do the following:

  1. 创建

  1. Create

CREATE INDEX ON :Person(full_name)

  • 匹配

  • Match

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

  • 您可以始终参考 http://docs.neo4j.org/refcard/2.0/了解更多提示和准则.

    You can always refer to http://docs.neo4j.org/refcard/2.0/ for more tips and guidelines.

    干杯

    肯尼

    这篇关于Neo4j/Cypher中多个属性的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆