Modelling Data in Neo4j, bidirectional relationships, qualifying relationships with properties vs. relationship types (performance comparison), Neo4j hardware sizing, Cypher vs. Java API
37. Node Record in the Node Store (9 bytes), first bit = inUse flag
Relationship Record in the Relationship Store (33 bytes), first bit = inUse flag, second bit unused
next
relationship
(35 bits)
next
property
(36 bits)
first node
(35 bits)
second node
(35 bits)
type
(16
bits)
first node's
previous
relationship
(35 bits)
first node's
next
relationship
(35 bits)
second
node's first
relationship
(35 bits)
second
node's next
relationship
(35 bits)
next property
(36 bits)
GraphAware
TM
Neo4j Data Layout
49. GraphAware
TM
START
pulpFiction=node({id})
MATCH
(pulpFiction)<-‐[r:RATED]-‐(fan)
WHERE
r.rating
>
3
RETURN
fan
Who liked Pulp Fiction? (Cypher)
50. GraphAware
TM
for
(Relationship
r
:
pulpFiction.getRelationships(INCOMING,
RATED))
{
if
((int)
r.getProperty("rating")
>
3)
{
Node
fan
=
r.getStartNode();
//do
something
with
it
}
}
Who liked Pulp Fiction? (Java)
52. GraphAware
TM
START
pulpFiction=node({id})
MATCH
(pulpFiction)<-‐[r:LIKED|LOVED]-‐(fan)
RETURN
fan
Who liked Pulp Fiction? (Cypher)
53. GraphAware
TM
for
(Relationship
r
:
pF.getRelationships(INCOMING,
LIKED,
LOVED))
{
Node
fan
=
r.getStartNode();
//do
something
with
it
}
Who liked Pulp Fiction? (Java)
70. User 2
User 1
User 3
TRAVELLED_WITH
User 4
TRAVELLED_TOGETHER
FRIEND
TRAVELLED_WITH
weight: 5
weight: 1
weight: 3 weight: 4
GraphAware
TM
Data Model
71. GraphAware
TM
START
from=node:node_auto_index(user_id="{FROM}"),
to=node:node_auto_index(user_id="{TO}")
MATCH
p
=
from-‐[r*1..5]-‐>to
RETURN
extract(n
in
nodes(p)
:
n.user_id),
extract(rel
in
relationships(p)
:
rel.weight),
extract(rel
in
relationships(p)
:
type(rel))
ORDER
BY
length(p),
reduce(totalWeight
=
0,
rel
in
relationships(p)
:
totalWeight
+
rel.weight)
LIMIT
3
72. GraphAware
TM
START
from=node:node_auto_index(user_id="{FROM}"),
to=node:node_auto_index(user_id="{TO}")
MATCH
p
=
from-‐[r*1..5]-‐>to
RETURN
extract(n
in
nodes(p)
:
n.user_id),
extract(rel
in
relationships(p)
:
rel.weight),
extract(rel
in
relationships(p)
:
type(rel))
ORDER
BY
length(p),
reduce(totalWeight
=
0,
rel
in
relationships(p)
:
totalWeight
+
rel.weight)
LIMIT
3
> 1 second