MySQL 9.0 is an Innovation Release, but as you may have read in my Quick Peek, I found little innovation. However, one new item caught my eye, and it could be a way to track query performance.

MySQL 9.0 EXPLAIN enhancement

Here is the syntax that will be referenced in the post.

{EXPLAIN | DESCRIBE | DESC} ANALYZE FORMAT = JSON INTO variable [schema_spec] select_statement

The new part is the EXPLAIN ANALYZE FORMAT = JSON INTO a variable. You can now save the output from the EXPLAIN ANALYZE into a JSON document. Big whoopie? No, this new option is the basis for storing data on query performance. You can then use that information to track historical performance and feed machine learning. Store the results over several runs over a period of time, and you will be able to answer The Hated Question.

The Hated Question

Anyone who works with a database hates the ‘Is this query running slower than it used to?’ type questions. Firstly, there is probably no benchmark time for how fast the query in question used to run. Secondly, there are many reasons why performance can be perceived as poor. And yes, darn it, over time, as data grows, loads on the server increase, and other factors are added in, a query can run slower.

Using EXPLAIN on a query is the most common way of examining how the server wants to run your query, using historical information on the data. Historical information can be good, but it is an estimate. Like a lousy estimate from a building contractor, a wrong estimate can lead to expensive overruns and things taking much longer to complete than initially planned. EXPLAIN ANALYZE runs the query to get the exact data on the performance of a query.

What is needed is a way to store a query and its performance parameters. Now that MySQL 9.0 introduced a way to store the output from EXPLAIN ANALYSE into a JSON document, we can take the first steps to save performance metrics. The information on query performance can be extracted from that JSON document and then stored for future comparison.

Which explain_json_format version?

We need to change a setting from the default to take advantage of this new addition to EXPLAIN. MySQL has two explain_json_format settings, 1 or 2. You have to use 2 to get the following to work. If not, you will be explicitly told to change the settings. By default, it is set to NULL. There are slight differences between setting 1 and 2 that I will skip, as what we want is in setting 2.

 

The query output with the actual statistics is now stowed away in the @exp variable. We can then use JSON functions to extract that data. For example. How many rows had to be read to supply the answer?

If the query were run through an MD5 hashing function, it could be used as an index. But that is a detail to be taken up later.

Moving data out of that variable and into the table uses regular JSON functions.

Conclusion

The above is just an idea I have kicking around. I am not sure if it is worth the time and effort to start a small proof of concept, but I would love your feedback.

Disclaimer: MySQL 9.0 is an Innovation Release that provides the public with an opportunity to see features that may or may not be included in future releases of the MySQL Community Edition. These releases are short-lived. Do not use 9.0 in production.

Percona delivers secure, tested, open source software complete with advanced features like backup, monitoring, and encryption only otherwise found in MySQL Enterprise Edition.

 

Discover Why Customers Choose Percona for MySQL

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments