在我的Spring Boot / Neo4j应用程序中,我有一个很大的Neo4j节点树,现在我想为每个节点实现分层注释。
现在我想到这些评论必须放在哪里......在Neo4j数据库或其他一些外部RDBMS / NoSQL存储中。
为什么我要考虑它 - 因为我现在有限的Cypher知识我面临着删除重Neo4j节点(与其他节点有很多关系的复合节点)的问题。我可以& #39;现在解决这个问题..所以引入新的Comment节点可能完全杀死我的系统。
那么您认为,我应该尝试修复删除问题或在某些外部数据存储中实施评论吗?
已更新
这是我的慢删除查询:
MATCH (d:Decision)
WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r
WITH t, r WHERE NOT (id(t) IN {decisionsIds})
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()-[r3:CONTAINS]-(t2) WHERE r2 <> r
WITH t, r2, t2, r3
WHERE none(x in labels(t)
WHERE x in ['User', 'Decision'])
DELETE t, r2, t2, r3
现在这个查询工作很长时间,所以我害怕将新实体添加到Decision节点,以免完全破坏性能。
有没有办法优化此查询?
答案 0 :(得分:2)
如果没有数据库,这是一次小优化的尝试,不确定它是否有帮助。
了解更多关于域模型和元素之间的基数(例如browser.manage().logs().get('browser').then(function(browserLog) {
if (browserLog.length) {
console.log('Browser console error!');
console.error('log: ' + JSON.stringify(browserLog));
}
});
和d
之间)
t
使用您的数据库进行测试,工作得非常好。关于你的断言,我不确定哪些是违反的。
MATCH (d:Decision) WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r
WITH t, collect(r) as rels
WHERE NOT (id(t) IN {decisionsIds}) AND NOT t:User OR t:Decision
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-(t1)
WHERE NOT (r2 in rels)
OPTIONAL MATCH (t1)-[r3:CONTAINS]-(t2)
DELETE t, r2, t2, r3;
你可以找到你的违规节点:
export decisionsIds=[332,336,335,334,333,340,339,338,337,344,343,342,341]
MATCH (d:Decision) WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r
WITH t, collect(r) as rels
WHERE NOT (t:User OR t:Decision) AND NOT (id(t) IN {decisionsIds})
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR|:CONTAINS]-(t1)
WHERE NOT (r2 in rels)
DELETE t, r2
WITH t1
OPTIONAL MATCH (t1)-[:CONTAINS]-(t2)
// will be detach delete in 2.3
FOREACH ( p IN (t2)--() | DELETE head(rels(p)))
DELETE t2;
+-------------------+
| No data returned. |
+-------------------+
Nodes deleted: 362
Relationships deleted: 723
340 ms
如果您想查看此查询所触及/将删除的所有数据,请使用以下命令:
neo4j-sh (?)$ match (n)-[r]-() where id(n) = 86715 return labels(n),n,r;
+----------------------------------------------------------------------------------------------------------------------------------+
| labels(n) | n | r |
+----------------------------------------------------------------------------------------------------------------------------------+
| ["VoteGroup","_VoteGroup"] | Node[86715]{createDate:"1443124989049",avgVotesWeight:5.0,totalVotesCount:1} | :CONTAINS[173172]{} |
| ["VoteGroup","_VoteGroup"] | Node[86715]{createDate:"1443124989049",avgVotesWeight:5.0,totalVotesCount:1} | :VOTED_ON[173169]{} |
| ["VoteGroup","_VoteGroup"] | Node[86715]{createDate:"1443124989049",avgVotesWeight:5.0,totalVotesCount:1} | :VOTED_FOR[173170]{} |
+----------------------------------------------------------------------------------------------------------------------------------+
然后你可以在结果中找到违规节点,并确保删除了它们周围的所有内容(每个rel类型)。
答案 1 :(得分:0)
怎么样:
MATCH (u:User {id:"Alex"})
MATCH (u)-[r:CREATED_BY]->(n)
OPTIONAL MATCH path = (n)<-[:VOTED_FOR|:VOTED_ON|:CONTAINS]-()
DELETE n,r,path