← Back to Blog
· Ivan Chepurnyi · 4 min read

Database, JOINs, and EAV

Why giant multi-JOIN SQL queries slow down Magento EAV lookups, and how Join Decomposition and focused indexers deliver faster, cleaner data retrieval.

Magento 2MySQLEAVPerformance

Recently I've started a short series of #MagentoPerformanceWisdom tweets and decided to expand the topic a little bit more in detail via articles. Today we are going to dive deeper into this one.

When I first learned databases in early 2003, it was a common practice to get as much data as possible in a single query to save precious network operations. Writing such a database query with tons of joins was a sign of deep understanding of SQL and was prised by other fellow developers. I was so fascinated by it, that was doing the same as a junior.

However, when we talk about web application stack of that time, it was mostly all on running on a single node without any network latency involved, so building large queries was not making too much sense already for some time before I started. It was an era of shared hosting, where you get 16 Mb per your PHP process to do work with data and if you go over - you script is killed. Imagine parsing large XML feed from external system, import data into database and still be under this limit. Simple XML? DOMDocument? Forget about them, only the good old XML Parser could do the job. However, this is a little bit off-topic, that deserves an article on its own. Let's get back to databases.

Once I got a nice conversation mid-2005 with a senior PHP developer who was moderating the Ukrainian PHP community at that time. His forum nickname was Sych (unfortunately, I can't remember his full name).

"You know, now I run multiple small queries instead of running a complex query that returns you everything in one result." - He mentioned very briefly. I didn't understand this idea at that time. I thought it should make sense, but at the same time, it was really against my world view at that time.

However, with years, I got to the point where all this started making sense. I've studied this excellent book, and also I've done some benchmarks to see if what I was trying out was making sense. You can find some of my findings in these slides.

The basic idea described in that quote is a performance optimization practice called Join Decomposition. So let's take a look at a practical example based on everyone's "favorite" EAV.

To get all product data via a conventional approach, you have to join dozens of tables into a single query, and it results in creating one of those monster queries. It happens because in EAV each attribute value is stored in a row, and you must produce two joins because of the store view model.

Is there a better way to deal with EAV? Do you need to join all data in one query? Definitely not. You are not running your application from the database, and you still have to process and enrich the data, so why not to separate data retrieval from filtration?

The alternative approach is to run a query that will produce you a list of identifiers for your current range of entities, ordered by required fields. Then you run a set of separate queries that retrieve data by those identifiers. In such a way, you are not only helping MySQL to figure out the best strategy for executing your query, but also you split your data retrieval into isolated components. So in future, you can replace filtration, sort, and pagination by something external like ElasticSearch, Solr, Manticore, but still access the rest of the data efficiently from the database by identifiers. And of course, if you are fancy, you can create an async application that runs those queries in parallel and assembles result on PHP side, producing incredible read performance.

What about filtering and sorting identifiers? As it is still going to be slow if we filter by joining attribute from EAV tables. That's why Magento has indexers, that denormalize EAV structure into simple tables for fast lookups with indexes on the right columns. So if you want to filter or sort by some field you should consider adding it to existing indexer or creating your own.

Dealing with a similar bottleneck in production?

Reach out directly to Ivan Chepurnyi for a performance audit, indexer redesign, or second opinion.

Get in Touch