Optimize Neo4j Cypher query

前端 未结 2 1567
走了就别回头了
走了就别回头了 2021-01-23 21:21

What I\'m doing is to get all profiles* who has a specific directed relation to a users profile* and if those have an alternate profile* get those in

相关标签:
2条回答
  • 2021-01-23 21:46

    You're using WHERE clauses where you don't need to. Let's look at the first one for example:

    WHERE User-->ProfileA-->ProfileB 
    

    This clause says "restrict the results only to users that have a relationship to a ProfileA which itself has a relationship to a ProfileB". However, that is already guaranteed to be true by your match clause. You're wasting CPU cycles re-verifying something that is already true.

    WITH ProfileA, rel, ProfileB 
    

    You aren't doing any sort of aggregation, calculation or reassignment, so there is no need for this WITH clause. You can continue on without it.

    WHERE relB IS NULL OR User-->ProfileA-->ProfileB<-->ProfileB2<--ProfileA2<--User
    

    Again, you're not getting any value out of this WHERE clause. This one says "restrict the results to paths where a relB wasn't found OR where one was found with the following path..." and then you list the exact same path that was in your MATCH.

    So, remove all those extraneous clauses and you get this:

    START User=node({source}) 
    MATCH User-[:profile]->ProfileA-[rel:related]->ProfileB<-[?:me]->ProfileB2<-[relB?:related]-ProfileA2<-[:profile]-User 
    RETURN ProfileB, COLLECT(ProfileB2), rel, relB
    LIMIT 25
    

    Try that and see if the performance is any better. If it's not enough then you may need to add more information to your question -- for my own part, I don't fully understand what your relationships actually mean (for example, what is the "me" relationship? what does it symbolize?)

    0 讨论(0)
  • 2021-01-23 21:46

    This is how I solved it:

    START User=node({source}) 
    MATCH User-[:profile]->ProfileA-[rel:related]->ProfileB<-[?:me]->ProfileB2-[relB?:related]-ProfileA2
    WHERE relB IS NULL OR User-[:profile]->ProfileA2
    RETURN ProfileB, COLLECT(ProfileB2), rel, relB
    LIMIT 25
    

    The ProfileA2<-[:profile]-User seemed to produce an endless loop.

    Recommendations are still welcome.

    0 讨论(0)
提交回复
热议问题