SHOW Statements - MySQL 8 Query Performance Tuning - A Systematic Method For Improving Execution Speeds
SHOW Statements - MySQL 8 Query Performance Tuning - A Systematic Method For Improving Execution Speeds
SHOW Statements - MySQL 8 Query Performance Tuning - A Systematic Method For Improving Execution Speeds
SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
PREV NEXT
⏮ ⏭
7. The Information Schema 9. The Slow Query Log
🔎
8. SHOW Statements
1
Jesper Wisborg Krogh
The SHOW statements are the good old workhorse in MySQL for database
administrators to obtain information about the schema objects and what
happens on the system. While today most of the information can be found in
the Information Schema or Performance Schema, the SHOW command is still
very popular for interactive use due to its short syntax.
This chapter starts out with an overview of how the SHOW statements match
up with the Information Schema views and Performance Schema tables. The
remainder of the chapter covers SHOW statements that do not have views or
tables in the Information Schema and Performance Schema including
obtaining engine status information with a little more in-depth view of the
InnoDB monitor output provided by the SHOW ENGINE INNODB STATUS
statement as well as getting replication and binary log information.
Relationship to the
Information Schema
For the SHOW statements returning information about the schema objects or
privileges, the same information that can be found in the Information
Schema. Table 8-1 lists the SHOW statements that get the information from
Information Schema views and which views the information can be found in.
CHARACTER
CHARACTER_SETS
SET
COLLATION COLLATIONS
COLUMNS COLUMNS
CREATE
SCHEMATA
DATABASE
CREATE
EVENTS
EVENT
CREATE
ROUTINES ROUTINE_TYPE
FUNCTION
CREATE
ROUTINES ROUTINE_TYPE
PROCEDURE
CREATE
TABLES
TABLE
CREATE
TRIGGERS
TRIGGER
DATABASES SCHEMATA
ENGINES ENGINES
EVENTS EVENTS
FUNCTION
ROUTINES ROUTINE_TYPE
STATUS
Find answers on the fly, or master something new. Subscribe today. See pricing options.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 1/6
4/15/2020 8. SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
COLUMN_PRIVILEGES
SCHEMA_PRIVILEGES
GRANTS
TABLE_PRIVILEGES
USER_PRIVILEGES
SHOW INDEXES a
INDEX STATISTICS INDEXES are syn
INDEX.
PLUGINS PLUGINS
PROCEDURE
ROUTINES ROUTINE_TYPE
STATUS
It is recommende
PROCESSLIST PROCESSLIST performance_s
instead.
Deprecated – use
PROFILE PROFILING
Schema instead.
Deprecated – use
PROFILES PROFILING
Schema instead.
TABLE
TABLES
STATUS
TABLES TABLES
TRIGGERS TRIGGERS
The information will not always be identical between the SHOW statement
and the corresponding Information Schema views. In some cases, there is
more information available using the views, and in general the views are
more flexible.
There are also several SHOW statements where the underlying data can be
found in the Performance Schema.
Relationship to the
Performance Schema
After the Performance Schema was introduced, some of the information that
was originally placed in the Information Schema has been moved to the
Performance Schema where it logically belongs. That is also reflected in the
relationship to the SHOW statements where there are now several tables as
shown in Table 8-2 that get their data from Performance Schema tables.
MASTER
log_status
STATUS
log_status
replication_applier_configuration
replication_applier_filters
replication_applier_global_filters
SLAVE
replication_applier_status
STATUS
replication_applier_status_by_coordin
replication_applier_status_by_worker
replication_connection_configuration
replication_connection_status
global_status
session_status
STATUS
events_statements_summary_global_by_e
events_statements_summary_by_thread_b
global_variables
VARIABLES
session_variables
There are a few columns in the SHOW SLAVE STATUS output that cannot be
found in the Performance Schema tables. Some of those can be found in the
slave_master_info and slave_relay_log_info tables in the mysql
schema (if master_info_repository and
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 2/6
4/15/2020 8. SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
For SHOW STATUS and SHOW VARIABLES, one difference is that the SHOW
statements returning session scope values will include the global values if
there is no session value. When querying the session_status and
session_variables, only the values belonging to the requested scope are
returned. Additionally, the SHOW STATUS statement includes the Com_%
counters, whereas when querying the Performance Schema directly, these
counters correspond to events in the
events_statements_summary_global_by_event_name and
events_statements_summary_by_thread_by_event_name tables
(depending on whether the global or session scope is queried).
There are also some SHOW statements that do not have any corresponding
tables. The first group of these that will be discussed is for the engine status.
Engine Status
The SHOW ENGINE statement can be used to get storage engine–specific
information. It is currently implemented for the InnoDB,
Performance_Schema, and NDBCluster engines. For all three engines, it is
possible to request the status, and for the InnoDB engine, it is also possible
to get mutex information.
By far, the most used engine status statement is SHOW ENGINE INNODB
STATUS which provides a comprehensive report called the InnoDB monitor
report which includes some information that cannot be obtained from other
sources. The rest of this section introduces the InnoDB monitor report.
Tip You can also make InnoDB output the monitor report to the error
log at regular intervals by enabling the innodb_status_output system
variable. When the innodb_status_output_locks option is set, the
InnoDB monitor (whether generated because of
innodb_status_output = ON or using SHOW ENGINE INNODB
STATUS) includes additional lock information.
The InnoDB monitor report starts out with the header and a note saying how
long the averages cover:
FILE I/O: Information about the I/O threads used by InnoDB including
the insert buffer thread, log thread, read threads, and write threads.
BUFFER POOL AND MEMORY: Information about the InnoDB buffer pool.
This information is better obtained from the
information_schema.INNODB_BUFFER_POOL_STATS view.
Several of the sections will be used in later chapters when their content is
used to analyze performance or lock problems.
The Encrypted column was added in MySQL 8.0.14 together with the
support for encrypted binary logs.
Find answers on the fly, or master something new. Subscribe today. See pricing options.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 3/6
4/15/2020 8. SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
In general, the file size will be larger than in the example as the autorotation
of the binary log files happens when the size exceeds max_binlog_size
(defaults to 1 GiB) after writing a transaction. Since transactions are not split
between files, if you have large transactions, the file can become somewhat
larger than max_binlog_size.
IN: The name of the binary log or relay log file to read events from.
FOR CHANNEL: For relay logs, the replication channel to read events for.
All arguments are optional. If the IN argument is not given, events from the
first log are returned. An example of using SHOW BINLOG EVENTS is shown
in Listing 8-1. If you want to try the example, you will need to replace the
binary log filename, position, and limit.
In practice, if you have access to the file system, it is in most cases better to
use the mysqlbinlog utility that is shipped with MySQL. (The SHOW
BINLOG EVENTS and SHOW RELAYLOG EVENTS statements can still be
useful in controlled testing or when replication stops and you quickly want
to inspect the event that caused the error.) The equivalent command using
the mysqlbinlog utility to the previous SHOW BINLOG EVENTS statement
is shown in Listing 8-2. That example also uses the verbose flag to show the
before and after images of the row-based event that updates the
world.city table.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 4/6
4/15/2020 8. SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
Listing 8-2 Inspecting the binary log using the mysqlbinlog utility
The -v arguments request verbose mode and can be given up to two times to
increase the amount of information included. A single -v is what generates
the comment with the pseudo query in the event starting at position 436.
The --base64-output=decode-rows argument tells mysqlbinlog not
to include a base64 encoded version of the events in row format. The --
start-position and --stop-position arguments specify the start and
stop offsets in bytes.
The most interesting event in the transaction is the one starting with the
comment # at 436 which means the event starts at offset 436 (in bytes). It
is written as a pseudo update statement with the WHERE part showing the
values before the change and the SET part the values after the update. This is
also known as the before and after images.
Note If you use encrypted binary logs, you cannot directly use
mysqlbinlog to read the files. One option is to make mysqlbinlog
connect to the server and read them which returns the logs unencrypted.
Another option if you use the keyring_file plugin to store the
encryption key is to use Python or standard Linux tools to decrypt the
file. These methods are described in
https://mysql.wisborg.dk/decrypt-binary-logs and
https://mysqlhighavailability.com/how-to-manually-
decrypt-an-encrypted-binary-log-file/.
The command to list the connected replicas is SHOW SLAVE HOSTS , for
example:
If no replicas are connected at the time the statement is executed, the result
will be empty. The Server_id and Master_id columns are the values of
the server_id system variable on the replica and source, respectively. The
Host is the hostname of the replica as specified with the report_host
option. Similarly, the Port column is the replica’s report_port value.
Finally, the Slave_UUID column is the value of @@global.server_uuid
on the replica.
The only group of SHOW statements left consists of various statements to get
information about privileges, users, open tables, warnings, and errors.
Miscellaneous Statements
There are a few SHOW statements that are useful but that do not fit into any
of the groups that have been discussed thus far. They can be used to list the
available privileges, return the CREATE USER statement for an account, list
the open tables, and list warnings or errors after executing a statement. The
statements are summarized in Table 8-3.
SHOW Description
Statement
The three most commonly used of the miscellaneous SHOW statements are
SHOW CREATE USER, SHOW GRANTS, and SHOW WARNINGS.
The SHOW CREATE USER statement can be used to retrieve the CREATE
USER statement for an account. This is useful for inspecting metadata for the
account without querying the underlying mysql.user table directly. All
users are allowed to execute the statement for the current user. For example:
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 5/6
4/15/2020 8. SHOW Statements - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
the current user, but if you have the SELECT privilege for the mysql system
database, you can also obtain the privileges assigned to other accounts. For
example, to list the privileges for the root@localhost account:
Note MySQL Shell does not support the SHOW WARNINGS statement as
it will automatically fetch warnings if the \W mode has been enabled (the
default) and otherwise not make the warnings available. The statement is
however still useful in the legacy mysql command-line client and from
some connectors such as MySQL Connector/Python.
Listing 8-3 shows an example where SHOW WARNINGS is used with the
legacy mysql command-line client to identify that the schema definition and
data do not match.
mysql> SELECT *
FROM world.city
WHERE ID = 130\G
*************************** 1. row ***************************
ID: 130
Name: Sydney
CountryCode: AUS
District: New South Wales
Population: 0
1 row in set (0.03 sec)
The example starts with the SQL mode set to the default in MySQL 8. First,
the SQL mode is changed using the sys.list_drop() function to remove
the STRICT_TRANS_TABLES mode which triggers a warning as disabling
the strict mode should be done together with other modes as they will be
merged together at a later date. Then the population of one of the cities in
the world.city table is updated, but the calculation ends up dividing with
0 which triggers two warnings. One warning is for division by 0 which is not
defined, so MySQL uses a NULL value which causes the second warning as
the Population column is a NOT NULL column. The result is that a
population of 0 is assigned to the city, which is probably not what is expected
in the application. This also illustrates why it is important to enable the strict
SQL mode as that would have made the division by zero an error and
prevented the update.
Summary
This chapter introduced the SHOW statements which date back to before the
Information Schema and Performance Schema were implemented.
Nowadays, it is often better to use the underlying data sources in the
Information Schema and Performance Schema. The mapping between the
SHOW statements and data sources was given in the first two sections.
There are also some SHOW statements that return data that cannot be
accessed through other sources. A commonly used feature is the InnoDB
monitor report from InnoDB obtained with the SHOW ENGINE INNODB
STATUS statement . The report is split into several sections, of which some
will be used when investigating performance and lock issues.
There are also some statements for replication and the binary logs that are
useful. The most commonly used statement of these is SHOW BINARY LOGS
which lists the binary logs that MySQL knows of for that instance. The
information includes the size and whether the log is encrypted. You can also
list events in the binary logs or relay logs, but in practice the mysqlbinlog
utility is usually a better option.
The final chapter about sources of information is about the slow query log.
Find answers on the fly, or master something new. Subscribe today. See pricing options.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_8_Chapter.xhtml 6/6