EAV or XML

  • I have a set of tables in database where I cannot change its structure. I need to create extensions to the tables by adding new attributes (columns). The list of columns and their data types is determined dynamically by the applications that use the database.

    I thought of using an EAV approach for the extensions, or create a table with a XML column to store the variable extensions and maintain a relational structure. Another possibility is a hybrid approach where I would create an EAV for the metadata only, and use the XML for storing the data.

    I would appreciate if someone could comments on the options or enlighten me with different ideas.

  • I have been spending a lot of time playing with the XML data type in SQL lately. And I have been largely UNDERwhelmed with the performance. I would POC it both ways to see how it performs.

    CEWII

  • I'm with Elliot. I've done lots of programming in and around XML on SQL Server. It's functional. But it's not terribly fast. If you're just talking, writing and reading this stuff in and out of the database as XML and then all parsing is done at the client side, yeah, that'll work. But if you're talking filtering and querying the XML, I think you'll be digging a pretty dark hole for yourself.

    EAV structures can work, although they're hard to set up. I saw a discussion on it recently somewhere (or maybe I was involved in one at an event, I don't remember) where people were able to get EAV to work nicely. But it takes a bit of effort and some careful coding. Take your time and don't do crazy shortcuts around how SQL Server stores and accesses data because those almost never work out well.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant, would it be possible to post a link to the event name or the document you saw, in case it is public domain? Thanks

  • I would if I remembered it. It might have been a conversation I had at SQL Saturday DC. I just don't recall, sorry.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • N_Muller (12/12/2013)


    I have a set of tables in database where I cannot change its structure. I need to create extensions to the tables by adding new attributes (columns). The list of columns and their data types is determined dynamically by the applications that use the database.

    I thought of using an EAV approach for the extensions, or create a table with a XML column to store the variable extensions and maintain a relational structure. Another possibility is a hybrid approach where I would create an EAV for the metadata only, and use the XML for storing the data.

    I would appreciate if someone could comments on the options or enlighten me with different ideas.

    If the columns being added are not added by the whim of an end user, consider multiple "sister tables" in a star schema. Although that can add a bit o' complexity to code, it allows real data-typing of the new columns, can be fast as the wind, and also easily allows for DRI.

    If you decide to go the EAV route and you have no "blob" data, read up on the SQL_VARIANT datatype which allows for some semblance of metadata preservation (I use it for column-level Audit tables).

    I share Elliott's and Grant's observations on XML except I'll go several steps further. I feel that it's a grossly overused, over advertised, resource intensive, difficult to maintain, pipe clogging, bloated method that should be avoided in a database at any and all costs because it's worth avoiding.

    Heh... strong letter to follow. 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks, everyone. In the end I'm using the EAV for metadata only. I created a stored procedure that runs every time a new attribute is created (trigger) to generate an alter view that returns all columns of the main table and the additional columns in the metadata EAV.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply