Optimizing MySQL database queries: A MultiIndex fits every query

Recently, I executed a query that took more than 9 seconds to process. The query consisted of a join of two tables: a list of items and a time series for each item. The schema looked something like this: Item(ShopID, SubID, Title) ItemPrice(ShopID, SubID, Price, Timestamp) The query looked something like this: SELECT Title, (SELECT Price FROM ItemPrice p WHERE i.ShopID = p.ShopID AND i.SubID = p.SubID ORDER BY Timestamp DESC LIMIT 1) AS Price FROM Item i In plain English: list all items with their latest price.

The problem with joins?

If you have about 9,000 items and approximately 11,000 price changes, the query would slow down immediately. But why is that? On one hand, there is a nested query here. While there is a general method to convert any sub-queries into joins (paper by Thomas Neumann at BTW15, Hamburg), this method cannot handle the ORDER BY and LIMIT clauses. The execution plan of the query is therefore clear: first, the item table is scanned, and for each of its rows, the sub-query is executed. The core essence of query optimization here is that the sub-query should occur with as little time overhead as possible (ideally via a lookup index). The next problem is the unclear key situation, because here two keys form the primary index. An index would therefore have to contain both keys.

The MultiIndex

There are basically two types of indexes: the Hash index and the BTree. The Hash index is a hash table from which you can ideally pick items with a complexity of O(1) if you know their access keys. The BTree, on the other hand, is a type of sorted list tree. The BTree can do much more than the Hash index: find items with an exact key, find items whose key lies between two values, or simply read items in sorted order. If you expand an index into multiple dimensions, something interesting happens: with the Hash index, you can only query values for which you know the values of all access keys. The BTree, however, has the following features: Technically speaking, the multidimensional BTree is therefore a list sorted by the keys. The effort for a BTree query is O(log(n)).

Efficient use of the MultiIndex

Every index consumes memory in the database. Therefore, one should use indexes sparingly. However, the following guidelines help with good index construction:
  1. For a query that joins across 3 attributes, create the index for these 3 attributes
  2. The attributes compared with "=" in the join (EquiJoin) are placed at the beginning of the index
  3. The attribute by which sorting or range queries will take place goes at the end
  4. If you have an index on columns (a,b,c) and an index on columns (a,b), the (a,b) index can be omitted, as the (a,b,c) index can also perform these queries.
  5. The columns compared with "=" in queries are interchangeable in the index. This can be used to optimally utilize the 4th position.

Back to the example

To process the query in the example above efficiently, the following index is recommended: Index(ShopID, SubID, Timestamp) Both Equi-Joins ShopID and SubID are standard O(log(n)) lookups on the index. Once this range is restricted, the range consists only of a list of items defined by ShopID and SubID, sorted by timestamp. Due to the ORDER BY DESC clause, the index now knows it must read the last item from the sorted list first. The LIMIT 1 clause stops after reading the first item. Effectively, O(log(n)) time was spent in the inner loop.

Conclusion

For every query, no matter how complex, a multi-index can be found that optimizes the joins. Software from Launix will therefore run efficiently even with large amounts of data.
Categories
Tags