Database engineering is hard.This also seems like a good use case for Neo4j and Cypher
https://qntm.org/gay