21

There is some hype around graph databases. I'm wondering why.

What are the possible problems that one can be confronted with in today's web environment that can be solved using graph databases? And are graph databases suitable for classical applications, i.e. can one be used as a drop-in replacement for a Relational Database? So in fact it's two questions in one.

Related: Has anyone used Graph-based Databases (http://neo4j.org/)?

Community
  • 1
  • 1
amirouche
  • 7,682
  • 6
  • 40
  • 94
  • You will find some answers in these two stackoverflow threads: - [What are some examples of problems that are best solved with graphs?](https://stackoverflow.com/questions/703999/what-are-some-examples-of-problems-that-are-best-solved-with-graphs) - [Have anyone used Graph based Databases : http://neo4j.org/](https://stackoverflow.com/questions/1000162/have-anyone-used-graph-based-databases-http-neo4j-org) Regarding classical apps, this Neo4j wiki page could be of interest: [Domain Modeling Gallery](http://wiki.neo4j.org/content/Domain_Modeling_Gallery) (I wrote it). – nawroth Jul 21 '09 at 21:54

2 Answers2

21

Many relational representations of graphs aren't particularly efficient for all operations you might want to perform.

For example, if one wants the connected set of all nodes where edges satisfy a given predicate, starting from a given node, there's no natural way in SQL to express that. Likely you'll either do a query for edges with the predicate, and then have to exclude disconnected edges locally, or have a very verbose conversation with the database server following one set of links to the next in iterated queries.

Graphs aren't a general replacement for relational databases. RDBs deal primarily in sets (tables), while graphs are primarily interesting because of the "shape" of interconnections. With relational DBs you follow links of a predetermined depth (a fixed number of joins) between sets, with results progressively filtered and grouped, while graphs are usually navigated to arbitrary and recursively-defined depth (i.e. not a predetermined number of "joins"). You can abuse either to match the characteristics of the other, but they'll have different strengths.

Barry Kelly
  • 41,404
  • 5
  • 117
  • 189
  • Transitive closure may not be part of the SQL standard (and is presumably hard to implement in the general case, or more vendors would have done it) but it is not hard to implement for a specific application using stored procedures. – finnw Jul 21 '09 at 13:50
  • 3
    For sure; but having to write ad-hoc queries as stored procedures can put a crimp in your style. – Barry Kelly Jul 21 '09 at 13:52
  • 3
    @finnw The problem isn't being able to do it, the problems are efficiency and performance. To gain good read performance you'd have to sacrifice insert performance and waste lots of disk space. This article: http://www.codeproject.com/KB/database/Modeling_DAGs_on_SQL_DBs.aspx outlines how this can be done using stored procedures for inserts and common SQL for reads. – nawroth Jul 21 '09 at 22:20
  • And I believe also how elegant/ natural one can solve problems. That's why people prefer different programming languages, and that's why people might prefer different data stores. – Eelco Jun 09 '11 at 19:56
  • You *can* do transitive closure in standard SQL - you can use recursive common table expressions. I've used them on PostgreSQL and MS SQL Server, and i believe they are supported by other leading RDBMSs. The syntax is slightly clunky, but they're really good fun once you get the hang of them. I suspect they aren't as fast as the equivalent queries in a graph database, though. – Tom Anderson Aug 08 '12 at 17:30
  • @TomAnderson quite right, and I have used them in Postgres. But they are essentially query iteration server-side. Without specific indexes, they won't be as fast as something purpose-built. – Barry Kelly Feb 27 '14 at 16:07
4

In my opinion, social networking sites may benefit from graph databases because graph is a natural way of storing connections between users.

empi
  • 15,755
  • 8
  • 62
  • 78