简体   繁体   English

使用NoSql功能装饰现有的关系SQL数据库

[英]Decorating an existing relational SQL database with NoSql features

We have a relational database (MySql) with a table that stores "Whatever". 我们有一个关系数据库(MySql),其中包含一个存储“Whatever”的表。 This table has many fields that store properties of different (logical and data-) types. 该表有许多字段,用于存储不同(逻辑和数据)类型的属性。 The request is that another 150 new, unrelated properties are to be added. 请求是要添加另外150个新的无关属性。

We certainly do not want to add 150 new columns. 我们当然不想添加150个新列。 I see two other options: 我看到另外两个选择:

  1. Add a simple key-value table (ID, FK_Whatever, Key, Value and maybe Type) where *FK_Whatever* references the Whatever ID and Key would be the name of the property. 添加一个简单的键值表 (ID,FK_Whatever,Key,Value和Maybe Type),其中* FK_Whatever *引用Whatever ID, Key将是属性的名称。 Querying with JOIN would work. 用JOIN查询会有效。
  2. Add a large text field to the Whatever table and serialize the 150 new properties into it (as Xml, maybe). 在Whatever表中添加一个大文本字段 ,并将150个新属性序列化为它(可能是Xml)。 That would, in a way, be the NoSql way of storing data. 在某种程度上,这将是NoSql存储数据的方式。 Querying those fields would mean implementing some smart full text statements. 查询这些字段意味着实现一些智能全文语句。

Type safety is lost in both cases, but we don't really need that anyway. 在这两种情况下,类型安全都会丢失,但无论如何我们并不需要这样做。

I have a feeling that there is a smarter solution to this common problem (we cannot move to a NoSql database for various reasons). 我觉得这个常见问题有一个更聪明的解决方案(由于各种原因我们无法移动到NoSql数据库)。 Does anyone have a hint? 有人有提示吗?

In an earlier project where we needed to store arbitrary extended attributes for a business object, we created an extended schema as follows: 在我们需要为业务对象存储任意扩展属性的早期项目中,我们创建了一个扩展模式,如下所示:

CREATE TABLE ext_fields
{
    systemId INT,
    fieldId  INT,
    dataType INT // represented using an enum at the application layer.

    // Other attributes.
}

CREATE TABLE request_ext
{
    systemId   INT,  // Composite Primary Key in the business object table.
    requestId  INT,  // Composite Primary Key in the business object table.
    fieldId    INT,
    boolean_value BIT,
    integer_value INT,
    double_value  REAL,
    string_value  NVARCHAR(256),
    text_value    NVARCHAR(MAX),
}

A given record will have only of the _value columns set based on the data type of the field as defined in the ext_fields table. 给定记录将仅根据ext_fields表中定义的字段的数据类型设置_value列。 This allowed us to not lose the type of the field and it's value and worked pretty well in utilizing all the filtering methods provided by the DBMS for those data types. 这使我们不会丢失字段的类型和它的价值,并且在利用DBMS为这些数据类型提供的所有过滤方法方面工作得很好。

My two cents! 我的两分钱!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM