Which would be faster. Using template.fetch(), which probably would be a line in a Service class of mine, or creating a repository interface method with a Cypher query. I would assume fetch creates a cypher query, probably the same one I would put on my repo interface method.
OK, as I was posting, I went and did some very very basic testing. I mean small scale, so larger scale could be quite different. But this is what I found. I have an integration JUnit test that queries three users for their friends. One node away. The three users are friends amongst themselves. auser friends cuser, buser friends cuser and therefore cuser is friends with both a and b users.
Here is that test code
Code:
@Test
@Transactional
public void testFindFriends() {
User aUser = userRepository.findByLogin("auser");
User bUser = userRepository.findByLogin("buser");
User cUser = userRepository.findByLogin("cuser");
List<User> friends = userRepository.findFriends(cUser, new PageRequest(0,10)).getContent();
//Set<User> friends = template.fetch(cUser.getFriends());
System.out.println("cusers friends: " + friends);
assertEquals("cUser should have two friends", 2, friends.size());
assertTrue("cuser should be friends with buser", friends.contains(bUser));
assertTrue("cuser should be friends with auser", friends.contains(aUser));
friends = userRepository.findFriends(aUser, new PageRequest(0,10)).getContent();
//friends = template.fetch(aUser.getFriends());
System.out.println("ausers friends: " + friends);
assertEquals("aUser should have one friend", 1, friends.size());
assertTrue("auser one friend should be cuser", friends.contains(cUser));
friends = userRepository.findFriends(bUser, new PageRequest(0,10)).getContent();
//friends = template.fetch(bUser.getFriends());
System.out.println("busers friends: " + friends);
assertEquals("bUser should have one friend", 1, friends.size());
assertTrue("buser one friend should be cuser", friends.contains(cUser));
}
When running it via a repo method
Code:
@Query("start user=node({0}) " +
"match (user)-[:FRIEND]-(friends) " +
"return friends " +
"order by friends.lastName asc")
public Page<User> findFriends(User user, Pageable page);
I saw unit test run times between 4.2 ms to 4.5 ms
When I tried the same but using the template.fetch(auser.getFriends()) etc, I had some faster times, but also a bigger range of times that were slower than a repo.
So with template I had run times between 4.0ms to 5.0ms.
Remember all, metrics like this should always be taken with a grain of salt. I tried to run it at least 20 times each, but the sampling rate and data is small.
Thanks
Mark