Index on multiple properties in Neo4j / Cypher

2019-06-23 11:01发布

问题:

Can I create an index with multiple properties in 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?

Thanks!

回答1:

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

Cheers,

Kenny



回答2:

As of 3.2, Neo4j supports composite indexes. For your example:

CREATE INDEX ON :Person(first_name, last_name)

You can read more on composite indexes here.



标签: neo4j cypher