• Tidak ada hasil yang ditemukan

Note The script requires Perl to be installed on your system. This is not a problem on Linux and unix where Perl is always present, but on Microsoft

Windows, you will need to install Perl yourself. One option is to install Strawberry Perl from http://strawberryperl.com/.

There are a few options to control the behavior of mysqldumpslow. These are summarized in Table 9-2. Additionally, the slow query log file can be given as an argument without an option name.

Table 9-2. Command-line arguments for mysqldumpslow Option Default Value Description

-a Do not replace number and string values with N and 'S'.

--debug Execute in debug mode.

-g Perform pattern matching (using the same syntax as for grep)

on the queries and only include matching queries.

-h * By default, mysqldumpslow searches for files in the datadir set in the MySQL configuration file. This option specifies the hostnames that the files should match assuming the default slow query log filename is used. Wildcards can be used.

--help Display a help text.

-i The instance name in the mysql.server startup script to use

in the automatic algorithm to look for slow query log files.

-l Do not extract the lock time for the queries.

(continued)

ChAPTEr 9 ThE SLOW QuEry LOG

The -s, -t, and -r options are the most commonly used. While mysqldumpslow can search for the slow query log using the MySQL configuration file in the default paths and hostname, it is more common to specify the path to the slow query log file as an argument on the command line.

The -s option is used to specify how to sort the queries included in the result. For some of the sorting options, there is the choice between using the total and the average for the sorting. The sorting options are listed in Table 9-3 and are also available from the mysqldumpslow --help output. The Total column specifies the option to use to sort by the total, and the Average column shows the option to sort by the average.

Table 9-3. The sorting options for mysqldumpslow

Total Average Description

c Sort by the number of times (count) the query has

been executed.

l al Sort by the lock time.

r ar Sort by the number of rows sent.

t at Sort by the query time.

Option Default Value Description

-n 0 The minimum number of digits that must be in numbers before they are abstracted to N.

-r reverse the order the queries are returned.

-s at how to sort the queries. The default is to sort according to the average query time. The full list of sort options will be covered separately.

-t (All) The maximum number of queries to return in the result.

--verbose Print additional information during the execution of the script.

Table 9-2. (continued)

162

It can sometimes be useful to generate several reports using different sorting options to get a better picture of the queries being executed on the instance.

As a case study, consider an instance starting out with an empty slow query log file;

then the queries in Listing 9-3 are executed. These queries are executed with long_

query_time set to 0 for the session to record all queries which is useful to avoid having to spend a long time executing the queries.

Listing 9-3. The queries used to create slow query log events for a case study SET GLOBAL slow_query_log = ON;

SET long_query_time = 0;

SELECT * FROM world.city WHERE ID = 130;

SELECT * FROM world.city WHERE ID = 131;

SELECT * FROM world.city WHERE ID = 201;

SELECT * FROM world.city WHERE ID = 2010;

SELECT * FROM world.city WHERE ID = 1;

SELECT * FROM world.city WHERE ID = 828;

SELECT * FROM world.city WHERE ID = 131;

SELECT * FROM world.city WHERE CountryCode = 'AUS';

SELECT * FROM world.city WHERE CountryCode = 'CHN';

SELECT * FROM world.city WHERE CountryCode = 'IND';

SELECT * FROM world.city WHERE CountryCode = 'GBR';

SELECT * FROM world.city WHERE CountryCode = 'USA';

SELECT * FROM world.city WHERE CountryCode = 'NZL';

SELECT * FROM world.city WHERE CountryCode = 'BRA';

SELECT * FROM world.city WHERE CountryCode = 'AUS';

SELECT * FROM world.city WHERE CountryCode = 'DNK';

SELECT * FROM world.city ORDER BY Population DESC LIMIT 10;

SELECT * FROM world.city ORDER BY Population DESC LIMIT 4;

SELECT * FROM world.city ORDER BY Population DESC LIMIT 9;

There are three basic queries with different values for the WHERE clause or LIMIT clause. First, the city is found by the primary key which will search one row in order to return one row. Second, cities are found by the CountryCode which is a secondary index, so several rows are found but still the same number of rows are examined as returned.

Third, all cities are examined to return the most populous cities.

ChAPTEr 9 ThE SLOW QuEry LOG

Assuming the slow query log file is named mysql-slow.log and you are executing mysqldumpslow from the same directory where the file is located, then you can group the queries and order them by the number of times the queries have been executed as shown in Listing 9-4. The -t option is used to limit the report to include three (normalized) queries.

Listing 9-4. Using mysqldumpslow to sort the queries by count

shell$ mysqldumpslow -s c -t 3 mysql-slow.log Reading mysql slow query log from mysql-slow.log

Count: 9 Time=0.00s (0s) Lock=0.00s (0s) Rows=150.1 (1351), root[root]

@localhost

SELECT * FROM world.city WHERE CountryCode = 'S'

Count: 7 Time=0.02s (0s) Lock=0.00s (0s) Rows=1.0 (7), root[root]

@localhost

SELECT * FROM world.city WHERE ID = N

Count: 3 Time=0.00s (0s) Lock=0.00s (0s) Rows=7.7 (23), root[root]

@localhost

SELECT * FROM world.city ORDER BY Population DESC LIMIT N

Notice how the WHERE and LIMIT clauses have been modified to use N and 'S'. The query time is listed as Time=0.00s (0s) which has the average query time first (0.00s) and the total time in parenthesis. Similar for the lock and row statistics.

Since the mysqldumpslow script is written in Perl, it is relatively easy to modify the script if you want to include support for new sorting options or to change the output. For example, if you want to include more decimals for the average execution time, you can modify the printf statement just before the usage subroutine (lines 168–169 in the script included with MySQL 8.0.18) like

printf "Count: %d Time=%.6fs (%ds) Lock=%.2fs (%ds) Rows=%.1f (%d),

$user\@$host\n%s\n\n",

$c, $at,$t, $al,$l, $ar,$r, $_;

The change is in the Time=%.6fs part of the first line. That will print the average execution time with microseconds.

164

Summary

This chapter has shown how the slow query log can be used to collect information about the queries executed on the MySQL instance. The slow query log is focused on capturing queries based on the execution time and whether they use indexes (in practice whether they perform full table or index scans). The main advantages of the slow query log over the Performance Schema are that the log includes the exact statements executed and that it is persisted. The disadvantages are the overhead and that it is harder to get a report returning the queries you are interested in.

First, the configuration options used to configure the slow query log were discussed.

There are options to control the minimum execution time, whether queries not using indexes should be logged irrespective of the execution time, the types of queries to log, and more. In MySQL 8.0.14 and later, you can use the log_slow_extra to include more detailed information about the slow queries.

Second, two examples of the slow query log events were discussed. There was an example with the default information and one with log_slow_extra enabled. The raw events can be useful if you are looking for information of the queries executing at a given point in time. For more general queries, aggregating the data with the mysqldumpslow script is more useful. The use of mysqldumpslow was discussed in the last section.

The next part covers some tools that are useful in performance tuning starting with a discussion of monitoring using MySQL Enterprise Monitor as an example.

ChAPTEr 9 ThE SLOW QuEry LOG

PART III