Magento EAV and Slow Product Queries: Why Flat Tables Helped
How Magento's Entity-Attribute-Value model leads to many-table JOINs on product grids, and how flat tables and search indexes helped on large catalogs.
· 3 min read · Erik Kubica
In the early 2010s, the Entity-Attribute-Value (EAV) database model was popular in enterprise software, and Magento built its catalog on it.
The appeal was real: a product catalog could support almost any schema without ALTER TABLE migrations. If an apparel merchant needed a "fabric_composition" attribute, an electronics vendor needed "battery_milliamp_hours", and a shoe store needed "euro_shoe_size", they could click "Add New Attribute" in the Magento admin panel.
No database migrations and no schema locks. The cost shows up later, in how queries run.
Many-Table JOINs for a Product Listing
In a plain relational schema, a product is a row in a products table. Fetching fifty products is a simple SELECT * FROM products WHERE category_id = 42 LIMIT 50, which on a small catalog runs in around a millisecond.
In Magento's EAV architecture, the main catalog_product_entity table holds little more than an entity_id, a sku, and a timestamp.
Attribute values are spread across five data-type tables:
- catalog_product_entity_varchar (names, URLs, meta descriptions)
- catalog_product_entity_int (visibility, status, tax classes)
- catalog_product_entity_decimal (prices, weights, special pricing)
- catalog_product_entity_text (long descriptions, size guides)
- catalog_product_entity_datetime (special price date ranges)
To render a simple product listing with title, price, status, thumbnail, and manufacturer, Magento generates an SQL query like this:
SELECT e.entity_id, e.sku,
v_name.value AS name,
d_price.value AS price,
i_status.value AS status,
v_image.value AS thumbnail
FROM catalog_product_entity AS e
LEFT JOIN catalog_product_entity_varchar AS v_name
ON v_name.entity_id = e.entity_id AND v_name.attribute_id = 71 AND v_name.store_id = 0
LEFT JOIN catalog_product_entity_decimal AS d_price
ON d_price.entity_id = e.entity_id AND d_price.attribute_id = 75 AND d_price.store_id = 0
LEFT JOIN catalog_product_entity_int AS i_status
ON i_status.entity_id = e.entity_id AND i_status.attribute_id = 97 AND i_status.store_id = 0
LEFT JOIN catalog_product_entity_varchar AS v_image
ON v_image.entity_id = e.entity_id AND v_image.attribute_id = 85 AND v_image.store_id = 0
WHERE e.entity_id IN (1042, 1043, 1044, 1045);
Multiply that across twenty attributes for forty products on a category page, and on the stores I worked with MySQL ended up joining around fourteen tables per entity lookup.
Every join is also scoped by store_id to support multi-language and multi-currency storefronts. If a product has no translated name in store 2, MySQL has to fall back to store 0 (the default admin scope). With five join conditions the query planner can struggle to estimate cardinality, and in my experience that sometimes turned index lookups into temporary tables on disk.
EAV gives merchants a lot of flexibility in the admin, and part of the cost shows up in the database at read time.
What Helped: Flat Tables and External Indexes
On one store with 100,000 SKUs and 300,000 attribute rows, category page response times climbed past 850 ms per request. PHP-FPM workers used around 160 MB of RAM each, mostly hydrating large arrays from the fragmented join results.
To keep that store steady under flash sale traffic, we worked around the EAV queries:
- Flat catalog tables: Magento offers
catalog_product_flat_1, an indexer that denormalizes the EAV tables into one wide MySQL table with static columns. On that store, query latency dropped from about 850 ms to about 14 ms. - Dedicated search indexers: In Magento 2.4, flat tables were deprecated in favor of external search engines like Elasticsearch and OpenSearch, which take the heavy attribute lookups off MySQL.
What I took from that work: EAV makes sense when merchants need to change the schema often without developers. If you control the schema, a normal relational design, or PostgreSQL JSONB with typed constraints for the flexible parts, may serve you better. Your catalog size and traffic will decide how much any of this matters.