I have some questions about database design.
- Is there a name for this?
- Is it good practice?
- Any performance considerations?
I have a generic table structure used to store relationships.
Recently I refactored some things to use this generic structure instead of direct Fk columns, but now I'm not sure if that was really the best idea.
Original Schema:
+------------------+ +---------------------+ +----------------------+ | Book | | Note | | MetaParent | |------------------| |---------------------| |----------------------| | Id | | Id | | Id | | NoteId | | MetaParentId:(Null) | | MetaTableId | | +-------+ +----+ KeyValue | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------------+ +---------------------+ +----------------------+
New Schema
+------------------+ +---------------------+ +----------------------+ | Book | | Note | | MetaParent | |------------------| |---------------------| |----------------------| | Id | | Id | | Id | | | | MetaParentId:(Null) | | MetaTableId | | + + +----+ KeyValue | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +------------------+ +---------------------+ +----------------------+
So basically instead of having a direct Fk relationship between Book and Note we have an indirect relationship via the MetaParent table using the MetaTableId/KeyValue columns.
Currently the MetaParent table has about 500k records and things are running acceptably. But we do rebuild the indexes on it every night.
My concerns are that now the relationship between Book and Note isn't obvious. You have to know one exists and to use the MetaParent table.
Also performance, I'm not sure at what point we'd run into issues with joins against MetaTableId/KeyValue running too slow. It seems the more you add to this table the slower queries would get.