7 ms·
I worked at a startup (now fairly popular in the US) where we had tables for each thing (users, companies, etc) and a “relationship” table that described each r
by withinboredom 4y ago
I worked at a startup (now fairly popular in the US) where we had tables for each thing (users, companies, etc) and a “relationship” table that described each relationship between things. There were no foreign keys, so making changes were pretty cheap. It was actually pretty ingenious (the two guys who came up with the schema went on to get paid to work on k8s).
It was super handy to simply query that table to debug things, since by merely looking for a user, you’d discover everything. If Mongo was more mature and scalable back then (2012ish), I wonder if we would have used it.
- hamandcheese 4y agoSo basically one table for each type of node, plus one table for all edges. Neat! How big was the relationships table? How was performance?
- withinboredom 4y agoIt was pretty massive. I’m certain that it is probably sharded by now. Database performance wasn’t our bottleneck, we had np-hard problems all over the place which was our primary performance issues. For most customers, they never really had issues, but our larger customers def did. Those problems were amazingly fun to solve.
- prox 4y agoHow do you shard a thing like this? (Apart from “with great difficulty.”) :)
- hamandcheese 4y agoI was wondering the same thing. But at the same time, it seems like having it all centralized in one place would make it really easy to write tooling to aid in such an endeavor. For example, suppose it was decided that sharding would be by account ID. It would probably be very achievable to have some monitoring for all newly created relationships to check that they have an account ID specified, and break that down by relationship type (and therefor attributable to a team). Dashboards and metrics would be trivial to create and track progress, which would be essential for any large scale engineering effort.
- prox 4y agoYeah it does seem you need to think ahead of what the future needs would be.
- withinboredom 4y agoThis would have happened after I left, so I'm just speculating based on experience. 1. You create a function in your application that maps a customer id to a database server. There is only one at first. 2. You setup replication to another server. 3. You update your application's mapping function to map about half of your ids to the new replica. Use something like md5(customer id) % 2. Using md5 or something else that has a good random distribution is important (sha-x DOES NOT have a good random distribution). 4. You application now will write to the replica, turn off replication, and delete the ids from each server that no longer map to that server. Adding a new server follows approximately the same lines, except you need to rebalance your keys which can be tricky.
- prox 4y agoThanks for that answer, it seems quite a specialist job. So many moving parts. Why would you need random distribution and not just customers 1 to 100k for example?
- Keyframe 4y agoThis depends on access patterns to your data.
- withinboredom 4y ago> Why would you need random distribution and not just customers 1 to 100k for example? To keep the databases from having hotspots. For example, if your average customer churns after 6 months, you'll have your "old customer" database mostly just idling, sucking up $$ that you can't downgrade to a smaller machine because there are still customers on it. Meanwhile, your "new customer" database is on fire, swamped with requests. By using a random distribution, you spread the load out. If you get fancy with some tooling, you can migrate customers between databases, and have a pool of small databases for "dead customers" and a more powerful pool for "live customers" or even have geographical distribution and use the datacenter closest to them. But you really do need some fancy tooling to pull that off, but it is doable.
- 4y ago
- chrisjc 4y agoDoes this question imply sharding would be more difficult with this sort of table design, compared with other table designs? Obviously sharding is difficult, but what would make it any more difficult with this table design? Picking the sharding keys, avoiding hotspots, rebalancing, failover, joining disparate tables on different shards, etc... these are all complicated issues with far reaching implications, but I just don't understand how this table design would complicate sharding any more than another. I'm not trying to sound condescending and suggest it's a dumb question... genuinely interested.
- lisper 4y ago> There were no foreign keys Then how did the relationship table refer to things in the thing tables?
- chrchr 4y agoI bet they meant no foreign key constraints.
- deleted 4y ago[deleted]
- lisper 4y agoOK, but that still doesn't make any sense because: > we had tables for each thing I take that to mean "a separate table for each class of thing." So the single "relationship" table had to somehow refer not just to rows in tables but to the tables themselves. AFAIK that is not possible to do directly in SQL. You would have to query the relationship table, extract a table name from the result, and then make a separate query to get the relevant row from that table. And you would have to make that separate query for every relationship. That would be horribly inefficient.
- criddell 4y agoI don't see why you would have to include the table name. I presume the calling code knows what they are looking for. For example, say we have a table of people and a table of books and a relationship table books-owned-by. If I want to see what's in your library I can select you in the relationship table and join with the books table to get a record set describing your library. Or is this not what the OP was describing?
- lisper 4y agoThe OP said, "we had tables for each thing (users, companies, etc) and a “relationship” table that described each relationship between things". I took that to mean that there was only one relationship table.
- bongobingo1 4y agoCant help but read this and wonder if it was an strict architectural decision (I think this is what you imply?), or a "move fast and fix it later" side effect that hung around. To me it seems like your discarding a lot of safety but perhaps that's overstated in my head.
- withinboredom 4y agoThe relationships were enforced in-code, so the safety existed there instead of by the database. Even where I work now with sharding and such, we don’t have any foreign key constraints yet serve billions of requests. Code can provide just as much safety as the database, you just have to be smart enough to not try and override it.
- holoduke 4y agoCan you tell me what the performance impact is with code logic and extra dB calls versus built-in constraint logic? Because I am fairly convinced that most modern DBs have almost zero impact from relationship constraints. Those are heavily optimised. I am curious to your study and want to know more.
- withinboredom 4y agoThe problem with sharding is that you can't join, as most databases don't support "distributed joins" so relationship constraints don't even make sense. Thus it has to be done in code and often requires multiple queries to various servers to get the full picture. Most of this can be optimized by denormalizing as much as possible for common queries and leaving normalized records for less common queries. There's a performance impact, for sure, but there's really not much that can be done without switching to some database technology that does sharding for you and can handle distributed joins (rethinkdb comes to mind, but I'm not sure if that is maintained any more). But then you have to rewrite all your queries and logic to handle this new technology and that probably isn't worth it. Even if you were to use something like cockroachdb and postgres, cockroachdb doesn't support some features of postgres (like some triggers and other things), so you'll probably still have to rewrite some things.
- dehrmann 4y ago> tables for each thing (users, companies, etc) and a “relationship” table that described each relationship between things Sounds like objects and associations in the Tao model: https://engineering.fb.com/2013/06/25/core-data/tao-the-power-of-the-graph/ https://engineering.fb.com/2013/06/25/core-data/tao-the-powe...
- CGamesPlay 4y agoBut why? Ok, no foreign key checking, so it’s fast, great. Why not include the foreign ID in the local table? What’s the purpose behind a global “relationships” table?
- fatherzine 4y agoGuessing: * Most relations are properly modeled as N:N anyways. * Corollary: no "aargh, need to upgrade this relation to N:N" regrets. * Corollary: no 1000 relationship tables to name & chase around the DB metadata. * Low friction to add relations on-the-fly when developing code. * Low friction to query all relations associated with an object. * Low friction to update a relation, but keep the ex around. Neat approach IMHO.
- zepolen 4y ago* low friction till things go wrong and you're not even aware they went wrong because the relational mistake wasn't constrained * aaargh we have trillion row tables that contain everything, how do we scale this (you don't) * aaargh our tables indices don't fit into memory, what do we do (you cry) * no at a glance metrics like table sizes to have an idea of which relations are growing * oh no we have to have complex app code to re-implement for relationships everything a database gave for free Reddit used a similar system in Posgresql and hit all of these issues before moving to Cassandra. Even then they still had to deal with constraints of the original model. Edit: Didn't even read the article being about Reddit, it pretty much confirms what I said. It took them something like 2 years iirc to migrate to Cassandra and the site was wholely unuseable during that time. There is no doubt in my mind if another Reddit style site existed during that time it would have eaten their lunch same as Reddit ate Diggs lunch (for non technical reasons). Furthermore, back then iirc they only really had something like 4 tables/relations: comment, post, user, vote. The 'faster development time' adding new relations was completely moot, they spent more time developing and dealing with a complex db query wrapper and performance issues (caching in memcache all over the place) than actual features. The (app+query) code and performance for their nested comments was absolutely hilarious. Something a well designed table would have given then for free in one recursive CTE. It wasn't until they moved to Cassandra that they were able to let the db do that job and work on adding the shit ton of features that exist today.
- OJFord 4y agoSounds like a 'star schema', just without foreign key constraints on the relationship ('fact') table. I'm not really sure I understand why not having that constraint is helpful - what sort of 'changes' are significantly cheaper? Schema changes on the thing ('dimension') tables? Or do you mean cheaper in the 'reasoning about it' sense, you can delete a thing without having to worry about whether it should be nullified, deleted, or what in the relationship table? (And sure something starts to 404 or whatever, but in this application that's fine.)
- withinboredom 4y ago> you can delete a thing without having to worry about whether it should be nullified, deleted, or what in the relationship table? Pretty much this. You could just delete all the relationships, or just some of them. For example, a user could be related to many things (appointments, multiple vendors, customers, etc). Thus a customer can just delete the relationship between them and a user, but the user still keeps their other relationships with the customer (such as appointments, allowing them to see their appointment history, even though the customer no longer has access to the user's information).
- Ecio78 4y agowouldn't you be able to do the same even with FK in place? what FK would prevent you from doing would be deleting a user before all his relationships are removed. That should prevent you to ending up with inconsistent data inside the tables.
- hamandcheese 4y agoIn my experience with regular Postgres databases shared across many teams, inconsistent/broken references is seen as a much less risky thing to do than, say, cascading a delete. Usually a broken reference is of no consequence. And you don’t use FKs, people tend to bake that into how they code things without much trouble.
- 4y ago
- agumonkey 4y agoHow far is that from rdf triplets and similar ideas ? Just curious
- withinboredom 4y agoI had never heard of this before! But yeah, you could use the relationship table to describe it as a RDF triplet. For example, relationships could be OWNER, or PATIENT, or something and could be seen as a predicate. So USER OWNER of COMPANY, or USER PATIENT of COMPANY, or RECORD BELONGS to COMPANY, or COMPANY VENDOR of COMPANY.
- asddubs 4y agowhat's the advantage (besides debugging) to have all relationships in one table, rather than having a separate table for each relationship? seems like it would be strictly worse
- Sakos 4y agoI'm a bit confused about how that relationship table would look. Is anybody kind enough to show a concrete example?
- withinboredom 4y agoIIRC, we had a nullable column for each ID type. So a column for user, company, appointment, etc. then we had an enumerated column for the relation (owner, patient, vendor, etc).
- deleted 4y ago[deleted]
- apocalyptic0n3 4y agoThis type of setup is semi-common and I've seen it called "polymorphic relationships". The PHP framework Laravel has first-party support for them if you want a real example, although those are generally only polymorphic for one side of the relationship (i.e. building a CRM and having a single "comments" table with the ability to relate Customers, Opportunity, Leads, etc. to each comment without having a unique column for each type). Generally, the relationship table will look like this: - relation1_id - relation1_type - relation2_id - relation2_type Where *_id is the database ID and *_type is some kind of string constant representing another table. Generally you put an index on "relation#_id, relation#_type", but that's obviously use case dependent
- bena 4y agoLike other have said, you can store the table types with the ids. But here's the neat part. That's often only for safety. If I want to find all users and their orders, I can join the user table to the relationship table, then join that to the order table. Something like: SELECT <COLUMNS GO HERE> FROM USER INNER JOIN RELATIONSHIP ON USER.Id IN (RELATIONSHIP.LeftId, RELATIONSHIP.RightId) INNER JOIN ORDER ON ORDER.Id IN (RELATIONSHIP.LeftId, RELATIONSHIP.RightId) So you're getting the Users. The filtering the ones that have relationships, then filtering that to the ones that also have orders. If you've ever managed an N:N relationship table, it's like that, but more generic.
- vivegi 4y agoThis is quite similar to the RDF triples model. Everything is a 'thing' including 'relationships' between things. So you just need a thing table and a relation table. The issue with this is schema management and rules gets pushed to the application layer. You also need to deal with very massive tables (in terms of # of rows) for the relationships table which leads to potential performance issues.
- replygirl 4y agolet's call them "nodes" and "edges"
- CabSauce 4y agoRight? What a groundbreaking idea! /s
- claytonjy 4y agoother downsides I saw from an implementation of this in postgres: 1.you can't have strongly typed schemas specific to a 'thing' subtype; to store it all together, you end up sticking all the interesting bits in a JSON field 2. any queries of interest require a lot of self joins and performance craters Please, please, never implement a "graph database" or "party model" or "entity schema" in a normal RDBMS, except perhaps at miniscule scale; the flexibility is absolutely not worth it.
- NicoJuicy 4y agoThat's why you have duplicated properties on things you went to query on.
- philjohn 4y agoThat's why in RDF you should really be using a well-modelled ontology. As for performance issues, using a dedicated Quad store helps there, rather than one built on top of an RDBMS (OpenLink Virtuoso excluded, because somehow they manage to make Quads on top of a distributed SQL engine blazing fast). It's been about 6 years since I've been in that world, so things might have changed wrt performance.
- ideamotor 4y agoFunny. I’ve done exactly this elsewhere.
- giancarlostoro 4y agoOne neat thing about Mongo is the ObjectID's: We didn't know it at the time, but you can get the date of creation from them. Which means... If you sort by ObjectID, you're actually sorting by creation time. You can create a generic ObjectID using JavaScript and filter by time ranges. I still have a codepen proof of concept I did for doing ranges. Anyway, the other thing is, and I'm not sure how many people use it this way: you can copy an ObjectID verbatim into other documents, like a foreign key, so you can reference between collections. If you do this, you'll save yourself a lot of headaches. Just because you can shove anything and everything into MongoDB documents, doesn't mean you absolutely should.
- anm89 4y agoI dont understand this. Wouldn't the relationships rows need to use foreign keys to know which things they apply to?
- benibela 4y agoSounds like the classic entity–relationship model I am assisting a professor with his intro to databases class this semester and that was the first thing taught there https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#/media/File:ER_Diagram_MMORPG.png https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_mo...