Design database based on EAV or XML for objects with variable features in SQL Server?

我的梦境 提交于 2019-12-05 07:15:28

问题


I want to make a database that can store any king of objects and for each classes of objects different features.

Giving some of the questions i asked on different forums the solution is http://en.wikipedia.org/wiki/Entity-attribute-value_model or http://en.wikipedia.org/wiki/Xml with some kind of validation before storage.

Can you please give me an alternative to the ones above or some advantages or examples that would help decide which of the two methods is the best one in my case?

Thanks

UPDATE 1 : Is your db read or write intensive? will be both -> auction engine Will you ever conceivably move off SQL Server and onto another platform? I won't move it, I will use a WCF Service to expose functionality to mobile devices. How do you plan to surface your data to the application? Entity Framework for DAL and WCF Service Layer for Bussiness Will people connect to your data through means other than those you control? No


回答1:


I will use my answer from another question:

EAV:

  1. Storage. If your value will be used often for different products, e.g. clothes where attribute "size" and values of sizes will be repeated often, your attribute/values tables will be smaller. Meanwhile, if values will be rather unique that repeatable (e.g. values for attribute "page count" for books), you will get a big enough table with values, where every value will be linked to one product.
  2. Speed. This scheme is not weakest part of project, because here data will be changed rarely. And remember that you always can denormalize database scheme to prepare DW-like solution. You can use caching if database part will be slow too.
  3. Elasticity This is the strongest part of solution. You can easily add/remove attributes and values and ever to move values from one attribute to another!

XML storage is more like NoSQL: you will abdicate database functionality and you wisely prepare your solution to:

  1. Do not lose data integrity.
  2. Do not rewrite all database functionality in application (it is senseless)



回答2:


While @marc_s is correct in his cautions, there unarguably are situations where the relational model is just not flexible enough. For quite a number of years now, I've been working with a database that is straightforwardly relational for the largest part, but has a small EAV part. This is because users can invent new properties any time for observation purposes in trials.

Admittedly, it is awkward wrt querying and reporting, to name a few, but no other strategy would suffice here. We use stored procedures with T-Sql's pivot to offer flattened data structures for reporting and grids with dynamic columns for display. Once the infrastructure stands it's pretty comfortable altogether.

We never considered using XML data because it wasn't there yet and, apart from its common limitations, it has some drawbacks in our context:

  1. The EAV data is queried heavily. A development team needs more than standard sql knowledge because of the special syntax. Indexing is possible but "there is a cost associated with maintaining the index during data modification" (as per MSDN).
  2. The XML datatype is far less accessible than regular tables and fields when it comes to data processing and reporting.
  3. Hardly ever do users fetch all attribute values of an entity, but the whole XML would have to be crunched anyway.

And, not unimportant: XML datatype is not (yet) supported by Entity Framework.

So, to conclude, I would go for a design that is relational as much as possible but EAV where necessary. Auction items could have a number of fixed fields and EAV's for the flexible data.




回答3:


I think there is way too much context missing for anyone to add any kind of valid comment to the discussion.

  • Is your db read or write intensive?
  • Will you ever conceivably move off SQL Server and onto another platform?
  • How do you plan to surface your data to the application?
  • Will people connect to your data through means other than those you control?



回答4:


First do not go either route unless the structure truly cannot be known in advance. Using EAV or XML because you don't want to actually define the requirements will result in an unmaintainable mess and a badly performing mess at that. Usually at least 90+% (a conservative estimate based on my own experience) of the fields can be known in advance and should be in ordinary relational tables. Only use special techiniques for structures that can't be known in advance. I can't stress this strongly enough. EAV tables look simple but are actually very hard to query especially for complex reporting queries. Sure it is easy to get data into them, but very very difficult to get the data back out.

If you truly need to go the EAV route, consider using a nosql database for that part of the application and a relational database for the rest. Nosql databases simply handle EAV better.



来源:https://stackoverflow.com/questions/7956484/design-database-based-on-eav-or-xml-for-objects-with-variable-features-in-sql-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!