MySQL Query Cache Usage and Deprecation

By Tom Nonmacher

The MySQL Query Cache was a well-regarded tool for improving the performance of MySQL database servers. Its function was to store the result set from SELECT queries so that if the same query gets executed again, it could retrieve the result from the cache instead of executing the query again. However, starting from MySQL 8.0, the Query Cache feature has been deprecated.

The Query Cache's main limitation was that it was not designed to handle high concurrency workloads efficiently. It uses a single mutex to control access to the cache, which becomes a hotspot under high concurrency. This limitation was the primary reason behind its deprecation in MySQL 8.0. Instead of the Query Cache, MySQL 8.0 and later versions recommend using other methods to optimize query performance. These include index optimization, query rewriting, MySQL server tuning, and hardware upgrading.

In SQL Server 2019, for instance, you could use Indexed Views to cache the result of complex queries. An Indexed View physically stores the result set of a query in the database, similar to a table. When the same query is executed again, SQL Server can retrieve the result from the Indexed View instead of executing the query again. This is an example of how to create an Indexed View in SQL Server:


CREATE VIEW SalesSummary WITH SCHEMABINDING
AS SELECT SUM(OrderQty) AS TotalSales
FROM Sales.SalesOrderDetail
GROUP BY ProductID
GO

CREATE UNIQUE CLUSTERED INDEX IDX_SalesSummary
ON SalesSummary (ProductID)

In DB2 11.5, you could use Materialized Query Tables (MQTs) to improve the performance of complex queries. An MQT is similar to an Indexed View in SQL Server. It physically stores the result set of a query in the database. Here is an example of how to create an MQT in DB2:


CREATE TABLE SalesSummary AS (
SELECT ProductID, SUM(OrderQty) AS TotalSales
FROM Sales.SalesOrderDetail
GROUP BY ProductID) DATA INITIALLY DEFERRED REFRESH DEFERRED
MAINTAINED BY USER

On the cloud side, Azure SQL and Azure Synapse also offer various methods to optimize query performance. In Azure SQL, you can use the Automatic Tuning feature to let Azure SQL tune your database automatically. It can create and drop indexes based on your workload. In Azure Synapse, you can use the Materialized Views feature to improve the performance of complex queries. Materialized Views in Azure Synapse are similar to Indexed Views in SQL Server and MQTs in DB2.

In conclusion, while the deprecation of the MySQL Query Cache in MySQL 8.0 may seem like a setback, there are other efficient methods available to optimize query performance in MySQL as well as other databases like SQL Server 2019, DB2 11.5, Azure SQL, and Azure Synapse. These methods may require a bit more effort to implement than the Query Cache, but they offer superior performance under high concurrency, making them more suitable for modern, high-demand database applications.




752423
Please enter the code from the image above in the box below.