Transaction, index and constraint of Neo4j

1. Services

In order to maintain data integrity and ensure good transaction behavior, Neo4j also supports ACID feature.

be careful:

  • All data modification operations to Neo4j database must be encapsulated in transactions.
  • The default isolation level is READ_COMMITTED.
  • Deadlock protection has been built into core transaction management. (Neo4j will detect the deadlock and throw an exception before the deadlock occurs. Before the exception is thrown, the transaction will be marked as rollback. When the transaction ends, the transaction will release the lock it holds, the deadlock caused by the lock of the transaction will be released, and other transactions can continue to execute. When the user needs, the transaction that threw the exception can try to execute again.)
  • Unless otherwise specified, the operations of Neo4j API are thread safe, and there is no need to use external synchronization methods for Neo4j database operations.

2. Index

2.1 introduction

  • Neo4j CQL supports indexes on node or relational attributes to improve application performance.
  • You can create indexes on attributes with the same label name.
  • These index columns can be used on operators such as MATCH or WHERE to improve the execution of CQL.

2.2 creating a single index

CREATE INDEX [INDEX_NAME] FOR (n:Label) ON (n.property)

For example:

create index for (n:Person) on (n.name)
create index index_name for (n:Person) on (n.name)

2.3 create composite index

create index for (p:Person) on (p.name,p.age)
create index  nameAndAge for (p:Person) on (p.name,p.age)

2.4 full text mode index

The previous conventional pattern index can only accurately match strings or pre suffix indexes (startswitch, endswitch, contains). The full-text index will tokenize the index string value, so it can match terms anywhere in the string. How the index string is tokenized and decomposed into terms depends on the analyzer that configures the full-text schema index. Indexes are created through attributes, which is convenient to quickly find nodes or relationships.

2.4.1 creating and configuring full-text mode indexes

Use dB index. fulltext. Createnodeindex and DB index. fulltext. Createrelationshipindex creates a full-text schema index. When creating an index, each index must specify a unique name for each index to refer to the relevant specific index when querying or deleting the index. Full text schema indexes are applied to label lists or relationship type lists respectively, and to node and relationship indexes respectively.

  • Deprecated full-text index syntax, creation and query
call db.index.fulltext.createNodeIndex("Index name",[Label,Label],[attribute,attribute])

call db.index.fulltext.createNodeIndex("nameAndDescription",["Person"],["name","description"])

query
call db.index.fulltext.queryNodes("nameAndDescription","two") yield node, score return node.name,node.age,score
  • Syntax, creation and query of full-text index in use
CREATE FULLTEXT INDEX [index_name] [IF NOT EXISTS]
FOR (n:LabelName[|...])
ON EACH "[" n.propertyName[, ...] "]"
[OPTIONS "{" option: value[, ...] "}"]

CREATE FULLTEXT INDEX titlesAndDescriptions FOR (n:Movie|Book) ON EACH [n.title, n.description]

CALL db.index.fulltext.queryNodes("titlesAndDescriptions", "matrix") YIELD node, score
RETURN node.title, node.description, score

Full text index official website reference: https://neo4j.com/docs/cypher-manual/4.3/administration/indexes-for-full-text-search

2.5 viewing and deleting indexes

show indexes or: schema (you can view indexes and constraints)
Discard: drop index on: person (name) or use: drop index_ name
Discard: drop index on: person (age, gender) or use: drop index_ name
Deprecated: call dB index. fulltext. Drop ("nameanddescription") or use: drop index_ name

3. Constraints

3.1 uniqueness constraints

effect:

  • Avoid duplicate records.
  • Enforce data integrity rules

Create uniqueness constraint

CREATE CONSTRAINT ON (variable:<label_name>) ASSERT variable.<property_name> IS UNIQUE

Specific examples:

Create without constraint name
create constraint on (n:Person) assert n.name is unique

Create with constraint name
create constraint const_name on (n:Person) assert n.name is unique

Delete uniqueness constraint

Discard
drop constraint on (n:Person) assert n.name is unique
 In use
drop constraint constraint_e26b1a8b

3.2 attribute constraint (available in enterprise version)

Discard
create constraint on (n:Person) assert exists(n.name)
In use
create constraint on (n:Person) assert n.name is not null

3.3 viewing constraints

Discard
call db.constraints

In use
show constrain or :schema

Neo4j introduction and basic operation tutorial address: https://mall.csdn.net/item/85530

Added by steve8557 on Tue, 18 Jan 2022 13:35:47 +0200