If EAV is evil, what to use for dynamic values?

橙三吉。 提交于 2019-11-30 03:06:41

问题


I need to create database where Accountgroup table will have dynamic fields so that Accounts can enter those dynamic field values when needed. It's probably not important but I'm using C# with EF and Linq.

It's hard for me because I never done anything like that and since I did my research everyone is saying that EAV systems are horrible and you should design it differently, the problem is that nobody tells afterwards - how?

So maybe you can help me out and tell how can I implement something similar without doing EAV?

This is what I have so far.


回答1:


The problem with rules of thumb is that they quickly go from "It is usually a bad idea to do X" to "Never do X".

EAV is generally a bad idea because in many ways it defeats the purpose of a relational schema and thereby it takes away many of the features and advantages of a relational DBMS, and other technologies built on RDBMS, such as ORMs like Entity Framework.

However, there are certain design problems for which RDBMS isn't a great fit. There are some that are such a bad fit that a whole new technology had to be invented (e.g. NoSQL DB like MongoDB).

There are times when EAV is probably the best choice left to you out of a set of imperfect options. If you don't (can't) know what your schema is before hand, then EAV may be your best choice. This is especially true if your schema turns out to be unimportant. Consider for example an online product catalog where you have a huge list of products, each of which has some number of features. You can't predict in advance which products will have which features. And in the end, the only thing you do with product features is dump them out in a "feature: value" list anyway. This is a situation where schema isn't especially powerful, so defeating it with EAV isn't especially damaging.

The most important thing is to understand what your design choices are going to do to your capabilities and operations. All design is trade-off. The point is to make your trade-offs consciously. Instead of "EAV is Evil", think instead: "EAV is a loaded gun, make sure you know whose foot you're pointing it at."




回答2:


Well, at the simplest level, just add the values as columns; perhaps using the sparse column support at the database so that it doesn't have much size impact. This avoids both EAV and the inner-platform effect, and means you are storing the values as regular, typed values.




回答3:


EAV is not "evil" - it just sometimes gets misused when other solutions might be more appropriate.

If your attributes are truly dynamic and you want to avoid dynamically adding columns1, then EAV is appropriate.


1 E.g. to avoid locking the table or because your ORM of choice doesn't play well with it or because there is simply too many of them.




回答4:


When you start using EAV fields for reports or BI, you will want to shoot yourself in the face.

  1. Use an XML field. Most databases have good XML support, XPath queries, indexing.

  2. Create a new schema for a tenant's user-designed fields. Let the user do what they want with that schema (within reason).

For example

create table account_group (
  id primary key references real_schema.accountgroup(id),
  super_important_note_field text not null
);

Yes, you can do foreign keys across schemas.



来源:https://stackoverflow.com/questions/17231834/if-eav-is-evil-what-to-use-for-dynamic-values

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