How to Increment consistently in Neo4j, Cypher is too slow?

1ManStartup

Im currently using Neo4j 2.0+ and cypher to create and save sessions.

My project at times requires multiple writes per second to a node labeled 'ChildSession', and I notice that when I 'increment' the ChildSession_ID in cypher, I often have ChildSession_ID's skipping numbers or being the same number.

Not sure if neo4j/cypher is too slow for my requirements, but I doubt it since the internal Neo4j Node ID's are incremented normally.

The cypher command i'm using, to increment ChildSession is:

 match (p:ChildSession) with count(p) as Total 
 Create (b:ChildSession{ChildSession_ID:Total + 1 })

One would expect the ChildSession_ID to increment, but I get the following results, when I check the nodes in neo4j browser:

match (u:ChildSession) return u,ID(u)

Results:
ChildSession_ID 1
44997
ChildSession_ID 1
44998
ChildSession_ID 1
44999
ChildSession_ID 4
45000
ChildSession_ID 5
45001
ChildSession_ID 6
45002
ChildSession_ID 6
45003
ChildSession_ID 8
45004
ChildSession_ID 8
45005

I've been unable to get neo4j to increment accurately. I tried using redis and its hincrby command which increments and then put this variable into my cypher query's ChildSession_ID attribute. This method works, but I would rather do it with cypher instead.

The argument could be made to use redis only instead for fast writes, but I use a hiearchy of session levels and need the querying neo4j offers.

Thanks.

JohnMark13

Have you added any manual locking around access to the ChildSession label? I would expect otherwise that the MATCH statement could happen on multiple threads at the same time (no read locking). The result of this would be the same value for Total being passed to your CREATE statement.

Two things spring to mind, using the COUNT is maybe not the fastest and would not guarantee unique values if you were ever to delete a ChildSession (maybe you do not care?). Maintaining the count as a property is probably faster (query time) and incurs minimal overhead.

I believe that the correct way to achieve this is to use a MERGE statement on a separate label which should lock on the Incremental Node through the whole transaction (as described in this Blog Post):

MERGE (nid:Incremental{type:ChildSession})
ON CREATE SET nid.count = 1
ON MATCH SET nid.count = nid.count + 1
RETURN nid.count

Caveat: Whilst I believe this is the correct way to do it, I'll be damned if it is working nicely in a rapid async environment, suggesting that the semantics of the locking are not what I expect.

You could of course generate a UUID if the incremental nature of your ChildSession_ID is not important?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Neo4j Bolt is not consistently processing Cypher queries

From Dev

Neo4j StatementResult too slow

From Dev

Neo4j cypher query is really slow

From Dev

Cypher / neo4j: Too many neighbours

From Dev

Neo4j breadth first search is too much slow

From Dev

how to get a list of node properties with cypher in neo4j

From Dev

Neo4j - Cypher: How to get random pairs of permotations

From Dev

neo4j cypher: how to change the type of a relationship

From Dev

How to group and count relationships in cypher neo4j

From Dev

neo4j cypher how to get rid of duplicate in count

From Dev

How to return random records in Neo4j using Cypher?

From Dev

Neo4j, Cypher: How to update a property name?

From Dev

How to "replace" a relationship in Neo4j using Cypher?

From Dev

how to add a property to existing node neo4j cypher?

From Dev

How to create multiple nodes with cypher in neo4j

From Dev

How to create unique CONSTRAINT to relationship by neo4j cypher?

From Dev

How to change/update a label in Neo4j - Cypher?

From Dev

How to add relation correctly to neo4j (cypher)?

From Dev

How to increase the speed of cypher queries in neo4j?

From Dev

How to query collection using cypher in neo4j?

From Dev

How to 'flatten' the result in Neo4j cypher?

From Dev

Neo4j Cypher - How to limit 50% of MATCH results

From Dev

Neo4j Cypher - How to limit 50% of MATCH results

From Dev

neo4j how to return all node labels with Cypher?

From Dev

neo4j cypher : how to query a linked list

From Dev

How to use START with Cypher / Neo4j 2.0

From Dev

how to add a property to existing node neo4j cypher?

From Dev

How to drop a relationship type in Neo4j's Cypher

From Dev

How represent this query with Cypher in Neo4j

Related Related

HotTag

Archive