- Use parameters instead of literals when possible. This allows Cypher to re-use your queries instead of having to parse and build new execution plans.
- Always set an upper limit for your variable length patterns. Itâs possible to have a query go wild and touch all nodes in a graph by mistake.
- Return only the data you need. Avoid returning whole nodes and relationshipsâââinstead, pick the data you need and return only that.
- Use
PROFILE
/EXPLAIN
to analyze the performance of your queries. See Query Tuning for more information on these and other topics, such as planner hints.
Neo4j Schema Operations
Performance
CONSTRAINT
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS UNIQUE
Create a unique property constraint on the label Person and property name. If any other node with that label is updated or created with a name that already exists, the write operation will fail. This constraint will create an accompanying index.
CREATE CONSTRAINT uniqueness ON (p:Person)
ASSERT p.age IS UNIQUE
Create a unique property constraint on the label Person and property age with the name uniqueness. If any other node with that label is updated or created with a age that already exists, the write operation will fail. This constraint will create an accompanying index.
CREATE CONSTRAINT ON (p:Person)
ASSERT p.surname IS UNIQUE
OPTIONS {indexProvider: 'native-btree-1.0'}
Create a unique property constraint on the label Person and property surname with the index provider native-btree-1.0 for the accompanying index.
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS NOT NULL
(â ) Create a node property existence constraint on the label Person and property name, throws an error if the constraint already exists. If a node with that label is created without a name, or if the name property is removed from an existing node with the Person label, the write operation will fail.
CREATE CONSTRAINT node_exists IF NOT EXISTS ON (p:Person)
ASSERT p.name IS NOT NULL
(â ) If a node property existence constraint on the label Person and property name or any constraint with the name node_exists already exist then nothing happens. If no such constraint exists, then it will be created.
CREATE CONSTRAINT ON ()-[l:LIKED]-()
ASSERT l.when IS NOT NULL
(â ) Create a relationship property existence constraint on the type LIKED and property when. If a relationship with that type is created without a when, or if the when property is removed from an existing relationship with the LIKED type, the write operation will fail.
CREATE CONSTRAINT relationship_exists ON ()-[l:LIKED]-()
ASSERT l.since IS NOT NULL
(â ) Create a relationship property existence constraint on the type LIKED and property since with the name relationship_exists. If a relationship with that type is created without a since, or if the since property is removed from an existing relationship with the LIKED type, the write operation will fail.
SHOW UNIQUE CONSTRAINTS YIELD *
List all unique constraints.
CREATE CONSTRAINT ON (p:Person)
ASSERT (p.firstname, p.surname) IS NODE KEY
(â ) Create a node key constraint on the label Person and properties firstname and surname. If a node with that label is created without both firstname and surname or if the combination of the two is not unique, or if the firstname and/or surname labels on an existing node with the Person label is modified to violate these constraints, the write operation will fail.
CREATE CONSTRAINT node_key ON (p:Person)
ASSERT (p.name, p.surname) IS NODE KEY
(â ) Create a node key constraint on the label Person and properties name and surname with the name node_key. If a node with that label is created without both name and surname or if the combination of the two is not unique, or if the name and/or surname labels on an existing node with the Person label is modified to violate these constraints, the write operation will fail.
CREATE CONSTRAINT node_key_with_config ON (p:Person)
ASSERT (p.name, p.age) IS NODE KEY
OPTIONS {indexConfig: {`spatial.wgs-84.min`: [-100.0, -100.0], `spatial.wgs-84.max`: [100.0, 100.0]}}
(â ) Create a node key constraint on the label Person and properties name and age with the name node_key_with_config and given spatial.wgs-84 settings for the accompanying index. The other index settings will have their default values.
DROP CONSTRAINT uniqueness
Drop the constraint with the name uniqueness, throws an error if the constraint does not exist.
DROP CONSTRAINT uniqueness IF EXISTS
Drop the constraint with the name uniqueness if it exists, does nothing if it does not exist.
INDEX
CREATE INDEX FOR (p:Person) ON (p.name)
Create an index on nodes with label Person and property name.
CREATE INDEX index_name FOR ()-[k:KNOWS]-() ON (k.since)
Create an index on relationships with type KNOWS and property since with the name index_name.
CREATE INDEX FOR (p:Person) ON (p.surname)
OPTIONS {indexProvider: 'native-btree-1.0', indexConfig: {`spatial.cartesian.min`: [-100.0, -100.0], `spatial.cartesian.max`: [100.0, 100.0]}}
Create an index on nodes with label Person and property surname with the index provider native-btree-1.0 and given spatial.cartesian settings. The other index settings will have their default values.
CREATE INDEX FOR (p:Person) ON (p.name, p.age)
Create a composite index on nodes with label Person and the properties name and age, throws an error if the index already exist.
CREATE INDEX IF NOT EXISTS FOR (p:Person) ON (p.name, p.age)
Create a composite index on nodes with label Person and the properties name and age if it does not already exist, does nothing if it did exist.
CREATE LOOKUP INDEX lookup_index_name FOR (n) ON EACH labels(n)
Create a token lookup index with the name lookup_index_name on nodes with any label .
CREATE LOOKUP INDEX FOR ()-[r]-() ON EACH type(r)
Create a token lookup index on relationships with any relationship type.
CREATE FULLTEXT INDEX node_fulltext_index_name FOR (n:Friend) ON EACH [n.name]
OPTIONS {indexConfig: {`fulltext.analyzer`: 'swedish'}}
Create a fulltext index on nodes with the name node_fulltext_index_name and analyzer swedish. Fulltext indexes on nodes can only be used by from the procedure db.index.fulltext.queryNodes. The other index settings will have their default values.
CREATE FULLTEXT INDEX rel_fulltext_index_name FOR ()-[r:HAS\_PET|BROUGHT\_PET]-() ON EACH [r.since, r.price]
Create a fulltext index on relationships with the name rel_fulltext_index_name. Fulltext indexes on relationships can only be used by from the procedure db.index.fulltext.queryRelationships.
SHOW INDEXES
List all indexes.
MATCH (n:Person) WHERE n.name = $value
An index can be automatically used for the equality comparison. Note that for example toLower(n.name) = $value will not use an index.
MATCH (n:Person)
WHERE n.name IN [$value]
An index can automatically be used for the IN list checks.
MATCH (n:Person)
WHERE n.name = $value and n.age = $value2
A composite index can be automatically used for equality comparison of both properties. Note that there needs to be predicates on all properties of the composite index for it to be used.
MATCH (n:Person)
USING INDEX n:Person(name)
WHERE n.name = $value
Index usage can be enforced when Cypher uses a suboptimal index, or more than one index should be used.
DROP INDEX index_name
Drop the index named index_name, throws an error if the index does not exist.
DROP INDEX index_name IF EXISTS
Drop the index named index_name if it exists, does nothing if it does not exist.