Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till November 30th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*The maximum is $4000 in credits, Offer valid till November 30th, 2024, New Customers Only, Credit will be applied after purchase and expires after six (6) months

MySQL Optimizer Trace | Whats & Hows

by | Mar 31, 2022

MySQL Optimizer Trace comes in handy in analyzing the optimizer’s actions and decisions. Want to learn more? Keep reading. 

At Bobcares, we offer solutions for every query, big and small, as a part of our MySQL Support Service.

Let’s take a look at how our Support Team is ready to help customers with MySQL Optimizer Trace.

All About MySQL Optimizer Trace

MySQL comes with a new tracing feature in the MySQL optimizer. The interface has a set of optimizer_trace_xxx system variables as well as INFORMATION_SCHEMA.OPTIMIZER_TRACE table in order to produce output understood by users as well as programs. Furthermore, it demystifies the MySQL Optimizer’s actions and decisions.

The output format is JSON and is made up of:

MySQL Optimizer Trace

For instance, let’s take a look at the following query

MariaDB> explain select * from table1 where a<100;
+------+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                 |
+------+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
|    1 | SIMPLE      | table1    | range | a             | a    | 5       | NULL | 10   | Using index condition |
+------+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+

However, if we consider only the component names, we wind up with:

MariaDB> select * from information_schema.optimizer_trace limit 1\G
*************************** 1. row ***************************
                            QUERY: select * from table1 where a<100
                            TRACE: 
{
  "steps": [
    {
      "join_preparation": { ... }
    },
    {
      "join_optimization": {
        "select_id": 1,
        "steps": [
          { "condition_processing": { ... } },
          { "table_dependencies": [ ... ] },
          { "ref_optimizer_key_uses": [ ... ] },
          { "rows_estimation": [
              {
                "range_analysis": {
                   "analyzing_range_alternatives" : { ... },
                  "chosen_range_access_summary": { ... },
                },
                "selectivity_for_indexes" : { ... },
                "selectivity_for_columns" : { ... }
              }
            ]
          },
          { "considered_execution_plans": [ ... ] },
          { "attaching_conditions_to_tables": { ... } }
         ]
      }
    },
    {
      "join_execution": { ... }
    }
  ]
}

Each SELECT command in the Trace structure comes with 2 steps, namely:

      • join_preparation
      • join_optimization

While the former displays early query rewrites, the latter is where the query optimizations take place as seen below:

      • condition_processing
      • ref_optimizer_key_uses
      • rows_estimation
      • considered_execution_plans
      • attaching_conditions_to_tables

According to our Support Techs, the above steps are for a single SELECT query only. If the SELECT query has subqueries, each of the subqueries will have these steps as well as extra steps or rewrites in order to handle the subquery construct.

We can also extract certain parts of the trace with the following functions:

      • JSON_EXTRACT: This function extracts a part of JSON document.
      • JSON_DETAILED: This function presents the trace in a user-readable way

How to enable or view the trace as a user

We can enable or view the trace as seen below:

SET SESSION OPTIMIZER_TRACE="enabled=on"; # enable tracing
  ; # like SELECT, EXPLAIN SELECT, UPDATE, DELETE...
  SELECT * FROM information_schema.OPTIMIZER_TRACE;
  [ repeat last two steps at will ]
  SET SESSION OPTIMIZER_TRACE="enabled=off"; # disable tracing

Guidelines to add trace

Here are a few guidelines to add trace:

      • Try to keep a limit on the number of distinct words like row, tmp, and so on.
      • Use simple characters like a-z,A-Z,_,#, and so on for key names.
      • Use an array when order is essential.

With this handy guide, you will be able to use Optimizer Trace to understand the optimizer’s actions and decisions better.

[Looking for a solution to another query? We are just a click away.]

Conclusion

To sum up, our skilled Support Engineers at Bobcares demonstrated how to enable and view MySQL Optimizer Trace.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF