Think of a platform-wide commenting-system. A comment can be added to a users profile, a website section, another comment or X other things.
That means that the comment has an "addressee_id" and the addressee_id can reference on different tables.
- Comments (id, adresseee_id, ..., ...)
- Sections (id, ...)
- Users (id, ...)
What is the best way to define on which table to join?
Three possible ways:
Denormalization I remove other tables and add X columns to the "comments"-table Problem: a lot of empty fields.
Another table "pages" with page_ids. Each table gets an extra column "page_id" with FK on pages.id. But here i have to make X JOINS for each request. Sounds expensive.
Comments-table gets an extra column "addresse_type" Here i have to implement IF/ELSE logics into the query which is also expensive and not easy to maintain.
What do you suggest?
Thanks Phil