Entity-Attribute-Value Table Design

北慕城南 提交于 2019-11-26 17:40:43

I'm going to offer a contrary opinion to most of the comments on this question. While EAV is EVIL for all of the reasons that you can find thoroughly explained many times here on SO and DBA.SE and elsewhere, there is one really common application for which most of the things that are wrong with EAV are largely irrelevant and the (few) advantages of EAV are very much germane. That application is online product catalogs.

The main problem with EAV is that it doesn't let the database do what it is really good at doing, which is helping to give proper context to different attributes of information about different entities by arranging them in a schema. Having a schema brings many, many advantages around accessing, interpreting and enforcing integrity of your data.

The fact about product catalogs is that the attributes of a product are almost entirely irrelevant to the catalog system itself. Product catalog systems do (at most) three things with product attributes.

  1. Display the product attributes in a list to end users in the form: {attribute name}: {attribute value}.

  2. Display the attributes of multiple products in a comparison grid where attributes of different products line up against each other (products are usually columns, attributes are usually rows)

  3. Drive rules for something (e.g. pricing) based on particular attribute/value combinations.

If all your system does is regurgitate information that is semantically irrelevant (to the system) then the schema for this information is basically unhelpful. In fact the schema gets in the way in an online product catalog, especially if your catalog has many diverse types of products, because you're always having to go back into the schema to tinker with it to allow for new product categories or attribute types.

Because of how it's used, even the data type of an attribute value in a product catalog is not necessarily (vitally) important. For some attributes you may want to impose contraints, like "must be a number" or "must come from this list {...}". That depends on how important attribute consistency is to your catalog and how elaborate you want your implementation to be. Looking at the product catalogs of several online retailers I'd say most are prepared to trade off simplicity for consistency.

Yes, EAV is evil, except when it isn't.

Jonathan Leffler

Yes, there is typically a large penalty in assembling the queries for an EAV model. There are bigger performance penalties for checking the self-consistency of the data, because the DBMS is not going to be able to do it for you. If something goes wrong, the DBMS cannot tell you.

With a more orthodox database design, as recommended by Oded in comments, the DBMS ensures that the data in the database is more nearly consistent. I would strongly counsel the use of a regular (non-EAV) design.

Yaroslav

I do not know if this should be a comment or answer. Nevertheless here I go.

I do not know exactly what are you building. But have you taken a look into Magento EAV database structure? Yes, it can be slow, queries can be huge but for us the pluses are more than the minus. And on the other hand magento takes care of the queries.

We are in the middle of a migration of our online store (medium-big size store) to use Magento and for now we are very happy with the EAV approach.

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