优化密码查询

时间:2012-12-13 15:04:04

标签: neo4j cypher

如何优化此密码查询?它比使用Gremlin的类似查询慢3-4倍。

START movie=node:vertices(movieId="100") 
MATCH genera1<--movie<--()-[ratedRel:rated]->anotherMovie-->genera1 
WHERE ratedRel.stars > 3 
RETURN anotherMovie.title as title, anotherMovie.movieId as id, 
genera1.genera as genera, 
COUNT(anotherMovie) as count ORDER BY count(anotherMovie) DESC LIMIT 20;

我只是想要检索评级超过3星且与START节点具有相同属性的电影: http://markorodriguez.files.wordpress.com/2011/09/movielens-schema.png?w=350

我正在控制台中运行查询,而我正在使用Neo4j 1.9

Gremlin查询:

m = [:];
x = [] as Set;
v = g.v(node_id);

v.out('hasGenera').aggregate(x).back(2).inE('rated').
filter{it.getProperty('stars') > 3}.outV.outE('rated').
filter{it.getProperty('stars') > 3}.
inV.filter{it != v}.
filter{it.out('hasGenera').toSet().equals(x)}.
groupCount(m){\"${it.id}:${it.title.replaceAll(',',' ')}\"}.iterate();

m.sort{a,b -> b.value <=> a.value}[0..24];

1 个答案:

答案 0 :(得分:0)

START movie=node:vertices(movieId="100") 
MATCH movie-->genera1<-anotherMovie<-[ratedRel:rated]-user
WHERE ratedRel.stars > 3 
RETURN anotherMovie.title as title, anotherMovie.movieId as id, genera1.genera as genera, 
COUNT(ratedRel) as cnt ORDER BY cnt DESC LIMIT 20;

<强>更新 根据你的gremlin查询,你能试试这个吗?

START movie=node:vertices(movieId="100") 
MATCH movie-[:hasGenera]->genera1<-[:hasGenera]-anotherMovie<-[ratedRel:rated]-user
WITH anotherMovie,count(ratedRel) as allVotes, sum(ratedRel) as allStars,genera1
WHERE allStars/allVotes>3 
RETURN anotherMovie.title as title, anotherMovie.movieId as id, genera1.genera as genera, 
allStars ORDER BY allStars DESC LIMIT 20;

关键是要在路径中尽可能多地定义元素(在这种情况下我们错过了rels名称)并省略了user节点,我不知道如何在cypher中执行此操作但是它显然没有在gremlin代码中列出。