Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polyglot persistece with a graph database for relationships is a good ideia?

I would like to know if worth the idea of use graph databases to work specifically with relationships.

I pretend to use relational database for storing entities like "User", "Page", "Comment", "Post" etc.

But in most cases of a typical social graph based workload, I have to get a deep traversals that relational are not good to deal and involves slow joins.

Example: Comment -(made_in)-> Post -(made_in)-> Page etc...

I'm thinking make something like this:

Example:

User id: 1

Query: Get all followers of user_id 1

  • Query Neo4j for all outcoming edges named "follows" for node user with id 1
  • With a list of ids query them on the Users table:

    SELECT * FROM users WHERE user_id IN (ids)

Is this slow?

I have seen this question Is it a good idea to use MySQL and Neo4j together?, but still cannot understand why the correct answer says that that is not a good idea.

Thanks

like image 208
Luccas Avatar asked Nov 17 '25 17:11

Luccas


1 Answers

Using Neo4j is a great choice of technologies for an application like yours, that requires deep traversals. The reason it's a good choice is two-fold: one is that the Cypher language makes such queries very easy. The second is that deep traversals happen very quickly, because of the way the data is structured in the database.

In order to reap both of these benefits, you will want to have both the relationships and the people (as nodes) in the graph. Then you'll be able to do a friend-of-friends query as follows:

START john=node:node_auto_index(name = 'John') MATCH john-[:friend]->()-[:friend]->fof RETURN john, fof

and a friend-of-friend-of-friend query as follows:

START john=node:node_auto_index(name = 'John') MATCH john-[:friend]->()-[:friend]->()->[:friend]->fofof RETURN john, fofof

...and so on. (Same idea for posts and comments, just replace the name.)

Using Neo4j alongside MySQL is fine, but I wouldn't do it in this particular way, because the code will be much more complex, and you'll lose too much time hopping between Neo4j and MySQL.

Best of luck!

Philip

like image 173
Philip Rathle Avatar answered Nov 19 '25 10:11

Philip Rathle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!