At this part of the Magento DB orientation series, we will look into the EAV or Entity Attribute Value (EAV) in Magento. Entity Attribute Value is a data model that describes the entities where many attributes as parameters and properties used are vast. However, the number of attributes that apply to the given entity may be relatively modest.
This is a mathematical model which is called the sparse matrix. EAV is also called object–attribute–value model and vertical database model etc. There are some special cases where the EAV schematic becomes an optimal data modeling approach in the problem domain. However, in many standard cases where the data is modeled in static relational terms, the EAV based approach is an anti-pattern that will lead to longer development periods, poor database resource use, and complex queries compared to RDBMS data schemes.
EAV in Magento
In the case of Magento, EAV represents the Entity, Attribute, and Value. Let us further explore each of these in a bit more detail.
- Entity: Entity typically represents the data items in Magento as products, customers, product categories, and orders. Each of these entities may have a unique entity record in the DB.
- Attribute: Attributes generally represent the data items that belong to a specific entity. Say, for example, a product entity may have attributes like name of the product, price, status as available or not, etc.
- Value: Values remain the simplest to understand the term as it is primarily the value linked to a specific attribute. To better understand, we may consider a product entity where each product will have a set of attributes like name, etc. Each listed product will have a specific value for the given attribute.
Entity table structure
Data representation in EAV takes space-efficient methods to store a sparse matrix while only non-empty values are stored. In the EAV database, each attribute-value pair describes a fact, and a row in the table will store a single fact. EAV tables are also often described as “long and skinny.” In this, long represents the total number of rows, and skinny denotes the columns. The data in the table is recorded in three columns as:
- The entity: This describes the item.
- Attribute or parameter: It is a foreign key into the table of attribute definitions. At the baseline, an attribute definitions table may contain the columns as
- Attribute ID
- Name of attribute
- Data type
- Columns assisting input validation etc.
Why is EAV used?
The primary use of EAV is because it is largely scalable than normalized DB structure. The developers can easily add the attributes to any entity as a product, category, customer, and order, etc., without modifying the database core structure. While custom attributes get added to the DB, no logic must be added to force Magento to save these attributes as it is already built into the model.
Downsides of EAV
One of the most important downsides is its speed of processing. When entity data is so much fragmented, creating a whole entity record will require many expensive joins on the table. So, the developers may have to implement a good cache system to cache information, which does not change too often.
Another major problem with EAV, which is denoted as its downside, is the big learning curve. Many inexperienced developers tend to give it due to this reason before they actually can identify its true simplicity. There is no quick fix for this, and hopefully, you may overcome this hesitation by reading this article. You can also get the assistance of expert database administrators like RemoteDBA to get support at this point.
Working with an EAV Model
The Achilles heel of EAV is the difficulty in working with a big volume of EAV data. It is also necessary to interconvert permanently or transiently between the columnar EAV-model representations of the same data. It can be error-prone on not being done manually and also CPU-intensive. The generic frameworks that utilize the attributes and attribute-grouping are mandated in case of any mixed schemas, which consist of a fine mixt of conventional-relational and EAV data.
No matter which approaches you to take, querying an EAV may never be as fast as querying the standard relational data, similarly by the access of elements in sparse matrices. These sparse matrices are represented using structures as linked lists and may require list traversal to access an element at the X-Y position. However, you may choose the EAV approach rightly for the problem that you are trying to solve.
Magento EAV data storage seems to be more complicated with the structure of separated data in each store. EAV attributes are divided further into groups where a group may have different attributes, and each of these attributes may be in many groups. An attribute set will also include the number of groups, and an object will hold an attribute set. Magento also optimizes data storage by offering value tables corresponding to various data types.
Exporting the EAV data needs continuous querying from various tables, so it carries the data mapping in many DB tables. To read data from any DB to object, the EAV model may follow the following steps:
- Read data from the main table or the entity table
- Define the set of object attributes
- Read out the values of attributes for objects
- Change the values of attributes
- Mapping of data in objects
Burning EAV data into databases is processed as follows:
- Get data mapped in objects
- Change the values of the attributes
- Save the data in the main table or the entity tables
- Save the data in values of attributes tables
As we discussed above, even though the processes look complicated with the EAV model, as a whole, it is quite simple. The beginners may take a bit of time to understand the concept and work for it, but it possesses many advantages if you handle it well.
Read More :
- Why Consider WordPress Caching Plugins in The Money Website?
- Importance of Online Store Web POS System