Determining the "Join Bomb" with Graph Queries
Determining the "Join Bomb" with Graph Queries
Join the DZone community and get the full member experience.Join For Free
Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%!
Curator's Note: The content of this post was originally written by Rick Van Bruggen on the Neo4j blog.
For the past couple of months, and even more so since the beer post, people have been asking me a question that I have been struggling to answer myself for quite some time: what is so nice about the graphs? What can you do with a graph database that you could not, or only at great pains, do in a traditional relational database system. Conceptually, everyone understands that this is because of the inherent query power in a graph traversal - but how to make this tangible? How to show this to people in a real and straightforward way?
And then Facebook Graph Search came along, along with it’s many crazy search examples - and it sort of hit me: we need to illustrate this with *queries*. Queries that you would not - or only with a substantial amount of effort - be able to do in traditional database system - and that are trivial in a graph.
This is what I will be trying to do in this blog post, using an imaginary dataset that was inspired by the Telecommunications industry. You can download the dataset here, but really it is very simple: a number of “general” data elements (countries, languages, cities), a number of “customer” data elements (person, company) and a number of more telecom-related data elements (operators - I actually have the full list of all mobile operators in the countries in the dataset coming from here and here, phones and conference call service providers).
So to start of with: what would this data set look like in a relational model?
What is immediately clear is that there is *so* much overhead in the model. In order to query anything meaningful from this normalised rdbms, you *need* to implement these things called “join tables”. And really: these things stink. It is just an example of what a poor match the relational model is to the real world - and the complexity it introduces when you start using it.
Compare this to the elegance of the graph model:
It is such a good match to reality - it is just great. But the beauty is not just in the model - it’s in what you do with the model, in the queries.
So let’s how we could ask some very interesting, Facebook style, queries of this model:
- Find person in London who speaks more than one language and who owns an iPhone5
- Find a city where someone from Neo Technologylives who speaksEnglish and hasThree as his operator
- Find a city where someone from Neo Technologylives who speaksEnglish and hasThree as his operator in the city that he lives in
- Find a person, not living in the Germany, who roams to more than 2 countries and who emailspeople who live in London
- Find a person who roams to more than 2 countries and who lives in the USA and uses a ConferenceCallService there
So let’s explore a couple of examples with some real world queries. The first thing to realise here is the relevance of a very important concept in the world of databases, and more specifically so in the world of Graph Databases - the use of Indexes. In a traditional database, indexes are expensive but indispensable tools to quickly find the appropriate records in a table using a “key”. And when joining two tables, the indexes on both tables would need to be scanned completely and recursively to find *all* the data elements fitting the query criteria. This is why “joins” are so expensive computationally - and this is also why graph queries are so incredibly fast for join-intensive requests. The thing is, that in a graph database, you *only* use the index on the data *once*, at the start of the query - to find your starting points of the “traversals”. Once you have the starting points, you can just “walk the network” and find the next data element by hopping along the edges/relationships, and NOT using any indexes. This is what we call “index-free adjacency” - and it is a fundamental concept in understanding graph traversals.
In the example below you can see that we are using three index lookups (depicted in green, and I even added a nice little parachute symbol to illustrate what we are doing here) to “parachute” or land into the network, and start walking the graph from there.
The query above is to look for “a city where someone from Neo Technology lives that speaks English and has Three as his operator in the city that he lives in.
|These are the three parachutes, landing by doing an index lookup for nodes using the node_auto_index of Neo4j.|
|Here we describe the pattern that we are looking for. From the three starting points, we are looking for a city that has very specific, directed relationships that need to match this pattern.|
|We return the city’s name and the person’s name as a result set from this query.|
|order by city.name;||and order them by the name of the city|
Here are are looking for two people in the same countries but on different home operators that call, mail or text each other.
|Here we use just one index lookup to find a “country” and then we start looking for the pattern.|
|The pattern in this case is quite a complex one, we quite a few hops on the different relationship types.|
|Here we limit the results to a specific condition that has to be applied.|
distinct person.name, samecountry.name;
|And here we return the distinct set of name of the person and the countries’ name.|
The nice thing about this way of querying is that in principle, its performance is extremely scalable and constant: we will not suffer the typical performance degradation that relational databases suffer when doing lots of joins over very long tables. The reason for this is simple: because we only use the indexes to find the starting points, and because the other “joins” will be done implicitly by following the relationships from node to node, we can actually know that performance will remain constant as the dataset grows. Finding the starting point may slow down (a bit) as the index grows, but exploring the network will typically not - as we know that not everything will be connected to everything, and the things unconnected to the starting nodes will simply “not exist” from the perspective of the running query.
Obviously there a lot more things to say about graph queries, but with these simple examples above, I hope to have given you a nice introduction as to where exactly the power of graph traversals is - and it’s in these complex, join-intensive queries.
Rik Van Bruggen.
Published at DZone with permission of Andreas Kollegger , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.