Name Value Pair - Part II

  • Comments posted to this topic are about the item Name Value Pair - Part II

  • Cool approach for dynamic columns,

    How can column names added dynamically instead of hardcoding it!

    Is it possible via a sysname data type or is it only possible via dynamic sql.

    regards Christian

  • Hi, I don't think this is a cool design. And if you read the final paragraph you'll see that this is an article against this design and not for it.

    In databases and programs column names are important and are the main link between then. How can you show the name of the customer if you have to guess the column name ? You need to assume its name, so you should "hardcore" the column name or your reports and application will be completly unestable (sometimes data will be shown and other not just because a programmer confused the column name).

    Just an opinion. Thanks, Marcos.

    ---

    Need to know everything about the DB structtre ? navigate it with Nautilus (http://sourceforge.net/projects/nautilus/)

  • If you see below and the name part is not in the lookup table, it will add to it automatically. I have included the script with the article for you to look into it.

    Regards

    CREATE procedure [dbo].[InsertNameValuePair]

    @CustomerId int,

    @Name nvarchar(100),

    @value nvarchar(max)

    as

    set nocount on

    declare @pairNameId smallint, @partitionId int

    if exists (select top 1 PairNameId from dbo.PairNames where PairName like @Name)

    begin

    select @pairNameId = PairNameId

    from dbo.PairNames where pairname = @Name

    end

    else

    begin

    insert into dbo.PairNames(PairName)

    values (@Name)

    select @pairNameId = scope_identity()

    end

    set @partitionId = @customerId%4 + 1

    insert into nvp(CustomerId, partitionId, PairNameId, PairValue)

    values (@customerId, @partitionId, @pairNameId, @value)

    go

  • Hi Markos,

    As you said it is not a cool design but for the situation that was described it

    was the best approch to take.

    The final paragraph also shows that to use name value pair for the problem that was described in this article is not good.

    How can you show the name of the customer if you have to guess the column name ?

    With name value pair you need to know the name to find the pair value. I am not sure what you mean here.

  • I mean this: if the program and the reports all need to know the property name, it's the same thing as knowing the column name of the table. So I prefer the traditional design.

    Besides that, adding a column isn't something difficult, so we should not be lazy about that.

    Besides that, you can use foreign keys when a column valus needs options (example: single, married, divorced, etc.). You can't do that with property-values pairs.

    Besides that, you can be sure that a requiered value is present using a "not null" column. That's dificult to achieve with a property-value design.

    Regards, Marcos.

  • Hi markos,

    marcosc (5/19/2008)


    I mean this: if the program and the reports all need to know the property name, it's the same thing as knowing the column name of the table. So I prefer the traditional design.

    Besides that, adding a column isn't something difficult, so we should not be lazy about that.

    Besides that, you can use foreign keys when a column valus needs options (example: single, married, divorced, etc.). You can't do that with property-values pairs.

    Besides that, you can be sure that a requiered value is present using a "not null" column. That's dificult to achieve with a property-value design.

    Regards, Marcos.

    You are right in this regard. I am not a fun of name value pair either. I am just sharing my experience and how I managed to get the system up and running without holding the project. I have never used name value pair design for this type of problem. EAV doesn't lend itself for this particular problem.

  • This sort of data-driven attribute-value pairs in a relational database quickly goes bad. I've seen too many attempts at this with disastrous results. In particular, when either querying the data with filtering and/or actually returning the data to the consumer (application).

    For an interesting read on one such attempt, see the following:

    http://www.simple-talk.com/opinion/opinion-pieces/bad-carma/


    [font="Arial Narrow"](PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.[/font]

  • I love this:

    "The suggestion was right in the long run, but as I have mentioned previously, the project had gone so far it was very difficult to revert back. So, I had to come up with a solution where it is possible to go live with the already developed solution and stil improve performance."

    There's always enough time to do it over, but not enough time to do it right.

    I've learned that when business people tell me that getting to market is more important than running their business well, I'm only there to postpone the inevitable business failure. And when they tell me that they are the business people and I need to let them do what they are good at - they don't even deserve that life preserver.

  • Seems like the 'right' answer is to redesign the DB.

    Having inherited designs like this most likely the reason for the design is to provided the flexibility to add new attributes without re-coding.

    Has anyone considered using an xml based approach to replace the E-A-V model?

  • You can use EAV or XML for easy maintenance user-defined stuff you don't know is coming in the evolution of application lifecycle - by WHY USE IT FOR THE PARTS OF THE SYSTEM YOU DO KNOW ABOUT?

  • Mark

    Mark Horninger (5/19/2008)


    Seems like the 'right' answer is to redesign the DB.

    No question about it.

    Has anyone considered using an xml based approach to replace the E-A-V model?

    Not a bad idea if you have got the indexing right.

  • I've seen a mixed approach used with some effectiveness. If it's a traditional data structure, with balanced reading and writing, it's hard to mount an effective case to not use the traditional relational table structure. However, to provide some flexibility to your application, mildly customizeable for each different customer, a name-value pair section can be added to provide them a way to save custom data fields. They can then use a 3rd party tool like Crystal Reports to dig into their custom data if your standard interface doesn't do any more than list it.

    In the long run though, you should consider making these custom fields part of your app and standard DB design if several customers want it.

  • I have been in similar situations.

    Sometimes, for instance, the application is set to support more than one business unit or entity and those units may share a number of data and features, but they may also have a number of fields that are unique to them. If the number of these business units is large, it can be difficult to maintain custom fields through a DB structure for each of them.

    In one particular scenario I was in, we provided a number of fields in our DB that we kept in the core data table, which we used meta data to describe based on the business unit. This required less joins and complexity on the DB, but involved more complexity in the application logic.

    The XML suggestion seems like a good one if it is available (SQL 2000 folks out of luck I guess), of course if we are talking about lots of fields, then that could get some serious bloat.

    It would be nice if sql server allowed for indexes using specific xpath queries, or better yet, if you could store the info using a format like JSON instead. Of course, for enterprising companies, it might be worth considering the creation of some CLR functions for storing and parsing data in that, or similar format.

  • Mark Horninger (5/19/2008)


    Seems like the 'right' answer is to redesign the DB.

    Having inherited designs like this most likely the reason for the design is to provided the flexibility to add new attributes without re-coding.

    Has anyone considered using an xml based approach to replace the E-A-V model?

    Designs similar to this are common in applications that allow customizable metadata on stored objects.

    I have indeed seen the XML tried for this - all looks very neat (and the developer gets to play with nice new XML features in modern DBs...) until you actually try and query anything. If you thought the performance would be bad with the type of queries you need against this sort of design, think an order-of-magnitude worse for XML. By all means try it for your app, but profile it early and convince yourself on the performance before you waste several weeks on the approach...

Viewing 15 posts - 1 through 15 (of 20 total)

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