← Back to Blog
· Ivan Chepurnyi · 4 min read

Monster Database Queries

Dissecting Magento's BaseFinalPrice indexer query. Why complex SQL queries degrade at scale, and how breaking price and tier-price calculation into focused steps speeds up indexing.

Magento 2MySQLPerformance

The problem with trying to optimize the query desperately is that you narrow your attention and focus on it and do not question the reason for the query in the first place. The database is not some magic software that returns you any data at will. It has to do work on its own to return you a result. Every single table in the database is a file on your filesystem, and it requires time to assemble a final result for you.

Let's examine this fantastic smart query for retrieving product price for an indexer in Magento.

As seen, it does a couple of things:

  • Returns a denormalized list of product price record for each website and customer group
  • Tries to calculate the minimal and maximum prices based on regular price, special price and group prices (from tier price)
  • Filters out dataset to exclude disabled products
  • Joins a bunch of data for backward compatibility

But it is also missing some of the essential parts of pricing, like tier prices itself. It just ignores the minimum tier price altogether. 

So why does this happen? The answer is simple - try to execute this query on a decent data set. And you'll notice that it gets slower the broader range of products you are trying to retrieve. This query takes at least 200 ms to return a base price for 500 simple products in 70k SKU dataset with a couple of websites and tier prices set for products. If we add aggregation by tier price, it is going to be even slower. 

However, indexer could have been much faster and even could include aggregated tear price calculation. Decomposing query responsibility into smaller units and not trying to put everything into this monster can save the day. 

Let's start with thinking about our calculation scope. Which products should have a price index entry? That's simple, the ones that are assigned and enabled within a website. It is the first temporary table we have to create for our next queries with proper indexes on fields we are going to join later. Ideally, this just should be an index on its own, as all indexers depend on this scope definition.

What differentiates a price within a website? A customer group. So it means we need to multiply our list of products for the site by customer groups. But do we need a customer group for regular price and special price? No, because it is the same within the scope of the website. So we can create a temporary table with a reduced range of special and regular prices. We are definitely can re-use generated data for future calculations. 

So before we start to calculate a final price index, we have the last piece of the puzzle, tier prices. There are plenty of variations of how the tier price is calculated. It's scope is confusing, as you can define it for: all websites and all customers; all customers on a specific website; a specific group of customers on all websites; and finally a particular group of customers in a particular website. Then the price itself can be defined as a fixed value or a percentage of the minimum current base price (from special or regular price). 

But it also possible to solve by decomposing logic into multiple queries that populate the same temporary table with proper override sequence:

  • We can start with the most specific cases when the price is defined for the customer group and website
  • Then populate values for all customer groups and particular websites, or all websites but specific customer groups
  • And then fill the table with values that should be applied to all customers and all websites

All this can be done with INSERT IGNORE so if some tier prices from more broad scope try to override more specific ones, the specific one is preserved.

And finally, we can generate our final select by just joining three tables: final and special price, calculated tier price and customer groups via cross join. This operation will be trivial for the database to perform. 

All of the above operations combined take far less time than the original query and can be applied to bigger ranges of products, as each operation is very specific and deals only with the related scope of data. Then the last query just combines pre-calculated values for each scope. This approach also makes it much easier to reason about the indexation logic and that is a win-win for everyone.

Let's stop building those monster database queries, they are slow, fragile and hard to maintain.

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