After years of MS SQL Server, switching to a graph database (I use Neo4j but it's the same concept) requires a mental mode change, but once you get past the "graph epiphany" you'll never want to go back.
After years of MS SQL Server, switching to a graph database (I use Neo4j but it's the same concept) requires a mental mode change, but once you get past the "graph epiphany" you'll never want to go back.
Graph DBs don't do away with JOINs...
they just do the JOIN on INSERT.
1) Wouldn't that have roughly the same performance impact on insert/update/etc. as building indexes? Possibly even more, as building an index (without foreign key constraints) only affects one table, whereas this would imply updating all tables with which this table has relationships?
2) Doesn't this require you to know all of the relationships your data has at insert/update, rather than at select?
2) Yes, very much so.
EDIT: For a bit more clarity on how/why you'd use it.
Graph DBs are very good for something like a social network. You would store each person as an object/document and then you could have a simple array of ids for all that person's friends.
Then when you retrieve the person from the database, the database can automatically retrieve all of the friends very quickly as there is no need to do further searching - it already knows where all the friends are stored as their id/address is already in the document.
In a relational DB, you would have a table of friend->friend and would need to do a one-many join across that table.
To the second, it sounds like if you're willing to give up a substantial amount of the flexibility that SQL gives you, you can achieve performance gains. Isn't that true of any RDBMS, though? If you go through and denormalize heavily, you can make similar tradeoffs (maybe not the exact same ones, so there could be cases where a graph DB would be better, but it's hardly the blanket "joins are bad" that seems to be implied by the original post).
Denormalize is something different, you're misunderstanding something I've said. With a graph DB, you still only have 1 copy of each person - you are just storing the 'joins' or ids/addresses of the 'rows' you want to join with ahead of time.
I certainly did not say that joins are bad, I was just answering your questions as to how a graph db differs and why it would perform better in certain specific circumstances. If you're in those circumstances then a graph DB is very useful.
Another nice part of a graph db is if you have a large collection of different objects in your programming language that all interact with each other. Those interactions can be stored directly in-line and allow for an ORM layer to be more efficient when retrieving the whole collection.
If you want all friends out to some specified distance n where n > 1, recursive CTEs are a much more sane solution that anything involving n levels of OUTER JOINs.
And if you just want specifically friends-of-friends-of-friends by joins, you want three levels of INNER JOINS.
> With a graph DB, you still only have 1 copy of each person - you are just storing the 'joins' or ids/addresses of the 'rows' you want to join with ahead of time.
More accurately, graph DBs store addresses (not ids/addresses) while RDBMS store ids (Foreign Keys) of the records you want to join with ahead of time. This means that to get from one record to a related record takes an additional level of indirection in an RDBMS compared to a graph database, but that doing so from many similar structured records to their equivalent linked records is more efficient in an RDBMS.
> Another nice part of a graph db is if you have a large collection of different objects in your programming language that all interact with each other. Those interactions can be stored directly in-line and allow for an ORM layer to be more efficient when retrieving the whole collection.
With a Graph DB instead of an RDBMS, you obviously wouldn't haven a ORM (Object-Relational Mapping) layer in any case.
At more than one level out
Some distributed graph databases do store ids and not addresses - there is no actual requirement that they be addresses, only that they be efficient to retrieve. So not actually more accurate.
People still call object-db mappers 'ORMs' even when dealing with document databases such as Google's bigtable or MongoDB. It's just an acronym at this point. [1][2]
[1] - http://mongomapper.com/
[2] - https://code.google.com/p/mongo-java-orm/
EDIT: By 'id' I'm referring to some unique hash or key that can be used to locate a document. By 'address' I'm referring to an actual on-disk location that can be directly loaded. Maybe my definitions clash with yours, I don't think this stuff is formally defined.
I'm not a big fan of tradition for its own sake.
> and many relational databases certainly don't have them.
The only major SQL-based RDBMSs I can think of off the top my head that don't support recursive CTEs in their current version are SQLite and MySQL -- Postgres, Firebird, DB2, Oracle, and MS SQL Server all do.
Huh? Could you expand on this?
Okay, I see what you are saying. So, what you'd like is something like NATURAL JOIN that instead of using matching column names to infer join conditions it would use declared FK relations. That would be convenient -- but possible complicated by the possibility of multiple different FKs from one table to another (though the syntax could involve specifying the FK rather than the target table, which would solve that.)
e.g., if you had
CREATE TABLE emp (
emp_id INTEGER PRIMARY KEY,
manager_id INTEGER FOREIGN KEY REFERENCES emp JOIN AS manager,
hr_rep_id INTEGER FOREIGN KEY REFERENCES emp JOIN AS hr_rep,
name VARCHAR(50),
...
);
You could do something like: SELECT emp.name AS "Employee", manager.name AS "Manager"
FROM emp
NAMED JOIN emp.manager;If you want to do it with a minimum of new syntax, you could defer naming the join until the select, as you normally would:
CREATE TABLE emp (
emp_id INTEGER PRIMARY KEY,
manager_id INTEGER FOREIGN KEY REFERENCES emp,
hr_rep_id INTEGER FOREIGN KEY REFERENCES emp,
name VARCHAR(50),
...
);
SELECT emp.name AS "Employee", manager.name AS "Manager"
FROM emp
NAMED JOIN emp.manager_id AS manager;
I hadn't given any thought to the syntax, but that looks pretty nice and tidy.Edit: NAMED JOIN isn't really appropriate anymore with that change in syntax. Something like DECLARED JOIN might be better.
But I can see it either way.
All I'm interested in is making the queries more concise. Otherwise, I like the loose coupling between related entities in the relational model. I would have been hesitant to add join information to the table declaration if it wasn't already there. But since it's already there as a constraint, you might as well get all the mileage you can out of it.
2) exactly. The tradeoff is that SQL lets you easily do ad-hoc analysis (which isn't an important use case for a lot of applications). But also in SQL you do have to know about foreign key relationships at insert/update time to validate those.