Relational “H2″ vs. Graph “Neo4j”

We will test the performance of Neo4j graph database against a relational database. I have selected H2 to represent the relational database community because it is light and assumed to be very fast. So, simply we will build a data model that represents relations among a set of people in both databases. Then we will test their performance by trying to ask “query” some questions.

The Story:

We have a set of 1000 persons each of them knows exactly 50 persons from the same set. We will build this data model in both databases using the below Java application. The relation between each person to his 50 friends will be randomly generated but it will be same on both databases.

Objective:

  • Test the performance of Neo4j against relational database “H2″.

Environment:

  • Eclipse (Indigo)
  • Neo4j “embedded”
  • H2 “embedded”

Libraries:

  • Neo4j jars under lib and system/lib folders
  • H2 jar under bin folder

( 1 ) Java Project

  • Neo4j.java: Builds data model in neo4j database
  • Neo4jServer.java: Starts internal server to run web console to view data store in neo4j
  • H2.java: Build data model in H2 database
  • FriendMaker.java: Randomly pick 50 friends for each person.
  • Main.java: Runs the code to build data model in both database.

Note: the role of Java application will ends once data models are generated.

Download Code: RelationalvsGraph.zip

( 2 ) H2 Relational Data Model

After running the Java code the following tables will be created and populated.

  • //CREATE TABLE PERSON(ID INT PRIMARY KEY, NAME VARCHAR(50));
  • //CREATE TABLE FRIEND_OF(PRS_ID INT, FRND_ID INT);

Run the H2 Console to view and query the data

Note: to view all FRIEND_OF data select Max rows: “All”

( 3 ) Neo4j Graph Data Model

After running the Java code 1000 nodes and 50000 “1000*50” relations will created.

Start Neo4jServer.java to run Neo4j webadmin “web console”

Note: we have 1001 = 1000 “we created” + 1 root

neo4j-h2

partial view of the complete graph data model

( 4 ) Time to Test

A. Find all friends of person-6

H2

SELECT * FROM FRIEND_OF WHERE PRS_ID = 6;

Neo4j

  • > start n = node(6)
  • > match n-[:FRIEND_OF]->m
  • > return m;

B. Find all friends of person-6's friends "friends of friends"

H2

SELECT * FROM FRIEND_OF F1, FRIEND_OF F2
WHERE F1.PRS_ID = 6 AND F2.PRS_ID = F1.FRND_ID;

Neo4j

  • > start n = node(6)
  • > match n-[:FRIEND_OF]->()-[:FRIEND_OF]->m
  • > return m;

C. Find all friends of all friends of person-6's friends "friends of friends of friends"

H2

SELECT * FROM
FRIEND_OF F1,  FRIEND_OF F2, FRIEND_OF F3
WHERE F1.PRS_ID = 6 AND F2.PRS_ID = F1.FRND_ID AND F3.PRS_ID = F2.FRND_ID;

Neo4j

  • > start n = node(6)
  • > match n-[:FRIEND_OF]->()-[:FRIEND_OF]->()-[:FRIEND_OF]->m
  • > return m;