May 28, 2019

New top story on Hacker News: Ask HN: DB wizards of HN, how do I represent a hierarchy in RDBMS?

Ask HN: DB wizards of HN, how do I represent a hierarchy in RDBMS?
4 by m33k44 | 6 comments on Hacker News.
Say, I have following hierarchy: Animal ==+==> Dog | +==> Cat and another "class", Pet, that references Animal. Pet --1-----+--> Animal The way I can represent this in RDBMS is by creating tables for each class, Pet, Animal, Cat and Dog. And then additional tables for the relationships, i.e. PetAnimal(pet_id, animal_id) [one-to-many] AnimalDog(animal_id, dog_id) [one-to-one] AnimalCat(animal_id, cat_id) [one-to-one] This way I am able to represent the hierarchy of Animal, Dog and Cat. So, tomorrow if I want to add Rabbit, then I will just add Rabbit and AnimalRabbit tables. My only concern is that the Animal table will grow rapidly as more pets and animal types are added and will be a performance issue. What is a better way to represent hierarchical structures in RDBMS to avoid both storage space explosion and performance issues?

0 comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

Site Search