How to store data with dynamic number of attributes in a database

前端 未结 8 2292
挽巷
挽巷 2020-12-04 13:14

I have a number of different objects with a varying number of attributes. Until now I have saved the data in XML files which easily allow for an ever changing number of attr

相关标签:
8条回答
  • 2020-12-04 13:16

    I used to implement this scheme:

    t_class (id RAW(16), parent RAW(16)) -- holds class hierachy.
    t_property (class RAW(16), property VARCHAR) -- holds class members.
    t_declaration (id RAW(16), class RAW(16)) -- hold GUIDs and types of all class instances
    t_instance (id RAW(16), class RAW(16), property VARCHAR2(100), textvalue VARCHAR2(200), intvalue INT, doublevalue DOUBLE, datevalue DATE) -- holds 'common' properties
    
    t_class1 (id RAW(16), amount DOUBLE, source RAW(16), destination RAW(16)) -- holds 'fast' properties for class1.
    t_class2 (id RAW(16), comment VARCHAR2(200)) -- holds 'fast' properties for class2
    --- etc.
    

    RAW(16) is where Oracle holds GUIDs

    If you want to select all properties for an object, you issue:

    SELECT  i.*
    FROM    (
            SELECT  id 
            FROM    t_class
            START WITH
                    id = (SELECT class FROM t_declaration WHERE id = :object_id)
            CONNECT BY
                    parent = PRIOR id
            ) c
    JOIN    property p
    ON      p.class = c.id
    LEFT JOIN
            t_instance i
    ON      i.id = :object_id
            AND i.class = p.class
            AND i.property = p.property
    

    t_property hold stuff you normally don't search on (like, text descriptions etc.)

    Fast properties are in fact normal tables you have in the database, to make the queries efficient. They hold values only for the instances of a certain class or its descendants. This is to avoid extra joins.

    You don't have to use fast tables and limit all your data to these four tables.

    0 讨论(0)
  • 2020-12-04 13:16

    sounds like you need something lick couchdb, not an RDBMS.

    0 讨论(0)
  • 2020-12-04 13:22

    Let me give some concreteness to what DVK was saying.

    Assuming values are of same type the table would look like (good luck, I feel you're going to need it):

    dynamic_attribute_table
    ------------------------
    id         NUMBER
    key        VARCHAR
    value      SOMETYPE?
    

    example (cars):

    |id|    key   |   value   |
    ---------------------------
    | 1|'Make'    |'Ford'     |
    | 1|'Model'   |'Edge'     |
    | 1|'Color'   |'Blue'     |
    | 2|'Make'    |'Chevrolet'|
    | 2|'Model'   |'Malibu'   |
    | 2|'MaxSpeed'|'110mph'   |
    

    Thus,
    entity 1 = { ('Make', 'Ford'), ('Model', 'Edge'), ('Color', 'Blue') }
    and,
    entity 2 = { ('Make', 'Chevrolet'), ('Model', 'Malibu'), ('MaxSpeed', '110mph') }.

    0 讨论(0)
  • 2020-12-04 13:27

    A variation on your 2d solution is just two tables (assuming all attributes are of a single type):

    T1: |Object data columns|Object_id|

    T2: |Object id|attribute_name|attribute value| (unique index on first 2 columns)

    This is even more efficient when combined with 3rd solution, e.g. all of the common fields go into T1.

    Sstuffing >1 attribute into the same blob is no recommended - you can not filter by attributes, you can not efficiently update them

    0 讨论(0)
  • 2020-12-04 13:28

    If you ever plan on searching for specific attributes, it's a bad idea to serialize them into a single column, since you'll have to use per-row functions to get the information out - this rarely scales well.

    I would opt for your second choice. Have a list of attributes in an attribute table, the objects in their own table, and a many-to-many relationship table called object attributes.

    For example:

    objects:
        object_id    integer
        object_name  varchar(20)
        primary key  (object_id)
    attributes:
        attr_id      integer
        attr_name    varchar(20)
        primary key  (attr_id)
    object_attributes:
        object_id    integer  references (objects.object_id)
        attr_id      integer  references (attributes.attr_id)
        oa_value     varchar(20)
        primary key (object_id,attr_id)
    

    Your concern about performance is noted but, in my experience, it's always more costly to split a column than to combine multiple columns. If it turns out that there are performance problems, it's perfectly acceptable to break 3NF for performance reasons.

    In that case I would store it the same way but also have a column with the raw serialized data. Provided you use insert/update triggers to keep the columnar and combined data in sync, you won't have any problems. But you shouldn't worry about that until an actual problem surfaces.

    By using those triggers, you minimize the work required to only when the data changes. By trying to extract sub-column information, you do unnecessary work on every select.

    0 讨论(0)
  • 2020-12-04 13:29

    if you are going to edit/manipulate/delete the attributes in later point, making a true n:m (second option) will be the one which I go for. (Or try to make it 2 table where the same attribute repeats.But data size will be high)

    If you are not dealing with attributes(just capturing and showing the data) then you can go ahead and store in one field with some separator(Make sure the separator wont occur in the attribute value)

    0 讨论(0)
提交回复
热议问题