How MySQL optimizer works?

How does MySQL optimizer work?

The MySQL optimizer is responsible for determining the most efficient way to execute a SQL statement. It evaluates different query execution plans and chooses the one that it estimates will have the lowest cost, where cost is defined as the amount of resources (e.g. CPU, I/O) required to execute the query.

The optimizer uses a variety of techniques to determine the optimal execution plan, including:

  • Index usage: The optimizer will attempt to use indexes to limit the amount of data that needs to be scanned. If a query references a column that has an index, the optimizer will use that index to quickly locate the relevant rows.
  • Join order: For queries with multiple tables, the optimizer will determine the most efficient order in which to join the tables. This can include both the order of the tables in the query and the type of join to use (e.g. nested loop join, hash join).
  • Access method: The optimizer will determine the most efficient method to access the data (e.g. table scan, index scan).

The optimizer also uses statistics about the data distribution, number of rows, and other information stored in the system catalog (information_schema) to estimate the cost of different query execution plans.

In some cases, the optimizer may not be able to determine the optimal execution plan, for example, if the statistics are not up-to-date, or if the query is too complex. In these cases, the optimizer may fall back to a suboptimal execution plan. To overcome this, you can use query hints or indexes on specific columns to guide the optimizer to use a specific execution plan.

Please note that the specific optimization techniques used by MySQL can be influenced by the version, and the configuration you are using. MySQL also allows to control the optimizer behavior by using different storage engines with different optimization techniques.

Execution plan of queries in MySQL optimizer

In MySQL, the optimizer generates an execution plan for a query to determine the most efficient way to execute it. This execution plan is a set of steps that the server will follow to retrieve the requested data. The execution plan includes information about which indexes to use, how to join tables, and the order in which to retrieve the data. The execution plan is generated based on the query, the available indexes, and the statistics about the data stored in the database.

There are several types of execution plans that the optimizer can generate, including:

  1. Index Scan: The optimizer chooses to use an index to scan through the table and retrieve the requested data.
  2. Table Scan: The optimizer chooses to scan through the entire table to retrieve the requested data.
  3. Nested Loop Join: The optimizer chooses to join two tables by scanning through one table and then looking up matching rows in the other table.
  4. Sort-Merge Join: The optimizer chooses to sort both tables and then merge them together to join the two tables.
  5. Hash Join: The optimizer chooses to create a hash table from one of the tables and then look up matching rows in the other table.

The optimizer will generate an execution plan based on the query, the available indexes, and the statistics about the data stored in the database. The optimizer will try to find the most efficient plan by taking into account the different algorithms available and the complexity of the query.

Python code for generating execution plan from MySQL using mysql connector

You can use the MySQL Connector/Python library to generate an execution plan for a query in MySQL. Here is an example of how you can do this:

import mysql.connector

# Connect to the MySQL server
cnx = mysql.connector.connect(user='<username>',

# Create a cursor object
cursor = cnx.cursor()

# Execute the EXPLAIN command
query = "EXPLAIN SELECT * FROM <table> WHERE <condition>"

# Fetch the execution plan
plan = cursor.fetchall()

# Print the execution plan
for row in plan:

# Close the cursor and the connection

This code creates a connection to the MySQL server using the mysql.connector library, creates a cursor object, and then executes the query preceded by the ‘EXPLAIN’ keyword. The execution plan is then fetched and printed. This code is just an example and you should change the credentials and the query to match your specific needs.

You can also use other libraries like PyMySQL which are python implementation of the MySQL protocol.

It’s worth noting that the execution plan generated by the EXPLAIN command may not be the same as the one actually used by the query optimizer, as the optimizer may make adjustments at runtime based on the actual data and statistics. The EXPLAIN command is mainly used for troubleshooting and understanding how a query will be executed.

About Shiv Iyer 72 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.

Be the first to comment

Leave a Reply