Beruflich Dokumente
Kultur Dokumente
The sys Schema - 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
⏮ ⏭
5. The Performance Schema 7. The Information Schema
🔎
The sys schema is the brainchild of Mark Leith, who has also for long been
part of the team that develops MySQL Enterprise Monitor. He started the
ps_helper project to experiment with monitoring ideas and to showcase
what the Performance Schema was able to do while making it simpler at the
same time. The project was later renamed to the sys schema and moved into
MySQL. There have since been contributions from several other people,
including the author of this book.
The sys schema is available for MySQL Server 5.6 and later. In MySQL 5.7 it
became part of the standard installation, so you do not need to do anything
to install the sys schema or upgrade it. As of MySQL 8.0.18, the sys schema
source code is part of the MySQL Server source.
The sys schema is used throughout the book for analyzing queries, locks,
and more. This chapter will give the high-level overview of the sys schema
including how to configure it, formatting functions, how the views work, and
various helper routines.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_6_Chapter.xhtml 1/5
4/15/2020 6. The sys Schema - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
Alternatively, you can change the setting just for the session. This is done by
taking the name of the configuration variable and prepending sys. and
turning it into a user variable. Listing 6-2 shows examples both of using the
sys_config table and a user variable to change the configuration of
statement_truncate_len. The result is tested with the
format_statement() function which is what the sys schema uses to
truncate statements.
First, a query is set in the @query user variable. This is purely for
convenience, so it is easy to keep referencing the same query. The
sys_get_config() function is used to get the current configuration value
for the statement_truncate_len option. This takes into account whether
the @sys.statement_trauncate_len user variable is set. The second
argument provides the value to return if the provided option does not exist.
Formatting Functions
The sys schema includes four functions to help you format the output of the
queries against the Performance Schema to make the result easier to read or
take up less space. Two of the functions have been deprecated in MySQL
8.0.16 as native Performance Schema functions have been added to replace
them.
Table 6-1 summarizes the four functions and the new native functions that
replace them for the case of format_time() and format_bytes() .
Conver
format_bytes() FORMAT_BYTES() a strin
based)
Takes
replac
tempo
format_path()
on wit
the cor
variab
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_6_Chapter.xhtml 2/5
4/15/2020 6. The sys Schema - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
Trunca
most t
charac
state
format_statement()
configu
replac
statem
(...).
Conver
format_time() FORMAT_PICO_TIME() picose
readab
Listing 6-3 shows an example of using the formatting functions, and for
format_bytes() and format_time(), the results will be compared to the
native Performance Schema functions.
These functions are not only useful on their own, they are also used by the
sys schema to implement views that return formatted data. As it is in some
cases necessary to work with the unformatted data, there exist two
implementations of most sys schema views as you will see next.
The Views
The sys schema provides a number of views that work as predefined
reports. The views mostly use the Performance Schema tables, but a few also
use the Information Schema. The views are there both to make it easy to get
information out of the Performance Schema and to serve as examples of how
to query the Performance Schema.
As the views are ready-made reports that you can use as a database
administrator or developer, they are defined with a default ordering. This
means that a typical way of using the views is to do a plain SELECT * FROM
<view name>, for example:
mysql> SELECT *
FROM
sys.schema_tables_with_full_table_scans\G
*************************** 1. row
***************************
object_schema: world
object_name: city
rows_full_scanned: 4079
latency: 269.13 ms
*************************** 2. row
***************************
object_schema: sys
object_name: sys_config
rows_full_scanned: 18
latency: 328.80 ms
2 rows in set (0.0021 sec)
The result depends on which tables have been used with a full table scan.
Notice how the latencies have been formatted like with the
FORMAT_PICO_TIME() or sys.format_time() function.
Most of the sys schema views exist in two forms with one having
statements, paths, byte values, and timings formatted and the other
returning the raw data. The formatted views are very useful if you query a
view at the console and look at the data yourself, whereas the unformatted
Find answers on the fly,views
or master
work better if something new.
you need to process the data inSubscribe
a program or want today.
to See pricing options.
https://learning.oreilly.com/library/view/mysql-8-query/9781484255841/html/484666_1_En_6_Chapter.xhtml 3/5
4/15/2020 6. The sys Schema - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
You can distinguish between the formatted and unformatted views from the
name. If a view contains formatting, there will also be an unformatted view
with the same name, but with x$ prepended to the name. For example, for
the schema_tables_with_full_table_scans view that was used in the
previous example, the unformatted view is named
x$schema_tables_with_full_table_scans:
mysql> SELECT *
FROM
sys.x$schema_tables_with_full_table_scans\G
*************************** 1. row
***************************
object_schema: world
object_name: city
rows_full_scanned: 4079
latency: 269131954854
*************************** 2. row
***************************
object_schema: sys
object_name: sys_config
rows_full_scanned: 18
latency: 328804286013
2 rows in set (0.0017 sec)
The last topic for the sys schema is the helper functions and procedures that
are provided.
Extrac
schem
name
path f
extract_schema_from_file_name Function
file-pe
InnoD
tables
file.
Extrac
table
from a
for a f
extract_table_from_file_name Function
per-ta
InnoD
tables
file.
Adds
eleme
list un
alread
exists
list_add Function list. T
for ex
usefu
need t
chang
SQL m
Remo
list_drop Function eleme
from a
Quote
identi
quote_identifier Function (e.g., t
name
backt
Retur
major
versio
the in
version_major Function you ar
query
For ex
it retu
for 8.0
Retur
minor
versio
the in
version_minor Function you ar
query
For ex
it retu
for 8.0
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_6_Chapter.xhtml 4/5
4/15/2020 6. The sys Schema - MySQL 8 Query Performance Tuning: A Systematic Method for Improving Execution Speeds
Retur
patch
releas
versio
the in
version_patch Function
you ar
query
For ex
it retu
for 8.0
Execu
query
as a st
The q
execu
using
prepa
statem
execute_prepared_stmt Procedure and th
proce
deallo
the pr
statem
after t
execu
has
comp
Retur
wheth
table
and if
table_exists Procedure wheth
a base
tempo
table,
view.
Several of these utilities are also used internally in the sys schema. The most
common use of the routines is in stored programs where you need to handle
data and queries dynamically.
Tip The sys schema functions and procedures come with built-in help
in the form of routine comments. You can obtain the help by querying the
ROUTINE_COMMENT column of the information_schema.ROUTINES
view.
Summary
This chapter has provided a brief introduction to the sys schema, so you
know what it is and how to use it when you see examples in later chapters.
The sys schema is a useful addition that provides ready-made reports and
utilities that can simplify your daily tasks and investigations. The sys
schema is a system schema in MySQL 5.7 and later, so no action is required
from your side to start using it.
First, the sys schema configuration was discussed. The global configuration
is stored in the sys.sys_config table which can be updated, if you prefer
different default values than what is provided when MySQL is installed. You
can also change the configuration option for a session by setting a user
variable with sys. prefixed to the name of the configuration option.
Then the sys schema formatting functions were covered with mention of the
cases where native Performance Schema functions have been added as
replacement for the sys schema functions. The formatting functions are also
used in several of the views to help make the data easier to read for humans.
For the views using the formatting functions, there is also a corresponding
unformatted view with x$ prefixed to the name.
Finally, several helper functions and procedures were discussed. These can
help you when you try to do work dynamically, such as executing a query
generated in a stored procedure.
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_6_Chapter.xhtml 5/5