1
votes

using neo4j 2.3 i created a query to find all the nodes that have a relation to tow nodes and create for them a relation to a third node

   MATCH (:group{Id:'1'})-->(b:item)<--(:group{Id:'2'} )
,   (g:ComboGroup{Id:'(1) AND (2))'})
Create (g)-[:HasItem]->( b)

this query never ends
but when i do return instead

  MATCH (:group{Id:'1'})-->(b:item)<--(:group{Id:'2'} )
, (g:ComboGroup{Id:'(1) AND (2))'})
RETURN g, b

i get the proper results with 2709 relationship created

here is the plan i get

enter image description here

now i added a with statement and a limit of to the create and it worked but created 3000 relationships instead of 2709

MATCH (:group{Id:'1'})-->(b:item)<--(:group{Id:'2'} )
,   (g:ComboGroup{Id:'(1) AND (2))'})
WITH b,g limit 3000
Create (g)-[:HasItem]->( b)

I'm dumbfounded from this problem. please help !

1
do you have an index on :group(Id) ?Michael Hunger

1 Answers

1
votes

Because a different query planner is used for read-only queries, there the new COST based planner which is faster and more efficient.

For write queries the previous RULE based planner is used which might not create optimal plans.

Sometimes it might be better (with complex queries that calculate information) to separate the read (compute) operation from the write (update) operation in two queries.

You can return computed values and node-id's from the read query and use them to update the graph afterwards

Update

For your query, do you have indexes on: :group(Id) and :ComboGroup(Id)?

As you want the query to use multiple indexes you will have to add using index after your match clause.

MATCH (a:group{Id:'1'})-->(b:item)<--(c:group{Id:'2'} ),
      (g:ComboGroup{Id:'(1) AND (2))'})
using index a:group(Id) 
using index c:group(Id) 
using index g:ComboGroup(Id)
Create (g)-[:HasItem]->( b)