2

首先,这不是关于如何在关系数据库中实现继承的问题。

问题:

我的系统有几个子系统,可能还有其他几个子系统,依此类推。我需要创建一个数据库来存储每个系统的配置参数。如果未提供子系统自己的设置,子系统将从父系统继承设置。继承是基于每个参数而不是它的完整集。

基本上...

存储时:

  • 系统 A:背景:白色,堆栈大小:100,线程:50
    • 系统 A-1:背景:蓝色,堆栈大小:null,线程:null
      • 系统 A-1-1:背景:null,堆栈大小:10,线程:null
    • 系统 A-2:背景:null,堆栈大小:null,线程:150

加载时:

  • 系统 A:背景:白色,堆栈大小:100,线程:50
    • 系统 A-1:背景:蓝色,堆栈大小:100,线程:50
      • 系统 A-1-1:背景:蓝色,堆栈大小:10,线程:50
    • 系统 A-2:背景:白色,堆栈大小:100,线程:150

你将如何实现这一点,所以它也可以快速加载?

我愿意使用 NoSql 和 Sql 解决方案。但是我使用它的应用程序将用 C# 编写。

提前致谢。

4

1 回答 1

1

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:

CREATE TABLE Configuration (
    SystemId varchar(30), 
    ParentSystemId varchar(30), 
    Name varchar(30), 
    Value varchar(30)
)

And to retrieve the configuration for a system:

;WITH SystemConfig (Id, ParentId, Name, Value, Rank) AS (
    SELECT SystemId, ParentSystemId, Name, Value, 1
    FROM Configuration C
    WHERE SystemId = @SystemId

    UNION ALL

    SELECT D.SystemId, D.ParentSystemId, D.Name, D.Value, Rank + 1
    FROM Configuration D
    INNER JOIN SystemConfig S ON S.ParentId = D.SystemId
)

SELECT DISTINCT K.Name, V.Value 
FROM SystemConfig K
CROSS APPLY (
    SELECT TOP 1 Value
    FROM SystemConfig G
    WHERE K.Name = G.Name
    ORDER BY Rank
) V

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.

于 2012-07-26T17:14:44.263 回答