Professional Documents
Culture Documents
PgDay 2017 Innodb Architecture Performance Optimization
PgDay 2017 Innodb Architecture Performance Optimization
Performance Optimization
MySQL 5.7 Edition
Peter Zaitsev
PgDay Russia 2017, Saint Petersburg | July 5th, 2017
Percona Live Europe Call for Papers &
Registration are Open!
Championing Open Source Databases
▪ MySQL, MongoDB, Open Source Databases
▪ Time Series Databases, PostgreSQL, RocksDB
▪ Developers, Business/Case Studies, Operations
▪ September 25-27th, 2017
▪ Radisson Blu Royal Hotel, Dublin, Ireland
Advanced Performance
Optimization Needs
Architecture Knowledge
3
Right Level
4
4
Wonder where Graphs are from ?
6
What Architecture ?
7
7
Innodb Versions Covered
Improvements
MySQL 5.7 as Changes in
in XtraDB /
Baseline MariaDB
Percona Server
8
8
Innodb Basics
•B+Tree Based
“Traditional” •ACID Transactions
Storage
Engine •MVCC
•OLTP Optimized
9
9
Data Structures
10
Main “Objects”
Log Undo
Undo Slot
Record Space
11
Innodb Main Files
Tablespace Files
Log Files
12
Tablespace
System Tablespace
Per-Table Tablespace
14
14
Tablespace: Physical Structure
15
15
Table
Stored In Tablespace
Consists of Indexes
18
18
File IO during INSERT
19
Row Operations During Insert
20
20
Physical and Logical Structure
22
22
Performance Considerations
Flushed Periodically
25
25
Performance Considerations
innodb_log_write_ahead_size
• Avoid ‘read-around-write’ for large log
files not in cache
27
27
Mind innodb_io_capacity
28
How Durable Transactions do you Need ?
29
29
Mind Group Committ
30
Double write Buffer
32
32
Parallel Doublewrite Performance
33
33
Undo Space
34
34
Undo Space Structure
Requires
Undo
Tablespace • innodb_undo_tablespaces=2
to be In • innodb_undo_log_truncate=1
Separate
Files
38
38
Purge Lag Prevention
Can help
to prevent
• innodb_max_purge_lag=10000000
run away • innodb_max_purge_lag_delay=10000
undo
space
39
39
Transaction History
40
40
Purge Progress
41
41
Purge Delay Management
42
42
Structures in Memory
43
Most Important Data Structures
44
44
Buffer Pool
46
Will be mostly used to store data pages
47
Watch Data vs Dirty Data
48
Buffer Pool Configuration
49
49
Online Resizing has Performance Impact
50
50
Buffer Pool For Caching
51
51
Young and Old
Beware of Swapping
• Better few percent too little than few percent too much
55
IO Analyses Use Case
56
Good Idea to look at IO Usage Per Query
57
Additional Memory Pool
60
60
Performance Considerations
Can be disabled
innodb_change_buffer_max_size
61
61
Insert/Change Buffer Size
62
62
Insert Buffer Performance
63
63
Adaptive Hash Index
65
Performance Considerations
Disable • Innodb_adaptive_hash_index=0
67
67
AHI Maintenance
68
68
Log Buffer
Innodb_log_buffer_size
innodb_flush_log_at_timeout
70
70
Double Write Buffer
Is it on disk or in memory ?
Both!
PS 5.7 - Improved
71
71
Lock Structures
No Lock Escalation
For each page having lock small bitmap allocated indicating locked rows
72
72
Data Dictionary Cache
Information
• Structure; Index Statistics etc.
about open • Tied to table_definition_cache_size
tables
Was not cache
• All accessed tables were always kept
before MySQL in memory
5.6
73
73
Operations Details
74
Transaction Control
75
75
Basic Operation
01010
Log Files
UPDATE City SET
name = 'Morgansville'
WHERE name = 'Brisbane'
AND CountryCode='AUS'
76
76
Database Operations
Reads
Writes
AUTOCOMMIT=1 by default
79
79
Performance Considerations
80
80
Better performance with READ-
UNCOMMITTED
• Running Long Select while running SysBench updates
81
Pessimistic Locking
83
Innodb Reads
87
87
Innodb DDL
88
88
Long Write Transactions
Large amount of versions for the single row can be the problem
90
90
MVCC Details
93
93
MVCC and Indexes
Key Value “5” will point to all rows which have key value 5 now or had it in
the past
94
94
MVCC Garbage Collection
Transaction Commits
Purge Threads
95
95
Performance Considerations
Monitor Innodb_history_list_length
96
96
Locking Basics
97
97
Types of Locks
Row Locks
Index Locks
Gap Locks
“SIX” – Set on the table which has some words being updated
99
99
Locking Performance
10
0
100
Foreign Keys
Immediate
Non-Recursive (5.6+)
innodb_lock_wait_timeout
Applies to Row Locks only (Meta Data Locks have their own Timeout)
10
3
103
Performance Considerations
Lock Less
10
7
107
Innodb Latches
11
0
110
InnoDB Latching in SHOW INNODB
STATUS
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 13569
OS WAIT ARRAY INFO: signal count 11421
--Thread 1152170336 has waited at ./../include/buf0buf.ic line 630 for 0.00 seconds the semaphore:
Mutex at 0x2a957858b8 created file buf0buf.c line 517, lock var 0
--Thread 1147709792 has waited at ./../include/buf0buf.ic line 630 for 0.00 seconds the semaphore:
Mutex at 0x2a957858b8 created file buf0buf.c line 517, lock var 0
Mutex spin waits 5018, rounds 70800, OS waits 2033
RW-shared spins 2736, rounds 84289, OS waits 2791
RW-excl spins 904, rounds 111941, OS waits 3607
Spin rounds per wait: 14.11 mutex, 30.81 RW-shared, 123.83 RW-excl
11
1
111
Some Lock Statistics in INNODB
METRICS
• RW Locks Only; Not Mutexes
• Sysbench 1,8,64 and 512 connections
11
2
OS Waits
11
3
Index Tree rw_lock contention
11
4
Adaptive Hash Index
11
5
Performance Considerations
innodb_spin_wait_delay
• Balance wasting CPU vs. cost of
context switch
Innodb_thread_concurrency
• Limit amount of threads in Innodb
Kernel
Thread Pool
• MySQL Enterprise, Percona Server,
MariaDB
11
6
116
Innodb_thread_concurrency still has its
place
11
7
117
Bonus Material
(If we have time to get to it)
Background Operations
11
Why Important
12
1
121
Checkpointing Basics
12
5
125
Main Question
12
7
127
Improvements in PS 5.6 vs MySQL 5.6
12
8
128
LRU Flushing Basics
innodb_max_dirty_pages_pct
innodb_flush_neighbors
innodb_lru_scan_depth
innodb_io_capacity
innodb_io_capacity_max
13
2
132
Purging
Keeping up with
Garbage coming from
MVCC
13
3
133
Purging Does
One or
More • Innodb_purge_threads
purge
threads
13
5
135
May not be able to keep up
13
7
137
Change Buffer
Innodb_io_capacity
innodb_change_buffer_max_size
13
9
139
Read Ahead
14
0
140
Types of Read-Ahead
Sequential Read-Ahead
Random Read-Ahead
Logical Read-Ahead
14
1
141
Sequential Read Ahead
Fetch the next Segment if a lot of pages are accessed from current one
Disabled by Default
innodb_random_read_ahead
14
3
143
Logical Read Ahead
Available in WebScaleSQL
• innodb_read_ahead_threshold
Configuration • innodb_random_read_ahead
• Innodb_buffer_pool_read_ahead
Status • Innodb_buffer_pool_read_ahead_evicted
• Innodb_buffer_pool_read_ahead_rnd
14
5
145
Advanced Features
14
Encryption
14
7
147
Encryption in MySQL
Only Innodb
Supports Key
5.7.11+ Tablespace at
Rotation
this point
General Log, Audit Log, Slow Log, Galera cache are still not encrypted
14
9
149
What's New in MySQL 5.7 Innodb
15
0
150
Improved to Flushing
15
2
152
Improved Locking
Automatically disable
DoubleWrite Buffer if
NVMFS is Detected
15
8
158
Transportable Partitioned Tablespaces
16
0
160
Faster Crash Recovery
ALTER TABLE t1
COMMENT='MERGE_THRESHOLD=40';
16
6
166
Tuning Innodb
Most important
configuration settings
16
7
167
Do not obsess with tuning
innodb_buffer_pool_size
innodb_buffer_pool_instances
innodb_log_file_size
innodb_log_buffer_size
innodb_flush_log_at_trx_commit
16
9
169
Tuning Innodb
innodb_flush_method
innodb_io_capacity
innodb_io_capacity_max
innodb_checksum_algorithm
innodb_adaptive_hash_index
17
0
170
Tuning Innodb
innodb_purge_threads
innodb_flush_neighbors
innodb_change_buffer_max_size=
innodb_flush_log_at_trx_commit
innodb_stats_on_metadata
17
1
171
Tuning Innodb
innodb_max_dirty_pages_pct
innodb_sync_array_size
innodb_max_purge_lag
innodb_max_purge_lag_delay
17
2
172
Tuning Innodb
innodb_file_per_table
Innodb_thread_concurrency
innodb_file_format
Innodb_page_size
innodb_spin_wait_delay
17
3
173
Further Reading
http://www.percona.com/blog
http://dimitrik.free.fr/blog
http://mysqlserverteam.com
http://blog.jcole.us/
http://www.planetmysql.org
17
4
174
Thank You!
pz@percona.com
https://www.linkedin.com/in/peterzaitsev
https://twitter.com/peterzaitsev
17
5
175