MySQL
SQL

MySQL 9.0 and the New EXPLAIN Features

intro

In this blog, we discuss everything you need to know about MySQL 9.0 and its new EXPLAIN features that are under the hood. Dig into the EXPLAIN MySQL query type!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

MySQL 9.0 is a completely new version of our favorite database management system and in this blog, we walk you through the new capabilities of EXPLAIN mysql query type in the DBMS.

Preface

Many users of MySQL know what the EXPLAIN clause is — them knowing what it actually does is a different topic though. Though, at its core, EXPLAIN isn’t very hard to comprehend.

The EXPLAIN MySQL query type digs into how your queries work under the hood with an aim to help you understand their internals; it provides many details for everyone to understand how their queries work and modify them to make their queries able to achieve a goal.

EXPLAIN can be used with the following syntax:

Copy
        
1 EXPLAIN [query]

Meaning that if you want to explain a particular SELECT, you would provide the entirety of your query after the EXPLAIN query type clause like so:

Copy
        
1 EXPLAIN SELECT * FROM `demo` WHERE a = b;

The same works with all other types of queries, including INSERTs, UPDATEs, and DELETEs.

MySQL and EXPLAIN Features

To understand the capabilities of EXPLAIN, you first have to understand how the query can assist you (and your database.) The output is split into a couple of parts:

  1. id: The numeric ID of the query.
  2. select_type: The type of the SELECT query that was run if a SELECT query was executed (the output of this type can either be SIMPLE, UNION, or something else depending on what clauses were in use.)
  3. table: The table the query was executed on.
  4. possible_keys, key, key_len: Information relevant to indexes (keys.) possible_keys means possible indexes that were considered for use, key means the index (key) that was actually used by the database, and key_len depicts the length of the index in bytes.
  5. ref: What or how many columns were compared to the index (”const” means that your database has compared one column.)
  6. rows: The number of rows that were examined (accessed) by the SQL query.
  7. Extra: What kind of clauses your query is using (more information on that below.)

For those evaluating query performance, the last columns would probably bear the most significance. Arguably the most important column would be the Extra column that provides information on how your database uses and accesses data using the index. The Extra column can have multiple values:

  1. Using the index condition means that your database filtered rows using the index and has used your table to retrieve the matched rows.
  2. Using a WHERE clause is frequently interpreted dubiously, however, in many cases, it means that the expression within a WHERE clause performed an index seek operation.
  3. Using filesort means that rows were read and sorted during an extra sorting phase during query execution.
  4. Impossible WHERE noticed after reading const tables means that your query doesn't make any sense to your database (e.g., do you have 2,000 rows, but your SELECT tries to access the row #5062?)

EXPLAIN MySQL Type Query in Action

To demonstrate the capabilities of EXPLAIN, we’ll run a couple of queries:

Copy
        
1 EXPLAIN SELECT * FROM `car_data` WHERE `car_brand` = 'Volvo';
Searching for a Volvo car in MariaDB using DbVisualizer
Searching for a Volvo car in MariaDB using DbVisualizer
Copy
        
1 EXPLAIN SELECT `id`, `car_model` FROM `car_data` WHERE `year` = '1995' AND `car_model` = '911';
Searching for an ID and a car model according to a year and car model in MariaDB
Searching for an ID and a car model according to a year and car model in MariaDB

In this example, both queries are using a composite index (we can deduct that from the name of the index), but the second query seems to choose the index without even considering indexes. That’s understandable — we’re selecting a car model and provide a year and the car model. Who does that? Even DBAs would be confused, so why are we surprised that our database is too?

There are many things that can be said about the EXPLAIN MySQL query type — dig into some of them using the documentation, and once you’re done, consider exploring new features of EXPLAIN, too.

New EXPLAIN Features in MySQL 9.0

Newer versions of MySQL (we’re talking about MySQL ≥ 9.0 here) also introduced a bunch of new features to EXPLAIN too. Now, users can use the clause like so:

Copy
        
1 EXPLAIN ANALYZE FORMAT=JSON INTO @variable [options] query

The new part of the query is the EXPLAIN ANALYZE FORMAT=JSON INTO... allowing you to save the output of the EXPLAIN clause into a variable. Discover what is new in MySQL 9.0!

Such an EXPLAIN query format would be very useful in scenarios where you would find yourself in a situation where you would have to select data from a table according to some filters (clauses) and want to save the output of how it’s executed into a variable. In that case, your query could be split into 2 separate parts (see example below) and you could use it like so:

Copy
        
1 EXPLAIN ANALYZE FORMAT=JSON INTO @savedq [1] 2 SELECT * FROM `car_data` WHERE `year` = '2001' AND `model` = 'Mazda'; [2]

In other words, first, you tell your database to save the output of the EXPLAIN query in a JSON format in a variable named savedq, and then execute the query that you would want analyzed. As a result, you would obtain a JSON array like so. Run SELECT @savedq to select data stored in the variable, and you will be provided with the following:

Copy
        
1 @savedq: { 2 "query": "/* select#1 */ select `hacking_mysql`.`car_data`.`id` AS `id`,`hacking_mysql`.`car_data`.`car_brand` AS `car_brand`,`hacking_mysql`.`car_model`.`car_model` AS `car_model`,`hacking_mysql`.`car_data`.`brand` AS `brand`,`hacking_mysql`.`car_data`.`car_vin` AS `car_vin` from `hacking_mysql`.`car_data` where ((`hacking_mysql`.`car_data`.`car_model` = 'Mazda')) order by `hacking_mysql`.`car_data`.`car_model`", 3 "inputs": [ 4 { 5 "inputs": [ 6 { 7 "operation": "Table scan on car_data", 8 "table_name": "car_data", 9 "access_type": "table", 10 "actual_rows": 10050, 11 "schema_name": "hacking_mysql", 12 "actual_loops": 1, 13 "used_columns": [ 14 "id", 15 "car_brand", 16 "car_model", 17 "year", 18 "car_vin" 19 ], 20 "estimated_rows": 10050, 21 "actual_last_row_ms": 3.9765349999999997, 22 "actual_first_row_ms": 0.381938, 23 "estimated_total_cost": 410.85 24 } 25 ], 26 "condition": "((car_data.car_brand = 'Mazda') and (car_data.year = '2001') and (car_data.model = 'Mazda'))", 27 "operation": "Filter: ((car_data.year = '2001') and (car_data.model = 'Mazda'))", 28 "access_type": "filter", 29 "actual_rows": 26, 30 "actual_loops": 1, 31 "estimated_rows": 4046, 32 "filter_columns": [ 33 "hacking_mysql.car_data.car_brand", 34 "hacking_mysql.car_data.model" 35 ], 36 "actual_last_row_ms": 4.76715, 37 "actual_first_row_ms": 4.4423010000000005, 38 "estimated_total_cost": 410.85 39 } 40 ], 41 "operation": "Sort: car_data.`car_brand`", 42 "query_type": "select", 43 "access_type": "sort", 44 "actual_rows": 26, 45 "sort_fields": [ 46 "car_data.`Name`" 47 ], 48 "actual_loops": 1, 49 "estimated_rows": 4046, 50 "actual_last_row_ms": 4.868095, 51 "actual_first_row_ms": 4.863844, 52 "estimated_total_cost": 410.85 53 }

Wow — a lot of information, right? That’s the power of the EXPLAIN MySQL query type, and it’s formatted as JSON data for easy observability. The rationale behind this is that the clause can provide you with a bunch of interesting information including, but not limited to the analysis of exact query conditions, the number of scanned rows, loops that your query went through, rows examined, the query cost, time to access the first row in milliseconds, etc.

SQL Clients and DbVisualizer

All this information may be vital for DBAs or those working with bigger data sets — but given that, it may be hard to interpret through a vanilla CLI appliance. Here’s where SQL clients like DbVisualizer come in handy — by providing you with an avenue to clearly observe, build, and execute your SQL queries, they can ensure that no matter what MySQL version you’re using, your database stays performant, stable, and available at all times.

Its powerful features like editing data like a spreadsheet, the ability to export result sets in a variety of different of formats like Excel, Text, SQL files, and the like, and the SQL client itself make even the best EXPLAIN MySQL query type queries pale in comparison.

With that being said, DbVisualizer does have a feature to analyze query performance that acts very similarly to EXPLAIN: it’s called the explain plan feature and through it, users of DbVisualizer are enabled to highlight statements, run scripts or load them from files — and after that’s done, DbVisualizer will tell you how the query was executed and how you can improve its performance.

From assisting in understanding the internals of simple queries such as a basicSELECT like the following (DbVisualizer’s auto-complete capability will help you complete it if you don’t know what to select too):

Copy
        
1 SELECT username,gender,ip_address FROM demo_data WHERE country = 'Brazil' LIMIT 10;
Selecting data with DbVisualizer
Selecting data with DbVisualizer

After you’re done, feel free to export data that is presented to you similarly to what you’d do with EXPLAIN — in this case, though, there are many options you can adjust before doing so:

Exporting Data in DbVisualizer
Exporting Data in DbVisualizer

And that’s only the tip of the iceberg of available features — no matter what kind of database management system you use (DbVisualizer supports a dozen), grab a free 21-day trial of DbVisualizer and try it for yourself! And tweet us once you do, too (@DbVisualizer).

Summary

The EXPLAIN MySQL query type is certainly a reliable partner you can count on — although it won’t solve your problems related to SQL queries, it will provide you with a bunch of reliable information you can act on when understanding your query internals.

MySQL 9.0 and up include the EXPLAIN ANALYZE FORMAT=JSON INTO... clause: its purpose is to save the analyzed query into a variable using a JSON-based format. This clause isn’t for everyone as it certainly provides information that would be hard to understand without having a DBA by your side, but if you want to study the EXPLAIN MySQL query type internals on a deeper level, certainly look into that one as well.

Aside from EXPLAIN, there are many other helpful tools to lend you a helping hand when dealing with data in your database and, you’ve guessed it, DbVisualizer is one of them. Download it for free, and until next time!

FAQ

What is the MySQL EXPLAIN query type?

EXPLAIN in MySQL is a query clause that helps you understand the internals of your query. This query clause provides you a bunch of interesting information that you can use at your discretion when understanding how your queries operate, including whether your query uses any indexes and what its type is, as well as in newer versions of MySQL (MySQL ≥9.0) provides you with a clause that enables you to save query analytic details in a variable with JSON output.

Why should I use DbVisualizer?

Given that you’re reading a blog about EXPLAIN queries in MySQL, you’re already interested in improving your query performance. DbVisualizer is not only a SQL client, but it also comes with a wide variety of other features that help you understand how your queries work internally and optimize them for performance. The best thing of all is that it’s absolutely free to try for 21 days — no strings attached. Give it a whirl and experience it for yourself!

Where can I learn more about the EXPLAIN query clause?

To learn more about the EXPLAIN MySQL query type and clause, follow the documentation of MySQL, follow industry events and updates on Twitter and LinkedIn, go to industry conferences like Percona LIVE, SQLBits, or MariaDB Server Fest, and stay updated by reading blogs just like this one.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

Massive SQL DELETE Operations and Database Purging Explained

author Lukas Vileikis tags DELETE SQL 4 min 2024-11-26
title

MySQL CAST: A Guide to the Data Conversion Function

author Antonello Zanini tags MySQL 3 min 2024-11-25
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2024-11-21
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

A Beginner’s Guide to SQL & Relational Database Management Systems

author Leslie S. Gyamfi tags BEGINNER SQL 5 min 2024-11-19
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18
title

Introducing MariaDB Catalogs: What They Are and How They Work

author Lukas Vileikis tags MIGRATION MySQL SECURITY SQL 5 min 2024-11-13

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗