First, this is not a question about how to implement inheritance in relational databases.
The Problem:
I have a system with several sub-systems that may have several others sub-systems and so on.
I need to create a database that will store configuration parameters for each system. Child systems will inherit settings from parents when their own settings where not provided. The inheritance is based in each parameter and not the full set of it.
Basically…
When Stored:
- System A: background: white, stack-size: 100, threads: 50
- System A-1: background: blue, stack-size: null, threads: null
- System A-1-1: background: null, stack-size: 10, threads: null
- System A-2: background: null, stack-size: null, threads: 150
- System A-1: background: blue, stack-size: null, threads: null
When loaded:
- System A: background: white, stack-size: 100, threads: 50
- System A-1: background: blue, stack-size: 100, threads: 50
- System A-1-1: background: blue, stack-size: 10, threads: 50
- System A-2: background: white, stack-size: 100, threads: 150
- System A-1: background: blue, stack-size: 100, threads: 50
How would you implement this, so it can also be loaded in a fast way?
I’m open for using either NoSql and Sql solutions. But my application to consume this will be written in C#.
Thanks in advance.
I have implemented something similar to this before (in MS Sql Server) using a self-referencing configuration table. Each entry would specify system, parent system, and setting name/value. This allows for flexibility around what settings are saved (rather than having a column for background, column for threads, etc). You can calculate the settings for a given system using a recursive CTE, as long as the number of levels doesn’t exceed the maximum recursion depth. As a place to start:
And to retrieve the configuration for a system:
Demo SQL Fiddle
I can’t say whether this is the most efficient way in SQL, and I’ve never attempted a NoSql solution, but this gets the job done and with reasonable indexing performs well.