문제
NEO4J를 배우고 내 장난감 프로젝트는 Twitter로 노는 것입니다.이 작은 스크립트에서는 Python Tweepy와 Py2Neo를 사용하여 하나의 Twitter_user를 가져 와서 모든 친구를 삽입합니다.
def insert_friends(twitter_user):
for friend in Cursor(api.friends, user_id=twitter_user.id_str).items():
n=neo4j.CypherQuery(graph_db,"""
MATCH (user),(friend)
WHERE user.id_str={user_id_str} AND friend.id_str={friend_id_str}
CREATE UNIQUE (user)-[:FOLLOWS]->(friend)
""").execute_one(user_id_str=twitter_user.id_str, friend_id_str=friend.id_str)
.
이것은 잘 작동하지만 최적화 될 수 있습니다.즉, WHERE 절에서는 동일한 사용자를 찾고 있습니다. 매번 동일합니다.매번 추가 조회를 피하는 것은 어떻게합니까?예를 들어, Neo4J에있는 노드가 어떤 노드를 지정하고 NEO4J 내부 노드 ID를 지정할 수있는 PIRIESI를 할 수 있습니까?
해결책
레이블과 인덱스를 사용해야합니다!
즉 :
CREATE INDEX on :User(id_str);
MATCH (user:User),(friend:User) // add labels so it knows to use the index
WHERE user.id_str={user_id_str} AND friend.id_str={friend_id_str}
CREATE UNIQUE (user)-[:FOLLOWS]->(friend);
. 제휴하지 않습니다 StackOverflow