SlideShare a Scribd company logo
1 of 101
Download to read offline
Introduction to MySQL Query Tuning
for Dev[Op]s
October 4, 2019
Sveta Smirnova
•Basics
•When MySQL Uses Indexes
•Diagnostics
EXPLAIN: estimation on how Optimizer works
Real Numbers: Inside Storage Engine
Real Numbers: Inside the Server
•How to Affect Query Plans
Table of Contents
2
• MySQL Support engineer
• Author of
• MySQL Troubleshooting
• JSON UDF functions
• FILTER clause for MySQL
• Speaker
• Percona Live, OOW, Fosdem,
DevConf, HighLoad...
Sveta Smirnova
3
Basics
Query Tuning
Concurrency
Environment
Troubleshooting Workflow
5
Query Tuning
Concurrency
Environment
Troubleshooting Workflow: This Session
6
$system = System::factory()
->setName($this->form->get(Field::NAME))
->setDescription(
$this->form->get(Field::DESCRIPTION)
);
DAO::system()->take($system);
The Query
7
$system = System::factory()
->setName($this->form->get(Field::NAME))
->setDescription(
$this->form->get(Field::DESCRIPTION)
);
DAO::system()->take($system);
The Query
7
cursor = conn.cursor()
q = ’’’UPDATE ‘foo‘ SET my_date=NOW(),
subject = %s,
msg = %s,
address = %s,
updated_at = NOW()
WHERE id=%s
’’’
cursor.execute(q, [
remote_resp.get(’subject’),
remote_resp.get(’msg’),
remote_resp.get(’address’),
my_id
])
The Query
7
cursor = conn.cursor()
q = ’’’UPDATE ‘foo‘ SET my_date=NOW(),
subject = %s,
msg = %s,
address = %s,
updated_at = NOW()
WHERE id=%s
’’’
cursor.execute(q, [
remote_resp.get(’subject’),
remote_resp.get(’msg’),
remote_resp.get(’address’),
my_id
])
The Query
7
SELECT dept_name, title, gender,
min(salary) AS mins, max(salary) AS maxs
FROM employees
JOIN salaries USING(emp_no)
JOIN titles USING(emp_no)
JOIN dept_emp USING(emp_no)
JOIN departments USING(dept_no)
JOIN dept_manager USING(dept_no)
WHERE dept_manager.to_date = ’9999-01-01’
GROUP BY dept_name, title, gender
ORDER BY gender, maxs DESC;
The Query
7
SELECT dept_name, title, gender,
min(salary) AS mins, max(salary) AS maxs
FROM employees
JOIN salaries USING(emp_no)
JOIN titles USING(emp_no)
JOIN dept_emp USING(emp_no)
JOIN departments USING(dept_no)
JOIN dept_manager USING(dept_no)
WHERE dept_manager.to_date = ’9999-01-01’
GROUP BY dept_name, title, gender
ORDER BY gender, maxs DESC;
The Query
7
• PMM QAN
Allways Tune Raw Query
8
• PMM QAN
• Slow Query Log
Allways Tune Raw Query
8
• PMM QAN
• Slow Query Log
• Application log
Allways Tune Raw Query
8
• PMM QAN
• Slow Query Log
• Application log
• ...
Allways Tune Raw Query
8
• Mind you data!
•
75,000,000 rows
• (INT, INT)
75,000,000 * (4 + 4) = 600,000,000 bytes = 572 MB
• (INT, INT, DATETIME, VARCHAR(255),
VARCHAR(255))
75,000,000 * (4 + 4 + 8 + 256 + 256) = 39,600,000,000 bytes = 37 G
• 39,600,000,000 / 600,000,000 = 66
Slow is relative
9
• Mind you data!
• Mind use case
• Popular website
•
Admin interface
• Weekly cron job
Slow is relative
9
• Mind you data!
• Mind use case
• Mind location
• Server, used by multiple connections
•
Dedicated for OLAP queries
Slow is relative
9
• MySQL performs a job to execute a query
Why Query can be Slow
10
• MySQL performs a job to execute a query
• In worst case scenario it will do a full table scan
•
CREATE INDEX
• ANALYZE TABLE ... UPDATE HISTOGRAM ON
Why Query can be Slow
10
• MySQL performs a job to execute a query
• In worst case scenario it will do a full table scan
•
CREATE INDEX
• ANALYZE TABLE ... UPDATE HISTOGRAM ON
• Incorrect index can be used
Why Query can be Slow
10
Query sent
Connection Pool: Authentication, Caches; SQL interface; Parser
Optimizer
Storage engines
Hardware
Query Execution Workflow
11
SELECT name FROM users SELECT name FROM users WHERE id=12
1 2 5 6 7 9 12 16 18 21 22 23 24 25
Full Table Scan
12
SELECT name FROM users SELECT name FROM users WHERE id=12
1 2 5 6 7 9 12 16 18 21 22 23 24 25
After Index Added
13
d001
d003
d008
d009
d003******
d009******
d008******
d009******
d001******
d003******
d009******
d008******
d009******
d001******
d008******
d008******
d001******
• B-Tree (Mostly)
• Fractal Tree
• LSM Tree
• R-Tree (Spatial)
• Hash (Memory SE)
• Engine-dependent
MySQL Indexes
14
• Single column
CREATE INDEX index name ON
the table(the column)
• Multiple columns
CREATE INDEX index name ON
the table(column1, column2)
How to Create an Index
15
• Single column
ALTER TABLE table name ADD INDEX
[index name] (the column)
• Multiple columns
ALTER TABLE table name ADD INDEX
[index name] (column1, column2)
How to Create an Index
15
When MySQL Uses Indexes
•
WHERE the column = a value
• WHERE the column IN(value1, value2,
value3)
• WHERE the column LIKE ’value%’
• WHERE the column LIKE ’%value’
Conditions
17
•
WHERE left part = value1 AND
right part = value2
• WHERE left part = value1 OR
right part = value2
• WHERE right part = value1 AND
left part = value2
•
WHERE right part = value1 OR left part =
value2
Conditions
17
•
table1 JOIN table2 ON table1.column1
= table2.column2
Joins
18
•
table1 JOIN table2 ON table1.column1
= table2.column2
• Same as FROM table1, table2 WHERE
table1.column1 = table2.column2
Joins
18
•
GROUP BY the column
• GROUP BY left part, right part
• GROUP BY right part, left part
• GROUP BY the index, another index
GROUP BY
19
•
ORDER BY the column
• ORDER BY left part, right part
• ORDER BY right part, left part
• ORDER BY the index, another index
ORDER BY
20
5.7 ORDER BY left part DESC, right part ASC
8.0 ORDER BY left part DESC, right part
ASC
• left part must be descending
• right part must be ascending
• the index(left part DESC, right part ASC)
ORDER BY
20
• Deterministic, built-in
•
Return same value for the same argument
• WHERE the column = FLOOR(123.45)
Expressions
21
• Deterministic, built-in
•
Return same value for the same argument
• WHERE the column = FLOOR(123.45)
• Non-deterministic
•
Return different values for different invocations
• WHERE the column = RAND() ∗ 100
Expressions
21
• Deterministic, built-in
•
Return same value for the same argument
• WHERE the column = FLOOR(123.45)
• Non-deterministic
•
Return different values for different invocations
• WHERE the column = RAND() ∗ 100
•
Stored functions and UDFs
• Indexes are not used
Use generated column indexes
Expressions
21
Diagnostics
Diagnostics
EXPLAIN: estimation on how Optimizer works
• EXPLAIN
•
Estimates what happens during query execution
5.6- EXTENDED
5.6- PARTITIONS
5.6+ FORMAT=JSON
8.0+ FORMAT=TREE
How to Find how MySQL Uses Indexes
24
• EXPLAIN
•
Estimates what happens during query execution
5.6- EXTENDED
5.6- PARTITIONS
5.6+ FORMAT=JSON
8.0+ FORMAT=TREE
• INFORMATION SCHEMA.OPTIMIZER TRACE
•
Real data, collected after query was executed
• Advanced topic
How to Find how MySQL Uses Indexes
24
mysql> explain select * from t1G
*************************** 1. row ***************************
...
rows: 12
Extra: NULL
mysql> explain select * from t1 where f2=12G
*************************** 1. row ***************************
...
key: NULL
...
rows: 12
Extra: Using where
Same number of examined rows for both queries
Effect of Indexes: Before
25
mysql> alter table t1 add index(f2);
Query OK, 12 rows affected (0.07 sec)
Records: 12 Duplicates: 0 Warnings: 0
mysql> explain select * from t1 where f2=12G
*************************** 1. row ***************************
...
key: f2
key_len: 5
ref: const
rows: 1
Extra: NULL
1 row in set (0.00 sec)
Much more effective!
Only 1 row examined
Effect of Indexes: After
26
mysql> explain extended select * from t1 join t2 where t1.int_key=1;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+------+---------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | f... | Extra
+----+-------------+-------+-------+---------------+---------+---------+-------+------+------+---------
| 1 | SIMPLE | t1 | ref | int_key,ik | int_key | 5 | const | 4 | 100. | NULL
| 1 | SIMPLE | t2 | index | NULL | pk | 9 | NULL | 6 | 100. | Using in
Using join buffer |
(Block Nested Loop) |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+------+---------
2 rows in set, 1 warning (0.00 sec)
Note (Code 1003): /* select#1 */ select ‘test‘.‘t1‘.‘pk‘ AS ‘pk‘,‘test‘.‘t1‘.‘int_key‘ AS ‘int_key‘,‘te
AS ‘pk‘,‘test‘.‘t2‘.‘int_key‘ AS ‘int_key‘ from ‘test‘.‘t1‘ join ‘test‘.‘t2‘ where (‘test‘.‘t1‘.‘int_ke
Number of select
Select type
Tables, for which information is printed
How data is accessed
Possible keys
Key, which was actually used
Length of the key
Which columns were compared with the index
Number of examined rows
% of filtered rows
rows x filtered / 100 — number of row
which will be joined with another table
Additional informatio
Table, for which information is printed
Product of rows here: how many rows in all tables will be accessed
For this example estimated value is 4*6 = 24
Actual (optimized) query as executed by MySQL Server
EXPLAIN: overview
27
mysql> explain extended select * from t1 join t2 where...
+----+-------------+-------+-------+***
| id | select_type | table | type |***
+----+-------------+-------+-------+***
| 1 | SIMPLE | t1 | ref |***
| 1 | SIMPLE | t2 | index |***
+----+-------------+-------+-------+***
2 rows in set, 1 warning (0.00 sec)
SIMPLE;PRIMARY;UNION;DEPENDENT UNION;UNION RESULT;
SUBQUERY;DEPENDENT SUBQUERY;DERIVED;MATERIALIZED
system
const
eq ref
ref
fulltext
ref or null
index merge
unique subquery
index subquery
range
index
ALL
EXPLAIN in Details
28
mysql> explain extended select * from t1 join t2 where t1.int_key=1;
***+---------------+---------+---------+-------+***
***| possible_keys | key | key_len | ref |***
***+---------------+---------+---------+-------+***
***| int_key,ik | int_key | 5 | const |***
***| NULL | pk | 9 | NULL |***
***+---------------+---------+---------+-------+***
2 rows in set, 1 warning (0.00 sec)
Keys, which can be used for resolving the query
Only one key was actually used
Actual length of the key (Important for multiple-column keys)
Which columns were compared with the index
Constant
Numeric in our case
Index used
to resolve rows
EXPLAIN in Details: keys
29
mysql> explain extended select * from t1 join t2 where t1.int_key=1;
***+-------+----------+----------------------------------------------------+
***| rows | filtered | Extra |
***+-------+----------+----------------------------------------------------+
***| 4 | 100.00 | NULL |
***| 6 | 100.00 | Using index; Using join buffer (Block Nested Loop) |
***+-------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
Number of rows accessed
% of rows filtered
Additional information:
how query is resolved
Using filesort
Using temporary
etc.
4X6
=
24
All rows used
EXPLAIN in Details: rows
30
mysql> explain select count(*) from employees where hire_date > ’1995-01-01’
********************** 1. row **********************
id: 1
select_type: SIMPLE
table: employees
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 300157
Extra: Using where
1 row in set (0.00 sec)
All rows in the table examined
Worst plan ever!
EXPLAIN Type by Example: ALL
31
• We need to add index to table employees first
mysql> alter table employees add index(hire_date);
Query OK, 0 rows affected (3.48 sec)
Records: 0 Duplicates: 0 Warnings: 0
EXPLAIN Type by Example: range
32
mysql> explain select count(*) from employees where hire_date>’1995-01-01’G
********************** 1. row **********************
id: 1
select_type: SIMPLE
table: employees
type: range
possible_keys: hire_date
key: hire_date
key_len: 3
ref: NULL
rows: 68654
Extra: Using where; Using index
1 row in set (0.00 sec)
Only rows from given range used
Compare with ALL:
300157/68654 = 4.3720
4 times less rows examined!
EXPLAIN Type by Example: range
33
• Consists of two or more columns
Combined Indexes
34
• Consists of two or more columns
• Only leftmost part used
mysql> alter table City add key
-> comb(CountryCode, District, Population),
-> drop key CountryCode;
Combined Indexes
34
mysql> explain select * from City where CountryCode = ’USA’G
********************** 1. row ******************
table: City
type: ref
possible_keys: comb
key: comb
key_len: 3
ref: const
rows: 273
Uses first field from the comb key
Combined Indexes: example 1
35
mysql> explain select * from City where 
-> District = ’California’ and population > 10000G
********************** 1. row ******************
table: City
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3868
Can’t use combined index:
not a leftmost part
Does not have the CountryCode
in the where clause
= can’t use comb index
Combined Indexes: example 2
36
• Key len = total size (in bytes)
• Index
• comb(CountryCode, District, Population)
Explain: Fields:
key: comb CountryCode char(3)
key len: 3 District char(20)
Population int(11)
3 ->Char(3) ->First field is used
Combined Indexes: key len
37
mysql> explain select count(*) from titles where title=’Senior Engineer’G
********************** 1. row **********************
id: 1
select_type: SIMPLE
table: titles
type: index
possible_keys: NULL
key: emp_no
key_len: 4
ref: NULL
rows: 444033
Extra: Using where; Using index
1 row in set (0.11 sec)
No row in the table was accessed to resolve the query!
Only index used
Still all records in the index were scanned
EXPLAIN Type by Example: index
38
•
Covered index = cover all fields in the query
select name from City
where CountryCode = ’USA’ and District = ’Alaska’ and population > 10000
mysql> alter table City add key
-> cov1(CountryCode, District, population, name);
Uses all fields in the query in particular order
1. Where part 2. Group By/Order (not used now) 3. Select part
Covered Indexes
39
mysql> explain select name from City where CountryCode = ’USA’ 
-> and District = ’Alaska’ and population > 10000G
*************************** 1. row ***********
table: City
type: range
possible_keys: cov1
key: cov1
key_len: 27
ref: NULL
rows: 1
Extra: Using where; Using index
Covered index is used
MySQL will only use index
Will not go to the data file
EXPLAIN by Example: Covered Indexes
40
Diagnostics
Real Numbers: Inside Storage Engine
• EXPLAIN is optimistic
mysql> explain select * from ol
-> where thread_id=10432 and site_id != 9939 order by id limit 3G
*************************** 1. row ***************************
id: 1 | ref: NULL
select_type: SIMPLE | rows: 33
table: ol | filtered: 8.07
partitions: NULL | Extra: Using where
type: index
possible_keys: thread_id
key: PRIMARY
key_len: 4
1 row in set, 1 warning (0,00 sec)
Handler * Status Variables
42
• Status variables ’Handler *’ show truth
mysql> flush status; select * from ol
-> where thread_id=10432 and site_id != 9939 order by id limit 3;
mysql> show status like ’Handler%’;
+----------------------------+--------+
| Variable_name | Value |
+----------------------------+--------+
...
| Handler_read_first | 1 |
| Handler_read_key | 1 |
| Handler_read_last | 0 |
| Handler_read_next | 100000 |
...
Handler * Status Variables
42
Diagnostics
Real Numbers: Inside the Server
•
SHOW [FULL] PROCESSLIST
•
INFORMATION SCHEMA.PROCESSLIST
•
performance schema.THREADS
PROCESSLIST
44
•
SHOW [FULL] PROCESSLIST
•
INFORMATION SCHEMA.PROCESSLIST
•
performance schema.THREADS
• Your first alert in case of performance issue
PROCESSLIST
44
•
SHOW [FULL] PROCESSLIST
•
INFORMATION SCHEMA.PROCESSLIST
•
performance schema.THREADS
• Your first alert in case of performance issue
• Shows all queries, running at the moment
PROCESSLIST
44
• Can be seen in PROCESSLIST
mysql> show processlistG
************************ 1. row ************************
Id: 7
User: root
Host: localhost:48799
db: employees
Command: Query
Time: 2
State: Sending data
Info: select count(*) from employees join titles using(emp_no)
where title=’Senior Engineer’
...
Execution Stages
45
• Can be seen in PROCESSLIST
• Very useful when you need to answer on question:
”What is my server doing now?”
Execution Stages
45
•
PERFORMANCE SCHEMA.EVENTS STAGES *
mysql> select eshl.event_name, substr(sql_text, 1, 15) as ‘sql‘,
-> eshl.timer_wait/1000000000000 w_s from events_stages_history_long
-> eshl join events_statements_history_long esthl on
-> (eshl.nesting_event_id = esthl.event_id) where
-> esthl.current_schema=’employees’ and sql_text like
-> ’select count(*) from employees%’ order by eshl.timer_start asc;
+--------------------------------+-----------------+--------+
| event_name | sql | w_s |
+--------------------------------+-----------------+--------+
| stage/sql/starting | select count(*) | 0.0002 |
| stage/sql/checking permissions | select count(*) | 0.0000 |
...
Execution Stages
45
•
PERFORMANCE SCHEMA.EVENTS STAGES *
...
| stage/sql/checking permissions | select count(*) | 0.0000 |
| stage/sql/Opening tables | select count(*) | 0.0000 |
| stage/sql/init | select count(*) | 0.0001 |
| stage/sql/System lock | select count(*) | 0.0000 |
| stage/sql/optimizing | select count(*) | 0.0000 |
| stage/sql/statistics | select count(*) | 0.0001 |
| stage/sql/preparing | select count(*) | 0.0000 |
| stage/sql/executing | select count(*) | 0.0000 |
| stage/sql/Sending data | select count(*) | 5.4915 |
| stage/sql/end | select count(*) | 0.0000 |
...
Execution Stages
45
• Status variables
mysql> flush status;
Query OK, 0 rows affected (0,01 sec)
mysql> select count(*) from employees join titles using(emp_no)
-> where title=’Senior Engineer’;
+----------+
| count(*) |
+----------+
| 97750 |
+----------+
1 row in set (5,44 sec)
Temporary tables and other job
46
• Status variables
mysql> select * from performance_schema.session_status
-> where variable_name in (’Created_tmp_tables’,
-> ’Created_tmp_disk_tables’, ’Select_full_join’,
-> ’Select_full_range_join’, ’Select_range’,
-> ’Select_range_check’, ’Select_scan’, ’Sort_merge_passes’,
-> ’Sort_range’, ’Sort_rows’, ’Sort_scan’) and variable_value > 0;
+------------------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+------------------------+----------------+
| Select_scan | 2 |
+------------------------+----------------+
1 row in set (0,00 sec)
Temporary tables and other job
46
• PERFORMANCE SCHEMA.EVENTS STATEMENTS *
mysql> select * from performance_schema.events_statements_history_long
-> where sql_text like ’select count(*) from employees join %’G
*************************** 1. row ****************************
...
ROWS_SENT: 1 SELECT_RANGE_CHECK: 0
ROWS_EXAMINED: 541058 SELECT_SCAN: 1
CREATED_TMP_DISK_TABLES: 0 SORT_MERGE_PASSES: 0
CREATED_TMP_TABLES: 0 SORT_RANGE: 0
SELECT_FULL_JOIN: 0 SORT_ROWS: 0
SELECT_FULL_RANGE_JOIN: 0 SORT_SCAN: 0
SELECT_RANGE: 0 NO_INDEX_USED: 0
Temporary tables and other job
46
•
sys.statement analysis
mysql> select * from statement_analysis where query like ’SELECT COUNT
-> ( * ) FROM ‘emplo%’ and db=’employees’G
*************************** 1. row ***************************
query: SELECT COUNT ( * ) FROM ‘emplo ... ‘emp_no‘ ) WHE...
db: employees max_latency: 5.59 s
full_scan: avg_latency: 5.41 s
exec_count: 7 lock_latency: 2.24 ms
err_count: 0 rows_sent: 7
warn_count: 0 rows_sent_avg: 1
total_latency: 37.89 s rows_examined: 3787406
Temporary tables and other job
46
•
sys.statement analysis
rows_examined_avg: 541058
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 0
tmp_disk_tables: 0
rows_sorted: 0
sort_merge_passes: 0
digest: 4086bc3dc6510a1d9c8f2fe1f59f0943
first_seen: 2016-04-14 15:19:19
last_seen: 2016-04-14 16:13:14
Temporary tables and other job
46
How to Affect Query Plans
• Index statistics
• Histogram statistics
• Optimizer switches
• Bugs in optimizer
What has Effect on Query Optimizer Plans?
48
• Collected by storage engine
Index Statistics
49
• Collected by storage engine
• Used by Optimizer
Index Statistics
49
• Can be examined by SHOW INDEX command
mysql> show index from sbtest1;
+---------+----------+-------------+-------------+
| Table | Key_name | Column_name | Cardinality |
+---------+----------+-------------+-------------+
| sbtest1 | k_1 | k | 49142 |
+---------+----------+-------------+-------------+
mysql> select count(distinct id), count(distinct k) from sbtest1;
+--------------------+-------------------+
| count(distinct id) | count(distinct k) |
+--------------------+-------------------+
| 100000 | 17598 |
+--------------------+-------------------+
Index Statistics
49
• Can be updated
• ANALYZE TABLE
•
If does not help: rebuild table
OPTIMIZE TABLE
ALTER TABLE ENGINE=INNODB; ANALYZE TABLE
Index Statistics
49
• Since version 8.0
Histogram Statistics
50
• Since version 8.0
• Collected and used by the Optimizer
Histogram Statistics
50
• Since version 8.0
• Collected and used by the Optimizer
• Can be examined in Information Schema
mysql> select HISTOGRAM from information_schema.column_statistics
-> where table_name=’example’G
*************************** 1. row ***************************
HISTOGRAM: {"buckets": [[1, 0.6], [2, 0.8], [3, 1.0]],
"data-type": "int", "null-values": 0.0, "collation-id": 8,
"last-updated": "2018-11-07 09:07:19.791470",
"sampling-rate": 1.0, "histogram-type": "singleton",
"number-of-buckets-specified": 3}
1 row in set (0.00 sec)
Histogram Statistics
50
• Since version 8.0
• Collected and used by the Optimizer
• Can be examined in Information Schema
More details
Histogram Statistics
50
mysql> select @@optimizer_switchG
*************************** 1. row ***************************
@@optimizer_switch: index_merge=on,index_merge_union=on,
index_merge_sort_union=on,index_merge_intersection=on,
engine_condition_pushdown=on,index_condition_pushdown=on,
mrr=on,mrr_cost_based=on,
block_nested_loop=on,batched_key_access=off,
materialization=on,semijoin=on,loosescan=on,firstmatch=on,
duplicateweedout=on,subquery_materialization_cost_based=on,
use_index_extensions=on,condition_fanout_filter=on,derived_merge=on
1 row in set (0,00 sec)
Optimizer Switches
51
• Turn ON and OFF particular optimization
Optimizer Switches
51
• Turn ON and OFF particular optimization
• Can be not helpful
• Especially for queries, tuned for previous versions
Optimizer Switches
51
• Turn ON and OFF particular optimization
• Can be not helpful
• Work with them as with any other option
• Turn OFF and try
SET optimizer_switch = ’use_index_extensions=off’;
SELECT ...
EXPLAIN SELECT ...
Optimizer Switches
51
• Turn ON and OFF particular optimization
• Can be not helpful
• Work with them as with any other option
•
If helps implement in queries
SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 ...;
SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE ...;
Optimizer Switches
51
• Optimizer choses wrong index for no reason
Bugs in Optimizer
52
• Optimizer choses wrong index for no reason
• Statistics is up to date
• Histograms are not usable
Bugs in Optimizer
52
• Optimizer choses wrong index for no reason
• Statistics is up to date
• Histograms are not usable
• Solution
•
Use index hints
FORCE INDEX
IGNORE INDEX
Bugs in Optimizer
52
• Optimizer choses wrong index for no reason
• Statistics is up to date
• Histograms are not usable
• Solution
• On every upgrade
• Remove index hints
• Test if query improved
•
You must do it even for minor version upgrades!
Bugs in Optimizer
52
• EXPLAIN is essential for query tuning
• Real job is done by storage engine
• Index statistics affect query execution plan
• All index hints, optimizer hints and other
workarounds must be validated on each
upgrade
Summary
53
EXPLAIN Syntax
EXPLAIN FORMAT=JSON is Cool! series
Troubleshooting Performance add-ons
Optimizer Satistics aka Histograms
Optimizer Hints
Tracing the Optimizer
More information
54
Alexander Rubin for combined and covered index examples
Special thanks
55
www.slideshare.net/SvetaSmirnova
twitter.com/svetsmirnova
github.com/svetasmirnova
Thank you!
56
Percona’s open source database experts are true
superheroes, improving database performance for
customers across the globe.
Percona’s open source database experts are true
superheroes, improving database performance for
customers across the globe.
Discover what it means to have a Percona career
with the smartest people in the database
performance industries, solving the most
challenging problems our customers come across.
We’re Hiring!
57
DATABASE PERFORMANCE
MATTERS

More Related Content

What's hot

How to migrate from MySQL to MariaDB without tears
How to migrate from MySQL to MariaDB without tearsHow to migrate from MySQL to MariaDB without tears
How to migrate from MySQL to MariaDB without tearsSveta Smirnova
 
Highload Perf Tuning
Highload Perf TuningHighload Perf Tuning
Highload Perf TuningHighLoad2009
 
How to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraHow to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraSveta Smirnova
 
Using Apache Spark and MySQL for Data Analysis
Using Apache Spark and MySQL for Data AnalysisUsing Apache Spark and MySQL for Data Analysis
Using Apache Spark and MySQL for Data AnalysisSveta Smirnova
 
Preparse Query Rewrite Plugins
Preparse Query Rewrite PluginsPreparse Query Rewrite Plugins
Preparse Query Rewrite PluginsSveta Smirnova
 
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB ClusterHow to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB ClusterSveta Smirnova
 
Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...Sveta Smirnova
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON? Sveta Smirnova
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysqlsabir18
 
Why MySQL Replication Fails, and How to Get it Back
Why MySQL Replication Fails, and How to Get it BackWhy MySQL Replication Fails, and How to Get it Back
Why MySQL Replication Fails, and How to Get it BackSveta Smirnova
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in ActionSveta Smirnova
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL PerformanceSveta Smirnova
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadDevelopers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadmCloud
 
Summary tables with flexviews
Summary tables with flexviewsSummary tables with flexviews
Summary tables with flexviewsJustin Swanhart
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsSveta Smirnova
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingSveta Smirnova
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionSveta Smirnova
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionSveta Smirnova
 

What's hot (20)

How to migrate from MySQL to MariaDB without tears
How to migrate from MySQL to MariaDB without tearsHow to migrate from MySQL to MariaDB without tears
How to migrate from MySQL to MariaDB without tears
 
Highload Perf Tuning
Highload Perf TuningHighload Perf Tuning
Highload Perf Tuning
 
How to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with GaleraHow to Avoid Pitfalls in Schema Upgrade with Galera
How to Avoid Pitfalls in Schema Upgrade with Galera
 
Using Apache Spark and MySQL for Data Analysis
Using Apache Spark and MySQL for Data AnalysisUsing Apache Spark and MySQL for Data Analysis
Using Apache Spark and MySQL for Data Analysis
 
Preparse Query Rewrite Plugins
Preparse Query Rewrite PluginsPreparse Query Rewrite Plugins
Preparse Query Rewrite Plugins
 
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB ClusterHow to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
How to Avoid Pitfalls in Schema Upgrade with Percona XtraDB Cluster
 
Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON?
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysql
 
Why MySQL Replication Fails, and How to Get it Back
Why MySQL Replication Fails, and How to Get it BackWhy MySQL Replication Fails, and How to Get it Back
Why MySQL Replication Fails, and How to Get it Back
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
 
MySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB StatusMySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB Status
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadDevelopers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
 
Summary tables with flexviews
Summary tables with flexviewsSummary tables with flexviews
Summary tables with flexviews
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
 

Similar to Introduction to MySQL Query Tuning for Dev[Op]s

Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Sveta Smirnova
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialSveta Smirnova
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksMYXPLAIN
 
sveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionDariia Seimova
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearchRyosuke Nakamura
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...Sveta Smirnova
 
PostgreSQL query planner's internals
PostgreSQL query planner's internalsPostgreSQL query planner's internals
PostgreSQL query planner's internalsAlexey Ermakov
 
Query parameterization
Query parameterizationQuery parameterization
Query parameterizationRiteshkiit
 
Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?Sveta Smirnova
 
Amazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Web Services
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
SQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingSQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingAbouzar Noori
 
POLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel QueryPOLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel Queryoysteing
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Sveta Smirnova
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQLGeorgi Sotirov
 

Similar to Introduction to MySQL Query Tuning for Dev[Op]s (20)

Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?
 
MySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete TutorialMySQL Performance Schema in Action: the Complete Tutorial
MySQL Performance Schema in Action: the Complete Tutorial
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
sveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in action
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
A Billion Goods in a Few Categories: When Optimizer Histograms Help and When ...
 
Quick Wins
Quick WinsQuick Wins
Quick Wins
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 
Mssql
MssqlMssql
Mssql
 
PostgreSQL query planner's internals
PostgreSQL query planner's internalsPostgreSQL query planner's internals
PostgreSQL query planner's internals
 
Query parameterization
Query parameterizationQuery parameterization
Query parameterization
 
Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?
 
Amazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and Optimization
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
SQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingSQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and Profiling
 
POLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel QueryPOLARDB for MySQL - Parallel Query
POLARDB for MySQL - Parallel Query
 
Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?Billion Goods in Few Categories: How Histograms Save a Life?
Billion Goods in Few Categories: How Histograms Save a Life?
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 

More from Sveta Smirnova

MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?
MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?
MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?Sveta Smirnova
 
Database in Kubernetes: Diagnostics and Monitoring
Database in Kubernetes: Diagnostics and MonitoringDatabase in Kubernetes: Diagnostics and Monitoring
Database in Kubernetes: Diagnostics and MonitoringSveta Smirnova
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveSveta Smirnova
 
MySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for DevelopersMySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for DevelopersSveta Smirnova
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOpsSveta Smirnova
 
MySQL Test Framework для поддержки клиентов и верификации багов
MySQL Test Framework для поддержки клиентов и верификации баговMySQL Test Framework для поддержки клиентов и верификации багов
MySQL Test Framework для поддержки клиентов и верификации баговSveta Smirnova
 
MySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessMySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessSveta Smirnova
 
Производительность MySQL для DevOps
 Производительность MySQL для DevOps Производительность MySQL для DevOps
Производительность MySQL для DevOpsSveta Smirnova
 
How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?Sveta Smirnova
 
Современному хайлоду - современные решения: MySQL 8.0 и улучшения Percona
Современному хайлоду - современные решения: MySQL 8.0 и улучшения PerconaСовременному хайлоду - современные решения: MySQL 8.0 и улучшения Percona
Современному хайлоду - современные решения: MySQL 8.0 и улучшения PerconaSveta Smirnova
 
How Safe is Asynchronous Master-Master Setup?
 How Safe is Asynchronous Master-Master Setup? How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?Sveta Smirnova
 
Что нужно знать о трёх топовых фичах MySQL
Что нужно знать  о трёх топовых фичах  MySQLЧто нужно знать  о трёх топовых фичах  MySQL
Что нужно знать о трёх топовых фичах MySQLSveta Smirnova
 
Optimizer Histograms: When they Help and When Do Not?
Optimizer Histograms: When they Help and When Do Not?Optimizer Histograms: When they Help and When Do Not?
Optimizer Histograms: When they Help and When Do Not?Sveta Smirnova
 

More from Sveta Smirnova (13)

MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?
MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?
MySQL 2024: Зачем переходить на MySQL 8, если в 5.х всё устраивает?
 
Database in Kubernetes: Diagnostics and Monitoring
Database in Kubernetes: Diagnostics and MonitoringDatabase in Kubernetes: Diagnostics and Monitoring
Database in Kubernetes: Diagnostics and Monitoring
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to Have
 
MySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for DevelopersMySQL Cookbook: Recipes for Developers
MySQL Cookbook: Recipes for Developers
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOps
 
MySQL Test Framework для поддержки клиентов и верификации багов
MySQL Test Framework для поддержки клиентов и верификации баговMySQL Test Framework для поддержки клиентов и верификации багов
MySQL Test Framework для поддержки клиентов и верификации багов
 
MySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your BusinessMySQL Cookbook: Recipes for Your Business
MySQL Cookbook: Recipes for Your Business
 
Производительность MySQL для DevOps
 Производительность MySQL для DevOps Производительность MySQL для DevOps
Производительность MySQL для DevOps
 
How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?
 
Современному хайлоду - современные решения: MySQL 8.0 и улучшения Percona
Современному хайлоду - современные решения: MySQL 8.0 и улучшения PerconaСовременному хайлоду - современные решения: MySQL 8.0 и улучшения Percona
Современному хайлоду - современные решения: MySQL 8.0 и улучшения Percona
 
How Safe is Asynchronous Master-Master Setup?
 How Safe is Asynchronous Master-Master Setup? How Safe is Asynchronous Master-Master Setup?
How Safe is Asynchronous Master-Master Setup?
 
Что нужно знать о трёх топовых фичах MySQL
Что нужно знать  о трёх топовых фичах  MySQLЧто нужно знать  о трёх топовых фичах  MySQL
Что нужно знать о трёх топовых фичах MySQL
 
Optimizer Histograms: When they Help and When Do Not?
Optimizer Histograms: When they Help and When Do Not?Optimizer Histograms: When they Help and When Do Not?
Optimizer Histograms: When they Help and When Do Not?
 

Recently uploaded

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Recently uploaded (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

Introduction to MySQL Query Tuning for Dev[Op]s

  • 1. Introduction to MySQL Query Tuning for Dev[Op]s October 4, 2019 Sveta Smirnova
  • 2. •Basics •When MySQL Uses Indexes •Diagnostics EXPLAIN: estimation on how Optimizer works Real Numbers: Inside Storage Engine Real Numbers: Inside the Server •How to Affect Query Plans Table of Contents 2
  • 3. • MySQL Support engineer • Author of • MySQL Troubleshooting • JSON UDF functions • FILTER clause for MySQL • Speaker • Percona Live, OOW, Fosdem, DevConf, HighLoad... Sveta Smirnova 3
  • 9. cursor = conn.cursor() q = ’’’UPDATE ‘foo‘ SET my_date=NOW(), subject = %s, msg = %s, address = %s, updated_at = NOW() WHERE id=%s ’’’ cursor.execute(q, [ remote_resp.get(’subject’), remote_resp.get(’msg’), remote_resp.get(’address’), my_id ]) The Query 7
  • 10. cursor = conn.cursor() q = ’’’UPDATE ‘foo‘ SET my_date=NOW(), subject = %s, msg = %s, address = %s, updated_at = NOW() WHERE id=%s ’’’ cursor.execute(q, [ remote_resp.get(’subject’), remote_resp.get(’msg’), remote_resp.get(’address’), my_id ]) The Query 7
  • 11. SELECT dept_name, title, gender, min(salary) AS mins, max(salary) AS maxs FROM employees JOIN salaries USING(emp_no) JOIN titles USING(emp_no) JOIN dept_emp USING(emp_no) JOIN departments USING(dept_no) JOIN dept_manager USING(dept_no) WHERE dept_manager.to_date = ’9999-01-01’ GROUP BY dept_name, title, gender ORDER BY gender, maxs DESC; The Query 7
  • 12. SELECT dept_name, title, gender, min(salary) AS mins, max(salary) AS maxs FROM employees JOIN salaries USING(emp_no) JOIN titles USING(emp_no) JOIN dept_emp USING(emp_no) JOIN departments USING(dept_no) JOIN dept_manager USING(dept_no) WHERE dept_manager.to_date = ’9999-01-01’ GROUP BY dept_name, title, gender ORDER BY gender, maxs DESC; The Query 7
  • 13. • PMM QAN Allways Tune Raw Query 8
  • 14. • PMM QAN • Slow Query Log Allways Tune Raw Query 8
  • 15. • PMM QAN • Slow Query Log • Application log Allways Tune Raw Query 8
  • 16. • PMM QAN • Slow Query Log • Application log • ... Allways Tune Raw Query 8
  • 17. • Mind you data! • 75,000,000 rows • (INT, INT) 75,000,000 * (4 + 4) = 600,000,000 bytes = 572 MB • (INT, INT, DATETIME, VARCHAR(255), VARCHAR(255)) 75,000,000 * (4 + 4 + 8 + 256 + 256) = 39,600,000,000 bytes = 37 G • 39,600,000,000 / 600,000,000 = 66 Slow is relative 9
  • 18. • Mind you data! • Mind use case • Popular website • Admin interface • Weekly cron job Slow is relative 9
  • 19. • Mind you data! • Mind use case • Mind location • Server, used by multiple connections • Dedicated for OLAP queries Slow is relative 9
  • 20. • MySQL performs a job to execute a query Why Query can be Slow 10
  • 21. • MySQL performs a job to execute a query • In worst case scenario it will do a full table scan • CREATE INDEX • ANALYZE TABLE ... UPDATE HISTOGRAM ON Why Query can be Slow 10
  • 22. • MySQL performs a job to execute a query • In worst case scenario it will do a full table scan • CREATE INDEX • ANALYZE TABLE ... UPDATE HISTOGRAM ON • Incorrect index can be used Why Query can be Slow 10
  • 23. Query sent Connection Pool: Authentication, Caches; SQL interface; Parser Optimizer Storage engines Hardware Query Execution Workflow 11
  • 24. SELECT name FROM users SELECT name FROM users WHERE id=12 1 2 5 6 7 9 12 16 18 21 22 23 24 25 Full Table Scan 12
  • 25. SELECT name FROM users SELECT name FROM users WHERE id=12 1 2 5 6 7 9 12 16 18 21 22 23 24 25 After Index Added 13
  • 27. • Single column CREATE INDEX index name ON the table(the column) • Multiple columns CREATE INDEX index name ON the table(column1, column2) How to Create an Index 15
  • 28. • Single column ALTER TABLE table name ADD INDEX [index name] (the column) • Multiple columns ALTER TABLE table name ADD INDEX [index name] (column1, column2) How to Create an Index 15
  • 29. When MySQL Uses Indexes
  • 30. • WHERE the column = a value • WHERE the column IN(value1, value2, value3) • WHERE the column LIKE ’value%’ • WHERE the column LIKE ’%value’ Conditions 17
  • 31. • WHERE left part = value1 AND right part = value2 • WHERE left part = value1 OR right part = value2 • WHERE right part = value1 AND left part = value2 • WHERE right part = value1 OR left part = value2 Conditions 17
  • 32. • table1 JOIN table2 ON table1.column1 = table2.column2 Joins 18
  • 33. • table1 JOIN table2 ON table1.column1 = table2.column2 • Same as FROM table1, table2 WHERE table1.column1 = table2.column2 Joins 18
  • 34. • GROUP BY the column • GROUP BY left part, right part • GROUP BY right part, left part • GROUP BY the index, another index GROUP BY 19
  • 35. • ORDER BY the column • ORDER BY left part, right part • ORDER BY right part, left part • ORDER BY the index, another index ORDER BY 20
  • 36. 5.7 ORDER BY left part DESC, right part ASC 8.0 ORDER BY left part DESC, right part ASC • left part must be descending • right part must be ascending • the index(left part DESC, right part ASC) ORDER BY 20
  • 37. • Deterministic, built-in • Return same value for the same argument • WHERE the column = FLOOR(123.45) Expressions 21
  • 38. • Deterministic, built-in • Return same value for the same argument • WHERE the column = FLOOR(123.45) • Non-deterministic • Return different values for different invocations • WHERE the column = RAND() ∗ 100 Expressions 21
  • 39. • Deterministic, built-in • Return same value for the same argument • WHERE the column = FLOOR(123.45) • Non-deterministic • Return different values for different invocations • WHERE the column = RAND() ∗ 100 • Stored functions and UDFs • Indexes are not used Use generated column indexes Expressions 21
  • 41. Diagnostics EXPLAIN: estimation on how Optimizer works
  • 42. • EXPLAIN • Estimates what happens during query execution 5.6- EXTENDED 5.6- PARTITIONS 5.6+ FORMAT=JSON 8.0+ FORMAT=TREE How to Find how MySQL Uses Indexes 24
  • 43. • EXPLAIN • Estimates what happens during query execution 5.6- EXTENDED 5.6- PARTITIONS 5.6+ FORMAT=JSON 8.0+ FORMAT=TREE • INFORMATION SCHEMA.OPTIMIZER TRACE • Real data, collected after query was executed • Advanced topic How to Find how MySQL Uses Indexes 24
  • 44. mysql> explain select * from t1G *************************** 1. row *************************** ... rows: 12 Extra: NULL mysql> explain select * from t1 where f2=12G *************************** 1. row *************************** ... key: NULL ... rows: 12 Extra: Using where Same number of examined rows for both queries Effect of Indexes: Before 25
  • 45. mysql> alter table t1 add index(f2); Query OK, 12 rows affected (0.07 sec) Records: 12 Duplicates: 0 Warnings: 0 mysql> explain select * from t1 where f2=12G *************************** 1. row *************************** ... key: f2 key_len: 5 ref: const rows: 1 Extra: NULL 1 row in set (0.00 sec) Much more effective! Only 1 row examined Effect of Indexes: After 26
  • 46. mysql> explain extended select * from t1 join t2 where t1.int_key=1; +----+-------------+-------+-------+---------------+---------+---------+-------+------+------+--------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | f... | Extra +----+-------------+-------+-------+---------------+---------+---------+-------+------+------+--------- | 1 | SIMPLE | t1 | ref | int_key,ik | int_key | 5 | const | 4 | 100. | NULL | 1 | SIMPLE | t2 | index | NULL | pk | 9 | NULL | 6 | 100. | Using in Using join buffer | (Block Nested Loop) | +----+-------------+-------+-------+---------------+---------+---------+-------+------+------+--------- 2 rows in set, 1 warning (0.00 sec) Note (Code 1003): /* select#1 */ select ‘test‘.‘t1‘.‘pk‘ AS ‘pk‘,‘test‘.‘t1‘.‘int_key‘ AS ‘int_key‘,‘te AS ‘pk‘,‘test‘.‘t2‘.‘int_key‘ AS ‘int_key‘ from ‘test‘.‘t1‘ join ‘test‘.‘t2‘ where (‘test‘.‘t1‘.‘int_ke Number of select Select type Tables, for which information is printed How data is accessed Possible keys Key, which was actually used Length of the key Which columns were compared with the index Number of examined rows % of filtered rows rows x filtered / 100 — number of row which will be joined with another table Additional informatio Table, for which information is printed Product of rows here: how many rows in all tables will be accessed For this example estimated value is 4*6 = 24 Actual (optimized) query as executed by MySQL Server EXPLAIN: overview 27
  • 47. mysql> explain extended select * from t1 join t2 where... +----+-------------+-------+-------+*** | id | select_type | table | type |*** +----+-------------+-------+-------+*** | 1 | SIMPLE | t1 | ref |*** | 1 | SIMPLE | t2 | index |*** +----+-------------+-------+-------+*** 2 rows in set, 1 warning (0.00 sec) SIMPLE;PRIMARY;UNION;DEPENDENT UNION;UNION RESULT; SUBQUERY;DEPENDENT SUBQUERY;DERIVED;MATERIALIZED system const eq ref ref fulltext ref or null index merge unique subquery index subquery range index ALL EXPLAIN in Details 28
  • 48. mysql> explain extended select * from t1 join t2 where t1.int_key=1; ***+---------------+---------+---------+-------+*** ***| possible_keys | key | key_len | ref |*** ***+---------------+---------+---------+-------+*** ***| int_key,ik | int_key | 5 | const |*** ***| NULL | pk | 9 | NULL |*** ***+---------------+---------+---------+-------+*** 2 rows in set, 1 warning (0.00 sec) Keys, which can be used for resolving the query Only one key was actually used Actual length of the key (Important for multiple-column keys) Which columns were compared with the index Constant Numeric in our case Index used to resolve rows EXPLAIN in Details: keys 29
  • 49. mysql> explain extended select * from t1 join t2 where t1.int_key=1; ***+-------+----------+----------------------------------------------------+ ***| rows | filtered | Extra | ***+-------+----------+----------------------------------------------------+ ***| 4 | 100.00 | NULL | ***| 6 | 100.00 | Using index; Using join buffer (Block Nested Loop) | ***+-------+----------+----------------------------------------------------+ 2 rows in set, 1 warning (0.00 sec) Number of rows accessed % of rows filtered Additional information: how query is resolved Using filesort Using temporary etc. 4X6 = 24 All rows used EXPLAIN in Details: rows 30
  • 50. mysql> explain select count(*) from employees where hire_date > ’1995-01-01’ ********************** 1. row ********************** id: 1 select_type: SIMPLE table: employees type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 300157 Extra: Using where 1 row in set (0.00 sec) All rows in the table examined Worst plan ever! EXPLAIN Type by Example: ALL 31
  • 51. • We need to add index to table employees first mysql> alter table employees add index(hire_date); Query OK, 0 rows affected (3.48 sec) Records: 0 Duplicates: 0 Warnings: 0 EXPLAIN Type by Example: range 32
  • 52. mysql> explain select count(*) from employees where hire_date>’1995-01-01’G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: employees type: range possible_keys: hire_date key: hire_date key_len: 3 ref: NULL rows: 68654 Extra: Using where; Using index 1 row in set (0.00 sec) Only rows from given range used Compare with ALL: 300157/68654 = 4.3720 4 times less rows examined! EXPLAIN Type by Example: range 33
  • 53. • Consists of two or more columns Combined Indexes 34
  • 54. • Consists of two or more columns • Only leftmost part used mysql> alter table City add key -> comb(CountryCode, District, Population), -> drop key CountryCode; Combined Indexes 34
  • 55. mysql> explain select * from City where CountryCode = ’USA’G ********************** 1. row ****************** table: City type: ref possible_keys: comb key: comb key_len: 3 ref: const rows: 273 Uses first field from the comb key Combined Indexes: example 1 35
  • 56. mysql> explain select * from City where -> District = ’California’ and population > 10000G ********************** 1. row ****************** table: City type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 3868 Can’t use combined index: not a leftmost part Does not have the CountryCode in the where clause = can’t use comb index Combined Indexes: example 2 36
  • 57. • Key len = total size (in bytes) • Index • comb(CountryCode, District, Population) Explain: Fields: key: comb CountryCode char(3) key len: 3 District char(20) Population int(11) 3 ->Char(3) ->First field is used Combined Indexes: key len 37
  • 58. mysql> explain select count(*) from titles where title=’Senior Engineer’G ********************** 1. row ********************** id: 1 select_type: SIMPLE table: titles type: index possible_keys: NULL key: emp_no key_len: 4 ref: NULL rows: 444033 Extra: Using where; Using index 1 row in set (0.11 sec) No row in the table was accessed to resolve the query! Only index used Still all records in the index were scanned EXPLAIN Type by Example: index 38
  • 59. • Covered index = cover all fields in the query select name from City where CountryCode = ’USA’ and District = ’Alaska’ and population > 10000 mysql> alter table City add key -> cov1(CountryCode, District, population, name); Uses all fields in the query in particular order 1. Where part 2. Group By/Order (not used now) 3. Select part Covered Indexes 39
  • 60. mysql> explain select name from City where CountryCode = ’USA’ -> and District = ’Alaska’ and population > 10000G *************************** 1. row *********** table: City type: range possible_keys: cov1 key: cov1 key_len: 27 ref: NULL rows: 1 Extra: Using where; Using index Covered index is used MySQL will only use index Will not go to the data file EXPLAIN by Example: Covered Indexes 40
  • 62. • EXPLAIN is optimistic mysql> explain select * from ol -> where thread_id=10432 and site_id != 9939 order by id limit 3G *************************** 1. row *************************** id: 1 | ref: NULL select_type: SIMPLE | rows: 33 table: ol | filtered: 8.07 partitions: NULL | Extra: Using where type: index possible_keys: thread_id key: PRIMARY key_len: 4 1 row in set, 1 warning (0,00 sec) Handler * Status Variables 42
  • 63. • Status variables ’Handler *’ show truth mysql> flush status; select * from ol -> where thread_id=10432 and site_id != 9939 order by id limit 3; mysql> show status like ’Handler%’; +----------------------------+--------+ | Variable_name | Value | +----------------------------+--------+ ... | Handler_read_first | 1 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 100000 | ... Handler * Status Variables 42
  • 65. • SHOW [FULL] PROCESSLIST • INFORMATION SCHEMA.PROCESSLIST • performance schema.THREADS PROCESSLIST 44
  • 66. • SHOW [FULL] PROCESSLIST • INFORMATION SCHEMA.PROCESSLIST • performance schema.THREADS • Your first alert in case of performance issue PROCESSLIST 44
  • 67. • SHOW [FULL] PROCESSLIST • INFORMATION SCHEMA.PROCESSLIST • performance schema.THREADS • Your first alert in case of performance issue • Shows all queries, running at the moment PROCESSLIST 44
  • 68. • Can be seen in PROCESSLIST mysql> show processlistG ************************ 1. row ************************ Id: 7 User: root Host: localhost:48799 db: employees Command: Query Time: 2 State: Sending data Info: select count(*) from employees join titles using(emp_no) where title=’Senior Engineer’ ... Execution Stages 45
  • 69. • Can be seen in PROCESSLIST • Very useful when you need to answer on question: ”What is my server doing now?” Execution Stages 45
  • 70. • PERFORMANCE SCHEMA.EVENTS STAGES * mysql> select eshl.event_name, substr(sql_text, 1, 15) as ‘sql‘, -> eshl.timer_wait/1000000000000 w_s from events_stages_history_long -> eshl join events_statements_history_long esthl on -> (eshl.nesting_event_id = esthl.event_id) where -> esthl.current_schema=’employees’ and sql_text like -> ’select count(*) from employees%’ order by eshl.timer_start asc; +--------------------------------+-----------------+--------+ | event_name | sql | w_s | +--------------------------------+-----------------+--------+ | stage/sql/starting | select count(*) | 0.0002 | | stage/sql/checking permissions | select count(*) | 0.0000 | ... Execution Stages 45
  • 71. • PERFORMANCE SCHEMA.EVENTS STAGES * ... | stage/sql/checking permissions | select count(*) | 0.0000 | | stage/sql/Opening tables | select count(*) | 0.0000 | | stage/sql/init | select count(*) | 0.0001 | | stage/sql/System lock | select count(*) | 0.0000 | | stage/sql/optimizing | select count(*) | 0.0000 | | stage/sql/statistics | select count(*) | 0.0001 | | stage/sql/preparing | select count(*) | 0.0000 | | stage/sql/executing | select count(*) | 0.0000 | | stage/sql/Sending data | select count(*) | 5.4915 | | stage/sql/end | select count(*) | 0.0000 | ... Execution Stages 45
  • 72. • Status variables mysql> flush status; Query OK, 0 rows affected (0,01 sec) mysql> select count(*) from employees join titles using(emp_no) -> where title=’Senior Engineer’; +----------+ | count(*) | +----------+ | 97750 | +----------+ 1 row in set (5,44 sec) Temporary tables and other job 46
  • 73. • Status variables mysql> select * from performance_schema.session_status -> where variable_name in (’Created_tmp_tables’, -> ’Created_tmp_disk_tables’, ’Select_full_join’, -> ’Select_full_range_join’, ’Select_range’, -> ’Select_range_check’, ’Select_scan’, ’Sort_merge_passes’, -> ’Sort_range’, ’Sort_rows’, ’Sort_scan’) and variable_value > 0; +------------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +------------------------+----------------+ | Select_scan | 2 | +------------------------+----------------+ 1 row in set (0,00 sec) Temporary tables and other job 46
  • 74. • PERFORMANCE SCHEMA.EVENTS STATEMENTS * mysql> select * from performance_schema.events_statements_history_long -> where sql_text like ’select count(*) from employees join %’G *************************** 1. row **************************** ... ROWS_SENT: 1 SELECT_RANGE_CHECK: 0 ROWS_EXAMINED: 541058 SELECT_SCAN: 1 CREATED_TMP_DISK_TABLES: 0 SORT_MERGE_PASSES: 0 CREATED_TMP_TABLES: 0 SORT_RANGE: 0 SELECT_FULL_JOIN: 0 SORT_ROWS: 0 SELECT_FULL_RANGE_JOIN: 0 SORT_SCAN: 0 SELECT_RANGE: 0 NO_INDEX_USED: 0 Temporary tables and other job 46
  • 75. • sys.statement analysis mysql> select * from statement_analysis where query like ’SELECT COUNT -> ( * ) FROM ‘emplo%’ and db=’employees’G *************************** 1. row *************************** query: SELECT COUNT ( * ) FROM ‘emplo ... ‘emp_no‘ ) WHE... db: employees max_latency: 5.59 s full_scan: avg_latency: 5.41 s exec_count: 7 lock_latency: 2.24 ms err_count: 0 rows_sent: 7 warn_count: 0 rows_sent_avg: 1 total_latency: 37.89 s rows_examined: 3787406 Temporary tables and other job 46
  • 76. • sys.statement analysis rows_examined_avg: 541058 rows_affected: 0 rows_affected_avg: 0 tmp_tables: 0 tmp_disk_tables: 0 rows_sorted: 0 sort_merge_passes: 0 digest: 4086bc3dc6510a1d9c8f2fe1f59f0943 first_seen: 2016-04-14 15:19:19 last_seen: 2016-04-14 16:13:14 Temporary tables and other job 46
  • 77. How to Affect Query Plans
  • 78. • Index statistics • Histogram statistics • Optimizer switches • Bugs in optimizer What has Effect on Query Optimizer Plans? 48
  • 79. • Collected by storage engine Index Statistics 49
  • 80. • Collected by storage engine • Used by Optimizer Index Statistics 49
  • 81. • Can be examined by SHOW INDEX command mysql> show index from sbtest1; +---------+----------+-------------+-------------+ | Table | Key_name | Column_name | Cardinality | +---------+----------+-------------+-------------+ | sbtest1 | k_1 | k | 49142 | +---------+----------+-------------+-------------+ mysql> select count(distinct id), count(distinct k) from sbtest1; +--------------------+-------------------+ | count(distinct id) | count(distinct k) | +--------------------+-------------------+ | 100000 | 17598 | +--------------------+-------------------+ Index Statistics 49
  • 82. • Can be updated • ANALYZE TABLE • If does not help: rebuild table OPTIMIZE TABLE ALTER TABLE ENGINE=INNODB; ANALYZE TABLE Index Statistics 49
  • 83. • Since version 8.0 Histogram Statistics 50
  • 84. • Since version 8.0 • Collected and used by the Optimizer Histogram Statistics 50
  • 85. • Since version 8.0 • Collected and used by the Optimizer • Can be examined in Information Schema mysql> select HISTOGRAM from information_schema.column_statistics -> where table_name=’example’G *************************** 1. row *************************** HISTOGRAM: {"buckets": [[1, 0.6], [2, 0.8], [3, 1.0]], "data-type": "int", "null-values": 0.0, "collation-id": 8, "last-updated": "2018-11-07 09:07:19.791470", "sampling-rate": 1.0, "histogram-type": "singleton", "number-of-buckets-specified": 3} 1 row in set (0.00 sec) Histogram Statistics 50
  • 86. • Since version 8.0 • Collected and used by the Optimizer • Can be examined in Information Schema More details Histogram Statistics 50
  • 87. mysql> select @@optimizer_switchG *************************** 1. row *************************** @@optimizer_switch: index_merge=on,index_merge_union=on, index_merge_sort_union=on,index_merge_intersection=on, engine_condition_pushdown=on,index_condition_pushdown=on, mrr=on,mrr_cost_based=on, block_nested_loop=on,batched_key_access=off, materialization=on,semijoin=on,loosescan=on,firstmatch=on, duplicateweedout=on,subquery_materialization_cost_based=on, use_index_extensions=on,condition_fanout_filter=on,derived_merge=on 1 row in set (0,00 sec) Optimizer Switches 51
  • 88. • Turn ON and OFF particular optimization Optimizer Switches 51
  • 89. • Turn ON and OFF particular optimization • Can be not helpful • Especially for queries, tuned for previous versions Optimizer Switches 51
  • 90. • Turn ON and OFF particular optimization • Can be not helpful • Work with them as with any other option • Turn OFF and try SET optimizer_switch = ’use_index_extensions=off’; SELECT ... EXPLAIN SELECT ... Optimizer Switches 51
  • 91. • Turn ON and OFF particular optimization • Can be not helpful • Work with them as with any other option • If helps implement in queries SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 ...; SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE ...; Optimizer Switches 51
  • 92. • Optimizer choses wrong index for no reason Bugs in Optimizer 52
  • 93. • Optimizer choses wrong index for no reason • Statistics is up to date • Histograms are not usable Bugs in Optimizer 52
  • 94. • Optimizer choses wrong index for no reason • Statistics is up to date • Histograms are not usable • Solution • Use index hints FORCE INDEX IGNORE INDEX Bugs in Optimizer 52
  • 95. • Optimizer choses wrong index for no reason • Statistics is up to date • Histograms are not usable • Solution • On every upgrade • Remove index hints • Test if query improved • You must do it even for minor version upgrades! Bugs in Optimizer 52
  • 96. • EXPLAIN is essential for query tuning • Real job is done by storage engine • Index statistics affect query execution plan • All index hints, optimizer hints and other workarounds must be validated on each upgrade Summary 53
  • 97. EXPLAIN Syntax EXPLAIN FORMAT=JSON is Cool! series Troubleshooting Performance add-ons Optimizer Satistics aka Histograms Optimizer Hints Tracing the Optimizer More information 54
  • 98. Alexander Rubin for combined and covered index examples Special thanks 55
  • 100. Percona’s open source database experts are true superheroes, improving database performance for customers across the globe. Percona’s open source database experts are true superheroes, improving database performance for customers across the globe. Discover what it means to have a Percona career with the smartest people in the database performance industries, solving the most challenging problems our customers come across. We’re Hiring! 57