2017-07-31 52 views
0

這裏是我的查詢:爲什麼cypher在neo4j管理面板中的查詢速度比在Rails應用程序中快?

MATCH (me:User{username: 'username'})-[:user_posted_post|user_share_post|post_in_gr|post_in_page]->(posts1:Post) WHERE posts1.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts1) return posts1 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[{subscribe: '1'}]-()-[:post_in_gr]-(posts3:Post)-[:user_posted_post|user_share_post]-(User{status:'active'}) WHERE posts3.access IN ['everybody', 'friend'] AND posts3.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts3) RETURN posts3 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[{subscribe: '1'}]-()-[:post_in_page]-(posts4:Post)-[:user_posted_post|user_share_post]-({status:'active'}) WHERE posts4.access IN ['everybody', 'friend'] AND posts4.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts4) RETURN posts4 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[:user_author_page]-()-[:post_in_page]-(posts5:Post)-[:user_posted_post|user_share_post]-({status:'active'}) WHERE posts5.access IN ['everybody', 'friend'] AND posts5.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts5) RETURN posts5 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[{subscribe: '1'}]-({status:'active'})-[:user_posted_post|user_share_post]-(posts6:Post{system_type: 'page'}) WHERE posts6.access IN ['everybody', 'friend'] AND posts6.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts6) RETURN posts6 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[{subscribe: '1'}]-({status:'active'})-[:user_posted_post|user_share_post]-(posts7:Post) WHERE posts7.access IN ['everybody', 'friend'] AND NOT (posts7)-[:post_in_gr]-() AND NOT (posts7)-[:post_in_page]-() AND posts7.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts7) RETURN posts7 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[:user_create_com]-()-[:com_in_post]-(posts8:Post) WHERE posts8.access IN ['everybody', 'friend'] AND posts8.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts8) RETURN posts8 as posts 
     UNION 
     MATCH (me:User{username: 'username'})-[:user_can_see_post|user_posted_post]-(posts9:Post) WHERE posts9.access = 'custom' AND posts9.hidden_id IS NULL AND NOT (me)-[:user_hide_post]-(posts9) RETURN posts9 as posts").to_a 

此查詢需要在管理面板約1300毫秒,但在Rails應用程序相同的查詢需要超過4000毫秒。

在rails應用程序中,我使用Neo4j :: Session.query來進行此查詢。

回答

0

我使用Python和Java相比,也有類似的問題。不幸的是,我無法專門與Rails集成,但是,我可以提供一些關於性能的想法:

1)檢查查詢的配置文件。請記住,你實際上是在執行8個唯一查詢 2)來看看有多重關係的查詢和未標記節點

例如:

MATCH(我:用戶{用戶名:「用戶名」}) - [:user_author_page] - () - [:post_in_page] - (posts5:Post) - [:user_posted_post | user_share_post] - ({status:'active'})WHERE posts5.access IN ['everybody','friend'] AND posts5.hidden_​​id IS NULL AND NOT(me) - [:user_hide_post] - (posts5)RETURN posts5 as posts

3)使用另一個驅動程序嘗試使用另一種語言的相同查詢。也許它的Ruby驅動程序很臭?與Java相比,python節點花費30-40倍的時間將節點轉換爲本地python對象。

https://fauie.com

+0

你是對的。這是我發現的。我已經添加到每個查詢LIMIT語句,它的工作速度更快,大約1秒。 所以,現在我想要做這8個查詢,然後,通過updated_at字段排序,並且只取前10個。我該怎麼做? –

+0

你在說什麼結果?假設您想在最終的最終結果集中獲得10個結果。如果您將這8個參數中的每一個設置爲10,您將以80個「臨時最終」結果結束。如果是我,第一選擇就是在你的ruby代碼中排序並獲得最新的10個。您介意將我的答案標記爲已接受嗎? – chrisfauerbach

相關問題