193 points · by gsky · 1y ago · 77 comments
Maybe I'm just not skilled enough to handle such a workload with Postgres. But Neo4j handled it easily
https://github.com/joelonsql/graph-query-benchmarks
I haven't tried the latest versions of both databases though.
Any sense if ClickHouse can scale to several TB of data, and serve giant queries on the last hour’s data across all sensors without getting OOMKilled? We’re also looking at some hacked together DuckDB abomination, or pg_duck.
What does your testing strategy look like with bigquery? We use snowflake, but the only way to iterate and develop is using snowflake itself, which is so painful as to impact the set of features that we have the stomach to build.
I presume that for larger real world graph datasets, maybe there's some better algorithms and storage methods. I couldn't figure out neo4j fast enough, and it wasn't clear that I could map all of the stuff like block modeling into it anyways, but it would be very useful for someone to figure out a better production ready storage backend for networkx at least where some of the data could be cached in SQLite3.
This reminds me of "Just Use Postgres for Everything": https://news.ycombinator.com/item?id=33934139
My first thought is sharding and materialized views.
For timeseries I'd argue with timescale there is very few use cases that ever outgrow postgres. But I might be biased.
If you plan for it, for example store your graph in an export friendly format it should be ok.
Using postgres as long as you can get away with it is a no brainer. It’s had so many smart people working on it for so long it really comes thru. It’s nice to see the beginner or intro level tools for it evolving quickly similar to what has made MySQL approachable for so many years.
Indeed this is coming to Postgres eventually.
https://www.postgresql.org/message-id/flat/a855795d-e697-4fa...
https://ashutoshpg.blogspot.com/2024/04/dbaag-with-sqlpgq.ht...
"pgRouting library contains following features:
All Pairs Shortest Path, Johnson’s Algorithm
All Pairs Shortest Path, Floyd-Warshall Algorithm
Shortest Path A*
Bi-directional Dijkstra Shortest Path
Bi-directional A\* Shortest Path
Shortest Path Dijkstra
Driving Distance
K-Shortest Path, Multiple Alternative Paths
K-Dijkstra, One to Many Shortest Path
Traveling Sales Person
Turn Restriction Shortest Path (TRSP)"\*
see more: https://docs.pgrouting.org/latest/en/pgRouting-concepts.html...Just check the Workshop : https://workshop.pgrouting.org/3.0/en/index.html
Pedestrian Routing:
https://workshop.pgrouting.org/3.0/en/basic/pedestrian.html#...
Graph views:
https://workshop.pgrouting.org/3.0/en/basic/graph_views.html
Create a Network Topology:
https://workshop.pgrouting.org/3.0/en/advanced/chapter-12.ht...
I forgot to say that it's also obviously only a tree, not a graph. It's still useful for some cases.
It has a very good way of querying which is probably fast if you have very deep trees but in the end we didn't need the deep trees so it was chosen for the wrong reasons.
https://www.postgresql.org/docs/current/ltree.html
You essentially have a path field in your record which lists the ids of all the records that are parents of the current one plus the current one.
e.g. if your records are regions of the world and the id is the country name then the path might look like this:
Earth.Europe.Poland
There is support for queries like "what are all the descendants of Europe"
SELECT name FROM regions WHERE path <@ 'Earth.Europe';
Or you can use matching: SELECT name FROM regions WHERE path ~ '*.Europe.*';That said - i WISH people would embrase the existing GraphDatabases more and make the hosters support them as standard, rather than abusing existing relational databases for graph purposes.
And to make it clear,i'm not talking about my own experimental one, i mean stuff like Neo4j, OrientDB etc.
In my experience, the vast majority of graphs can be embedded in relational databases just fine and most people don't want general graph querying. People just don't like optimizing queries (or equivalently the schema to enable such queries).
I personally have never seen a pitch for graph databases that makes them seem attractive for more than data exploration on your local machine.
I don't think that GraphDBs should a default choice, but there are cases in which they just perform better.
Could i write my research project with a relational DB? Yes - i tried - and it sucked xD
https://docs.aws.amazon.com/neptune/latest/userguide/migrati...
Also, the last time i checked neptune, it could only be run in AWS but there was no public application image for local tinkering/dev. But its some years ago to be fair - maybe have changed.
Sure you can represent a GraphDB in a RDBMS - but graphdbs are optimized for their specific use case. Therefor for example locking of resources is optimized for said purpose. When going a "Edge" and "Node" table approach, locking is by default rather unoptimized in many RDBMS. Just one simple example.
If your text processing stack works good enough, and JSON only solves edge cases, then I think its a legit argument against adding another tech to the stack.
Its not like everyone immediately switched over to JSON even if they could have.
And the analogy may not be the best - in the case of graph databases, they don't do what RDBMS can do, at least not as efficiently.
So if we need some graph functionality in our RDBMS, do we split the database and use a "real" graph database or just incorporate the graph functionality into the existing DB? The choice seems quite easy for me, because there are whole host of issues in splitting DBs (duplication of information, inconsistency, etc.)
I wrote about how we use it here: https://www.yetto.app/blog/post/how-labels-work/
That makes it good for relatively static and flat data like your label hierarchy, which probably receives a new label or a repainting within a tree relatively infrequently, and might have a p95 depth of 10. That makes it also a good fit for actual human (parent, child) relationships since parent-child change is usually infrequent and the total children per layer in the tree is also small.
For a tree like a Notion workspace where depth can be quite deep, and layers can easily have hundreds or hundreds of thousands of siblings, the cost at write-time when you reparent an ancestor in the worst case isn’t feasible.
For something like a social graph I’m not sure how to use ltree at all since a social graph isn’t a dag.
First-party data structure for tree (directed, acyclic graph) data
https://m.youtube.com/watch?v=QDdTbhSR2Vo
https://duckdb.org/community_extensions/extensions/duckpgq.h...
The approach I came up with was to instead use MSSQL hierarchy IDs (there's a paper describing them, so they can be ported to any database). This specifically optimizes for ancestor-of/descendant-of queries. The gist of it is:
1. Find all cycles/strong components in the graph. Store those in a separate table, identifying each cycle with an ID. Remove all the nodes in the cycle, and insert a single node with their cycle ID instead (turning the graph into a DAG). The reason we do this is because we will always visit every node in a cycle when doing an ancestor-of/descendant-of query.
+---+
+-------> B +------+
+-+-+ +-^-+ +-v-+
| A | | | C |
+---+ | +-+-+
+-+-+ |
| D <------+
+---+
Becomes: +---+ +---+
| A +--> 1 |
+---+ +---+
+---+---+
| 1 | B |
| 1 | C |
| 1 | D |
+---+---+
2. You effectively want to decompose the DAG into a forest of trees. Establish a worklist and place all the nodes into it. The order in this worklist may be something that can affect performance (you want the deepest/largest tree first). Grab nodes out of this worklist and perform a depth-first search, removing nodes from the worklist as you traverse them. These nodes can then be inserted into the hierarchy ID table. You should insert all child nodes of each node, even if it has already been inserted before, but only continue traversing downwards if the node hasn't yet been inserted. +---+
+----> B +----+
+-+-+ +---+ +-v-+ +---+
| A | | D +--> E |
+-+-+ +-^-+ +---+
| +---+ |
+----> C +----+
+---+
Becomes: +---+ +---+ +---+
+---> B +--> D +--> E |
+-+-+ +---+ +---+ +---+
| A |
+-+-+ +---+ +---+
+---> C +--> D |
+---+ +---+
Or, as hierarchy IDs A /1
B /1/1
D /1/1/1
E /1/1/1/1
C /1/2
D /1/2/1
Now, you do still need a recursive CTE - but it's guaranteed to terminate. The "interior" of the CTE would be a range operation over the hierarchy IDs. Basically other.hierarchy >= origin.hierarchy && other.hierarchy < next-sibling(origin.hierarchy) (for descendant-of queries). You need to recurse to handle situations involves nodes like D in the example above (if we started at C, we'd get D in the first iteration, but would need to recurse to the first D node in order to find E).This was two orders of magnitude faster than a recursive CTE using strings to prevent infinite recursion.
The major disadvantage of this representation is that you can't modify it. It has to be calculated from scratch each time a node in the graph is changed. The dataset I was dealing with was 100,000s of nodes for each customer, took under a second, so that wasn't a problem. You could probably also identify changes that don't require a full rebuild (probably any change that doesn't involve a back edge or cross edge), but I never had to bother so didn't solve it.