SlideShare a Scribd company logo
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
6 Tips to MySQL Performance Tuning
Nitin Mehta
Principal Technical Consultant – MySQL
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
2
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Agenda
3
Oracle Premier Support for MySQL
MySQL Performance Tuning Best Practices
Top 6 Tips
Summary
Q & A
1
2
3
4
5
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Oracle Premier Support for MySQL
We’ve Got You Covered
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Enterprise Support
• Provided by the experts, straight from the source
• Backed by the MySQL developers
• World-class support, in 27 languages
• Hot fixes & maintenance releases
• 24x7x365
• Unlimited incidents
• Consultative support
• Global scale and reach
Get immediate help for any MySQL
issue, plus expert advice
5
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Consultative Support
• Troubleshooting using Webex
• Replication review
• Partitioning review
• Schema review
• Query review
• Performance tuning
• ...and more
6
Make the Most of your Deployments
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
7
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
• Be wary about “best practices”
– No two systems are the same
– What was true earlier may no longer hold
8
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
• Be wary about “best practices”
– No two systems are the same
– What was true earlier may no longer hold
• HW and SW vendors are all lying! Not really.
– Test, Benchmark, Monitor
– One knob at a time
– Use sysbench, mysqlslap, monitoring tools
9
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
• Be wary about “best practices”
– No two systems are the same
– What was true earlier may no longer hold
• HW and SW vendors are all lying! Not really.
– Test, Benchmark, Monitor
– One knob at a time
– Use sysbench, mysqlslap, monitoring tools
• Think – what you’re doing and why?
10
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
• Be wary about “best practices”
– No two systems are the same
– What was true earlier may no longer hold
• HW and SW vendors are all lying! Not really.
– Test, Benchmark, Monitor
– One knob at a time
– Use sysbench, mysqlslap, monitoring tools
• Think – what you’re doing and why?
• There are guidelines, not best configurations
11
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL Performance Tuning Best Practices
• Be mindful of your requirements
– Some options give the choice between performance or data safety – what do you
need?
• Often the default value is the best value
• Ensure all tables have a PRIMARY KEY
• InnoDB organizes the data according to the PRIMARY KEY:
– The PRIMARY KEY is included in all secondary indexes in order to be able to locate the
actual row => smaller PRIMARY KEY gives smaller secondary indexes.
– A mostly sequential PRIMARY KEY is in general recommended to avoid inserting rows
between existing rows.
12
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Top 6 Tips
Tuning MySQL Performance
13
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
01. Think
• Thinking is your best defense
• Analyze your performance problem:
– Make sure you understand what the issue is:
• No: performance is too slow
• Yes:
– The query takes 10 seconds, but it is used for interactive use so must complete in 0.1 second or less
– The server must handle 200k queries per second
– Determine the cause
• Don’t jump to conclusions
• Consider the whole stack
• Justify why you think you have found the cause
14
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
01. Think
• Implement the solution
– List all the solutions you can think of
• Make sure you think outside the box – not just what you assume
– Explain why the solutions will work
– Implement an action plan:
• Test the action plan and update if necessary
• Ensures you implement the solution the same on the test and production systems
• If a regression occurs, you have a record of the steps you made
15
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
• Gives you a base line
• Tells you what has happened – useful to investigate performance issues
• Allows you to proactively handle potential issues
16
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
• Many options available:
– MySQL Enterprise Monitor
– MySQL Workbench
– Slow Query Log
– MySQL Plugin for Oracle Enterprise Monitor
– And many more
• Make sure you configure alerts so you react appropriately to all events
based on the severity level!
17
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
• On Linux perf is a great tool for live monitoring and can record over a time
period
– https://perf.wiki.kernel.org/index.php/Main_Page
– http://www.brendangregg.com/perf.html
• MySQL Enterprise Monitor has some reports to get snapshot data
• Workbench has performance reports based on the sys schema
“Real time” monitoring
18
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
MySQL Workbench Performance Reports
19
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
• The sys schema is a collection of views, functions, and procedures aiming
to make the Information Schema and Performance Schema simpler to use.
• Included by default in MySQL 5.7+
• Also available from https://github.com/mysql/mysql-sys for MySQL 5.6
• Formerly known as ps_helper by Mark Leith
• https://dev.mysql.com/doc/refman/5.7/en/sys-schema.html
What was that sys schema?
20
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
Finding queries to optimize - MySQL Enterprise Monitor Query Analyzer
21
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
02. Monitoring
Finding queries to optimize – Performance Schema Digest Summary
mysql> SELECT LEFT(DIGEST_TEXT, 64) AS DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT, MAX_TIMER_WAIT
FROM performance_schema.events_statements_summary_by_digest
ORDER BY MAX_TIMER_WAIT DESC
LIMIT 10;
+------------------------------------------------------------------+------------+-----------------+----------------+
| DIGEST_TEXT | COUNT_STAR | SUM_TIMER_WAIT | MAX_TIMER_WAIT |
+------------------------------------------------------------------+------------+-----------------+----------------+
| INSERT INTO `salaries` VALUES (...) /* , ... */ | 342 | 159811231808000 | 4156961573000 |
| INSERT INTO `dept_emp` VALUES (...) /* , ... */ | 42 | 31561264335000 | 2458392698000 |
| INSERT INTO `titles` VALUES (...) /* , ... */ | 63 | 35738435708000 | 1735350241000 |
| INSERT INTO `employees` VALUES (...) /* , ... */ | 51 | 18004605187000 | 1679817477000 |
| INSERT INTO `sbtest` ( `k` , `c` , `pad` ) VALUES (...) /* , ... | 10 | 5241286782000 | 1247361451000 |
| COMMIT | 342 | 31984662051000 | 992714081000 |
| DROP SCHEMA IF EXISTS `employees` | 6 | 1252459420000 | 848771265000 |
| CREATE TABLE `sbtest` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCR | 1 | 565468324000 | 565468324000 |
| CREATE TABLE `dept_manager` ( `dept_no` CHARACTER (?) NOT NULL , | 3 | 355874700000 | 220491035000 |
| SELECT COUNT (?) AS `cnt` , `round` ( ( `performance_schema` . ` | 6 | 386062170000 | 217206520000 |
+------------------------------------------------------------------+------------+-----------------+----------------+
10 rows in set (0.00 sec)
22
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
1. Start with an empty configuration file
2. Set paths, port, etc.
3. Enable additional monitoring
4. Set capacity settings
5. Don’t do much more!
23
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• It can be an advantage to have the paths pointing to separate disk systems
– Reduces contention
– I/O often becomes a bottleneck
– Can place the hot files on faster disks
• For example with innodb_flush_log_at_trx_commit = 1 and high transaction commit rate, it can be
necessary to place the InnoDB redo log on SSDs to support the high rate of flushes.
• Note: File-per-table tablespaces and general tablespaces can belocated
outside datadir when they are created
Paths
24
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• If you use InnoDB, enable all the INNODB_METRICS counters:
– innodb_monitor_enable = ‘%’
• The overhead has turned out to be small so worth the extra details
• Ensure the Performance Schema is enabled
– Does have overhead, but provides very useful information for performance tuning
– Consider enabling additional consumers and instruments as required (for example for
transactions in MySQL 5.7)
– The Performance Schema can also be configured dynamically at runtime
Enable additional monitoring
25
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• MySQL Reference Manual (about innodb_buffer_pool_size):
“On a dedicated database server, you might set the buffer pool size to 80%
of the machine's physical memory size.”
– What if you have 1TB of memory? Do you still want to reserve 20% for other uses?
• Instead:
– How much memory does the host have?
– Subtract memory required by OS and other processes
– Subtract memory required by MySQL other then the InnoDB buffer pool
– Choose minimum of this and the size of the “working data set”
Capacity settings - innodb_buffer_pool_size
26
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• Total redo log size defined by two options:
– innodb_log_file_size
– innodb_log_files_in_group
• Total size = innodb_log_file_size * innodb_log_files_in_group
– Max supported total redo log size:
• MySQL 5.5 and earlier: just below 4G
• MySQL 5.6 and later: just below 512G
• Should be large enough to avoid “excessive” checkpointing
• The larger redo log, the slower shutdowns potentially
Capacity settings – InnoDB redo log
27
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• Get current Log sequence number (LSN) and last checkpoint:
– SHOW ENGINE INNODB STATUS:
– INNODB_METRICS:
Capacity settings – InnoDB redo log – Is it large enough?
---
LOG
---
Log sequence number 602763740
Log flushed up to 602763740
Pages flushed up to 584668961
Last checkpoint at 555157885
mysql> SELECT NAME, COUNT
FROM information_schema.INNODB_METRICS
WHERE NAME IN ('log_lsn_current', 'log_lsn_last_checkpoint');
+-------------------------+-----------+
| NAME | COUNT |
+-------------------------+-----------+
| log_lsn_last_checkpoint | 555157885 |
| log_lsn_current | 602763740 |
+-------------------------+-----------+
28
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• Calculate the amount of used redo log:
– Used log = log_lsn_current – log_lsn_last_checkpoint
= 602763740 - 555157885
= 47605855 (bytes)
• Compare to total size:
– Used % = (Used log / Total log) * 100
= (47605855 / (innodb_log_file_size * innodb_log_files_in_group)) * 100
= (47605855 / 100663296) * 100
= 47.29 %
Capacity settings – InnoDB redo log – Is it large enough?
29
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• If the usage reaches 75% an asynchronous flush is triggered
– I/O can be so intensive that all other work stalls
– Main InnoDB thread may be in the state: flushing buffer pool pages
• Ensure you have enough head room to handle peak loads
– For example aim at using at most 60% or 70% of the redo log
• Important that your monitoring solution monitors the redo log usage
• Improvements to the flushing algorithm and new options to control the I/O
have been added in newer MySQL versions
Capacity settings – InnoDB redo log – Is it large enough?
30
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• innodb_undo_tablespaces can only be set before initializing the datadir!
• If set creates undo log tablespaces outside the system tablespace (ibdata1)
• Advantages:
– Keeps the system tablespace smaller
– Can keep the undo log on faster disks
– In MySQL 5.7 the undo log tablespaces can be truncated
• Each undo tablespace file is 10M initially
• Maximum of 95 undo tablespaces in 5.7
InnoDB Undo Log
31
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
03. Initial Configuration File
• max_connections – be careful setting this too large as each connection
requires memory
• table_definition_cache – ensure all tables can be in the cache. If you expect
4000 tables, set table_definition_cache > 4000
• table_open_cache – each table can be open more than once
• table_open_cache_instances - Typically a good value is 16
Other capacity options
32
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• Surely enabling caches is a good thing?
• Surely the larger caches and buffers the better?
Several buffers and caches available
33
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• Surely enabling caches is a good thing?
• Surely the larger caches and buffers the better?
• No!
Several buffers and caches available
34
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• It is rarely good to enable the Query Cache
– Guarded by a single mutex
• Most workloads is best left with the Query Cache disabled (the default):
– query_cache_type = 0
• If you think your workload benefits from the Query Cache, test it
– The more writes, the less benefit
– The more data fitting into the buffer pool, the less benefit
– The more complex queries and the larger scans, the more benefit
• Often other caching solutions are a better option.
Query Cache
35
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• In MySQL 5.5 and earlier: only used for plain index scans, range index
scans, and joins that do not use indexes
– No reason to have it larger than the size of each matching row
• In MySQL 5.6+ also used for Batched Key Access (BKA)
– Queries using BKA can benefit from a larger join buffer
• Is the minimum size allocated!
• Usually a small global (default for new connections) value is best: 32k-256k
• Increase for a session as needed
join_buffer_size
36
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• Like for join_buffer_size a small global value is usually best: 32k-256k
• Can monitor using the global status variable Sort_merge_passes:
• Aim at a few Sort_merge_passes per second on a busy server
• Increase for a session as needed
sort_buffer_size
mysql> SHOW GLOBAL STATUS LIKE 'Sort_merge_passes';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Sort_merge_passes | 0 |
+-------------------+-------+
1 row in set (0.00 sec)
37
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
04. Buffers And Caches
• Some buffers are allocated in full each time they are used
• One query may use several buffers, so large buffers can cause significant
memory usage
• Memory allocations are relatively expensive
• For example Linux glibc malloc changes memory allocation algorithm when
crossing threshold (typical 256k or 512k).
– Algorithm for larger allocations can be 40 times slower than for smaller allocation
Why is it important with small global values?
38
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
05. Data Consistency Versus Performance
• Possible values in order of data safety:
– 1: is theoretically the slowest, but with fast SSD it may be around as fast as 2 and 0
– 2: flushed every second (innodb_flush_log_at_timeout in 5.6+)
• Transactions may be lost if the OS crashes
– 0: MySQL never fsyncs
• Transactions may be lost if the OS crashes
• Defaults to 1 – flush the redo log after each commit
– Required for D in ACID
– For this reason it is the recommended value
innodb_flush_log_at_trx_commit
39
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
05. Data Consistency Versus Performance
• What if innodb_flush_log_at_trx_commit = 1 is too slow and you want the
D in ACID?
– Make sure the redo logs are on separate disks
• Do not be tempted to have the individual redo logs on different disks
– Consider SSD particularly if you have a high commit rate
– Battery-backed disk cache also makes flushes cheaper
innodb_flush_log_at_trx_commit
40
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
05. Data Consistency Versus Performance
• Specifies how many transactions between each flush of the binary log
– 0: when rotating the binary log or when the OS decides
– 1: at every transaction commit – this is the safest
– N: every N commits
• Default value:
– MySQL 5.6 and earlier: 0
– MySQL 5.7 and later: 1
• MySQL 5.6 and later support group commit for InnoDB giving less overhead
of sync_binlog = 1
sync_binlog
41
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
05. Data Consistency Versus Performance
• sync_binlog != 1 means that slaves most likely will have to be rebuild if the
master crashes
• But sync_binlog = 0 must be the best performance wise, right?
sync_binlog
42
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
05. Data Consistency Versus Performance
• sync_binlog != 1 means that slaves most likely will have to be rebuild if the
master crashes
• But sync_binlog = 0 must be the best performance wise, right?
– By default: max_binlog_size = 1G
– 1G is not a lot of memory nowadays
– So the OS may buffer the entire binary log
– When rotating the binary log, up to 1G will be flushed to disk
• Stalls all other commits until the binary log rotation is done
• So in short: sync_binlog = 0 may give the best throughput, but sync_binlog
= 1 may give the most predictable performance
sync_binlog
43
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• The issue may occur at any place in the stack, e.g.:
– Application
– Application host/hardware
– Network between application and host with MySQL
– MySQL host/hardware
– MySQL
• This should also be considered when monitoring
• Consider the OS/hardware settings
44
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• Make sure you have enough RAM
– The active data should fit in the buffer pool
– MySQL connections and caches take memory
– Extra RAM for
• FS cache
• Monitoring
• RAM disk (tmpfs)
Hardware considerations
45
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• Fast CPU is required for single threaded performance
• Recent servers have 32 to 80 cores.
• Enable hyper-threading
• MySQL now scales beyond 72 cores in 5.7
• Same core count in fewer sockets is better
• Faster cores better than more but slower cores
Hardware considerations
46
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• Fast and reliable storage is good for IO bound loads
• HDD for sequential reads and writes
• Bus-attached SSD for random reads and writes
• Big sata or other disk for log files
• Several disks!
• Life time should be considered
Hardware considerations - Storage
47
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• L of LAMP
• Good on Solaris
• Oracle invests on Windows
• For pure performance, favor Linux
Operating System
48
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• I/O Scheduler
– Several Linux distributions use the CFQ scheduler by default
• OK for reads
• Serializes writes!
– NOOP and deadline are usually better for MySQL workloads
• Deadline is the default I/O scheduler for Oracle Linux
I/O settings on the operating system
49
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• I/O Scheduler
– Check the current scheduler:
– Update the scheduler dynamically:
– To set at boot time, use the “elevator=deadline” boot option.
I/O settings on the operating system
shell# cat /sys/block/sda/queue/scheduler
noop deadline [cfq]
shell# echo deadline > /sys/block/sda/queue/scheduler
shell# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq
50
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• Linux glibc malloc can be a performance bottleneck
• Better to use alternative malloc library:
– tcmalloc
– jemalloc
Memory allocation library
51
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
06. Consider the Whole Stack
• Set alternative malloc library:
– mysqld_safe
– systemd distributions:
• Set LD_PRELOAD in /etc/sysconfig/mysql
Memory allocation library
[mysqld_safe]
malloc-lib = /usr/lib64/libjemalloc.so.1
52
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Summary
Wrapping it all up
53
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Summary
• MySQL performance tuning is similar to all other performance tuning:
– Premature optimization is bad
– Only change one thing at a time and don’t make too large changes
– Don’t micro manage
– One size does not fit all
– Base your decisions on measurements
– Understand what an option does before changing it
– Understand your system (data)
– Understand what you need
– Consider the whole stack
54
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Thank You
Q&A
55
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 56
6 Tips to MySQL Performance Tuning

More Related Content

PPTX
Robust easy affordable disaster recovery for MySQL Data
OracleMySQL
 
PPTX
MySQL Performance Tuning 101 (Bahasa)
OracleMySQL
 
PPT
MySQL in Oracle environment : Quick start guide for Oracle DBA (Part 1)
OracleMySQL
 
PPTX
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
PPTX
MySQL High Availibility Solutions
Mark Swarbrick
 
PDF
MySQL Manchester TT - 5.7 Whats new
Mark Swarbrick
 
PPTX
State ofdolphin short
Mandy Ang
 
PDF
MySQL 5.7 Replication News
Ted Wennmark
 
Robust easy affordable disaster recovery for MySQL Data
OracleMySQL
 
MySQL Performance Tuning 101 (Bahasa)
OracleMySQL
 
MySQL in Oracle environment : Quick start guide for Oracle DBA (Part 1)
OracleMySQL
 
MySQL in oracle_environments(Part 2): MySQL Enterprise Monitor & Oracle Enter...
OracleMySQL
 
MySQL High Availibility Solutions
Mark Swarbrick
 
MySQL Manchester TT - 5.7 Whats new
Mark Swarbrick
 
State ofdolphin short
Mandy Ang
 
MySQL 5.7 Replication News
Ted Wennmark
 

What's hot (20)

PDF
MySQL Cloud Service Deep Dive
Morgan Tocker
 
PDF
MySQL Enterprise Monitor
Ted Wennmark
 
ODP
MySQL Enterprise Portfolio
Abel Flórez
 
PDF
MySQL Manchester TT - Security
Mark Swarbrick
 
PDF
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
Geir Høydalsvik
 
PDF
Oracle Enterprise Manager for MySQL
Mario Beck
 
PDF
MySQL: From Single Instance to Big Data
Morgan Tocker
 
PDF
MySQL Security
Ted Wennmark
 
PDF
MySQL Manchester TT - Performance Tuning
Mark Swarbrick
 
PDF
MySQL 5.7 - What's new, How to upgrade and Document Store
Abel Flórez
 
PDF
MySQL Enterprise Edition Overview
Mario Beck
 
PDF
MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...
Olivier DASINI
 
PDF
MySQL 5.7: What's New, Nov. 2015
Mario Beck
 
PDF
MySQL Document Store
Mario Beck
 
PDF
Efficient Performance Analysis and Tuning with MySQL Enterprise Monitor
Mark Matthews
 
PDF
Why MySQL High Availability Matters
Matt Lord
 
PDF
Introduction to MySQL
Ted Wennmark
 
PDF
MySQL Enterprise Backup
Mario Beck
 
PDF
Netherlands Tech Tour - 06 MySQL Enterprise Monitor
Mark Swarbrick
 
PDF
MySQL for Software-as-a-Service (SaaS)
Mario Beck
 
MySQL Cloud Service Deep Dive
Morgan Tocker
 
MySQL Enterprise Monitor
Ted Wennmark
 
MySQL Enterprise Portfolio
Abel Flórez
 
MySQL Manchester TT - Security
Mark Swarbrick
 
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
Geir Høydalsvik
 
Oracle Enterprise Manager for MySQL
Mario Beck
 
MySQL: From Single Instance to Big Data
Morgan Tocker
 
MySQL Security
Ted Wennmark
 
MySQL Manchester TT - Performance Tuning
Mark Swarbrick
 
MySQL 5.7 - What's new, How to upgrade and Document Store
Abel Flórez
 
MySQL Enterprise Edition Overview
Mario Beck
 
MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...
Olivier DASINI
 
MySQL 5.7: What's New, Nov. 2015
Mario Beck
 
MySQL Document Store
Mario Beck
 
Efficient Performance Analysis and Tuning with MySQL Enterprise Monitor
Mark Matthews
 
Why MySQL High Availability Matters
Matt Lord
 
Introduction to MySQL
Ted Wennmark
 
MySQL Enterprise Backup
Mario Beck
 
Netherlands Tech Tour - 06 MySQL Enterprise Monitor
Mark Swarbrick
 
MySQL for Software-as-a-Service (SaaS)
Mario Beck
 
Ad

Similar to 6 Tips to MySQL Performance Tuning (20)

PDF
MySQL Performance Best Practices
Olivier DASINI
 
PPTX
MySQL Tech Tour 2015 - Manage & Tune
Mark Swarbrick
 
PDF
MySQL 5.6 Performance
MYXPLAIN
 
PDF
01 demystifying mysq-lfororacledbaanddeveloperv1
Ivan Ma
 
PDF
MySQL Enterprise Monitor
Mark Swarbrick
 
PDF
MySQL Enterprise Monitor
Mario Beck
 
PDF
MySQL Webinar Series 4/4 - Manage & tune
Mark Swarbrick
 
PPT
MySQL Performance Tuning at COSCUP 2014
Ryusuke Kajiyama
 
DOCX
MYSQL_Basic_Performance_Tuning_Guidelines_-_V2
Shelton Reese
 
PDF
20190615 hkos-mysql-troubleshootingandperformancev2
Ivan Ma
 
PDF
20150110 my sql-performanceschema
Ivan Ma
 
PPTX
My sql performance tuning course
Alberto Centanni
 
PDF
50-Tips-for-Boosting-MySQL-Performance-CON2655.pdf
AsparuhPolyovski2
 
PDF
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
Tommy Lee
 
PDF
How to analyze and tune sql queries for better performance vts2016
oysteing
 
PDF
The Peoper Care and Feeding of a MySQL Server for Busy Linux Admin
Dave Stokes
 
PDF
Using MySQL Enterprise Monitor for Continuous Performance Improvement
Mark Matthews
 
PDF
MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...
Mark Swarbrick
 
PDF
How to analyze and tune sql queries for better performance
oysteing
 
ODP
Scalablity and benchmark in mysql performance
Amrendra Kumar
 
MySQL Performance Best Practices
Olivier DASINI
 
MySQL Tech Tour 2015 - Manage & Tune
Mark Swarbrick
 
MySQL 5.6 Performance
MYXPLAIN
 
01 demystifying mysq-lfororacledbaanddeveloperv1
Ivan Ma
 
MySQL Enterprise Monitor
Mark Swarbrick
 
MySQL Enterprise Monitor
Mario Beck
 
MySQL Webinar Series 4/4 - Manage & tune
Mark Swarbrick
 
MySQL Performance Tuning at COSCUP 2014
Ryusuke Kajiyama
 
MYSQL_Basic_Performance_Tuning_Guidelines_-_V2
Shelton Reese
 
20190615 hkos-mysql-troubleshootingandperformancev2
Ivan Ma
 
20150110 my sql-performanceschema
Ivan Ma
 
My sql performance tuning course
Alberto Centanni
 
50-Tips-for-Boosting-MySQL-Performance-CON2655.pdf
AsparuhPolyovski2
 
제3회난공불락 오픈소스 인프라세미나 - MySQL Performance
Tommy Lee
 
How to analyze and tune sql queries for better performance vts2016
oysteing
 
The Peoper Care and Feeding of a MySQL Server for Busy Linux Admin
Dave Stokes
 
Using MySQL Enterprise Monitor for Continuous Performance Improvement
Mark Matthews
 
MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...
Mark Swarbrick
 
How to analyze and tune sql queries for better performance
oysteing
 
Scalablity and benchmark in mysql performance
Amrendra Kumar
 
Ad

More from OracleMySQL (7)

PDF
MySQL Performance Tuning (In Korean)
OracleMySQL
 
PDF
Solving Performance Problems Using MySQL Enterprise Monitor
OracleMySQL
 
PDF
MySQL partitioning
OracleMySQL
 
PDF
What's New MySQL 8.0?
OracleMySQL
 
PPTX
MySQL 8.0 in a nutshell
OracleMySQL
 
PDF
Infographic oracle-my sql-cloud
OracleMySQL
 
PPTX
MySQL in oracle_public_cloud
OracleMySQL
 
MySQL Performance Tuning (In Korean)
OracleMySQL
 
Solving Performance Problems Using MySQL Enterprise Monitor
OracleMySQL
 
MySQL partitioning
OracleMySQL
 
What's New MySQL 8.0?
OracleMySQL
 
MySQL 8.0 in a nutshell
OracleMySQL
 
Infographic oracle-my sql-cloud
OracleMySQL
 
MySQL in oracle_public_cloud
OracleMySQL
 

Recently uploaded (20)

PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
The Future of Artificial Intelligence (AI)
Mukul
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 

6 Tips to MySQL Performance Tuning

  • 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 6 Tips to MySQL Performance Tuning Nitin Mehta Principal Technical Consultant – MySQL
  • 2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  • 3. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Agenda 3 Oracle Premier Support for MySQL MySQL Performance Tuning Best Practices Top 6 Tips Summary Q & A 1 2 3 4 5
  • 4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Oracle Premier Support for MySQL We’ve Got You Covered
  • 5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Enterprise Support • Provided by the experts, straight from the source • Backed by the MySQL developers • World-class support, in 27 languages • Hot fixes & maintenance releases • 24x7x365 • Unlimited incidents • Consultative support • Global scale and reach Get immediate help for any MySQL issue, plus expert advice 5
  • 6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Consultative Support • Troubleshooting using Webex • Replication review • Partitioning review • Schema review • Query review • Performance tuning • ...and more 6 Make the Most of your Deployments
  • 7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices 7
  • 8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices • Be wary about “best practices” – No two systems are the same – What was true earlier may no longer hold 8
  • 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices • Be wary about “best practices” – No two systems are the same – What was true earlier may no longer hold • HW and SW vendors are all lying! Not really. – Test, Benchmark, Monitor – One knob at a time – Use sysbench, mysqlslap, monitoring tools 9
  • 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices • Be wary about “best practices” – No two systems are the same – What was true earlier may no longer hold • HW and SW vendors are all lying! Not really. – Test, Benchmark, Monitor – One knob at a time – Use sysbench, mysqlslap, monitoring tools • Think – what you’re doing and why? 10
  • 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices • Be wary about “best practices” – No two systems are the same – What was true earlier may no longer hold • HW and SW vendors are all lying! Not really. – Test, Benchmark, Monitor – One knob at a time – Use sysbench, mysqlslap, monitoring tools • Think – what you’re doing and why? • There are guidelines, not best configurations 11
  • 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | MySQL Performance Tuning Best Practices • Be mindful of your requirements – Some options give the choice between performance or data safety – what do you need? • Often the default value is the best value • Ensure all tables have a PRIMARY KEY • InnoDB organizes the data according to the PRIMARY KEY: – The PRIMARY KEY is included in all secondary indexes in order to be able to locate the actual row => smaller PRIMARY KEY gives smaller secondary indexes. – A mostly sequential PRIMARY KEY is in general recommended to avoid inserting rows between existing rows. 12
  • 13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Top 6 Tips Tuning MySQL Performance 13
  • 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 01. Think • Thinking is your best defense • Analyze your performance problem: – Make sure you understand what the issue is: • No: performance is too slow • Yes: – The query takes 10 seconds, but it is used for interactive use so must complete in 0.1 second or less – The server must handle 200k queries per second – Determine the cause • Don’t jump to conclusions • Consider the whole stack • Justify why you think you have found the cause 14
  • 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 01. Think • Implement the solution – List all the solutions you can think of • Make sure you think outside the box – not just what you assume – Explain why the solutions will work – Implement an action plan: • Test the action plan and update if necessary • Ensures you implement the solution the same on the test and production systems • If a regression occurs, you have a record of the steps you made 15
  • 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring • Gives you a base line • Tells you what has happened – useful to investigate performance issues • Allows you to proactively handle potential issues 16
  • 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring • Many options available: – MySQL Enterprise Monitor – MySQL Workbench – Slow Query Log – MySQL Plugin for Oracle Enterprise Monitor – And many more • Make sure you configure alerts so you react appropriately to all events based on the severity level! 17
  • 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring • On Linux perf is a great tool for live monitoring and can record over a time period – https://perf.wiki.kernel.org/index.php/Main_Page – http://www.brendangregg.com/perf.html • MySQL Enterprise Monitor has some reports to get snapshot data • Workbench has performance reports based on the sys schema “Real time” monitoring 18
  • 19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring MySQL Workbench Performance Reports 19
  • 20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring • The sys schema is a collection of views, functions, and procedures aiming to make the Information Schema and Performance Schema simpler to use. • Included by default in MySQL 5.7+ • Also available from https://github.com/mysql/mysql-sys for MySQL 5.6 • Formerly known as ps_helper by Mark Leith • https://dev.mysql.com/doc/refman/5.7/en/sys-schema.html What was that sys schema? 20
  • 21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring Finding queries to optimize - MySQL Enterprise Monitor Query Analyzer 21
  • 22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 02. Monitoring Finding queries to optimize – Performance Schema Digest Summary mysql> SELECT LEFT(DIGEST_TEXT, 64) AS DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT, MAX_TIMER_WAIT FROM performance_schema.events_statements_summary_by_digest ORDER BY MAX_TIMER_WAIT DESC LIMIT 10; +------------------------------------------------------------------+------------+-----------------+----------------+ | DIGEST_TEXT | COUNT_STAR | SUM_TIMER_WAIT | MAX_TIMER_WAIT | +------------------------------------------------------------------+------------+-----------------+----------------+ | INSERT INTO `salaries` VALUES (...) /* , ... */ | 342 | 159811231808000 | 4156961573000 | | INSERT INTO `dept_emp` VALUES (...) /* , ... */ | 42 | 31561264335000 | 2458392698000 | | INSERT INTO `titles` VALUES (...) /* , ... */ | 63 | 35738435708000 | 1735350241000 | | INSERT INTO `employees` VALUES (...) /* , ... */ | 51 | 18004605187000 | 1679817477000 | | INSERT INTO `sbtest` ( `k` , `c` , `pad` ) VALUES (...) /* , ... | 10 | 5241286782000 | 1247361451000 | | COMMIT | 342 | 31984662051000 | 992714081000 | | DROP SCHEMA IF EXISTS `employees` | 6 | 1252459420000 | 848771265000 | | CREATE TABLE `sbtest` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCR | 1 | 565468324000 | 565468324000 | | CREATE TABLE `dept_manager` ( `dept_no` CHARACTER (?) NOT NULL , | 3 | 355874700000 | 220491035000 | | SELECT COUNT (?) AS `cnt` , `round` ( ( `performance_schema` . ` | 6 | 386062170000 | 217206520000 | +------------------------------------------------------------------+------------+-----------------+----------------+ 10 rows in set (0.00 sec) 22
  • 23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File 1. Start with an empty configuration file 2. Set paths, port, etc. 3. Enable additional monitoring 4. Set capacity settings 5. Don’t do much more! 23
  • 24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • It can be an advantage to have the paths pointing to separate disk systems – Reduces contention – I/O often becomes a bottleneck – Can place the hot files on faster disks • For example with innodb_flush_log_at_trx_commit = 1 and high transaction commit rate, it can be necessary to place the InnoDB redo log on SSDs to support the high rate of flushes. • Note: File-per-table tablespaces and general tablespaces can belocated outside datadir when they are created Paths 24
  • 25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • If you use InnoDB, enable all the INNODB_METRICS counters: – innodb_monitor_enable = ‘%’ • The overhead has turned out to be small so worth the extra details • Ensure the Performance Schema is enabled – Does have overhead, but provides very useful information for performance tuning – Consider enabling additional consumers and instruments as required (for example for transactions in MySQL 5.7) – The Performance Schema can also be configured dynamically at runtime Enable additional monitoring 25
  • 26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • MySQL Reference Manual (about innodb_buffer_pool_size): “On a dedicated database server, you might set the buffer pool size to 80% of the machine's physical memory size.” – What if you have 1TB of memory? Do you still want to reserve 20% for other uses? • Instead: – How much memory does the host have? – Subtract memory required by OS and other processes – Subtract memory required by MySQL other then the InnoDB buffer pool – Choose minimum of this and the size of the “working data set” Capacity settings - innodb_buffer_pool_size 26
  • 27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • Total redo log size defined by two options: – innodb_log_file_size – innodb_log_files_in_group • Total size = innodb_log_file_size * innodb_log_files_in_group – Max supported total redo log size: • MySQL 5.5 and earlier: just below 4G • MySQL 5.6 and later: just below 512G • Should be large enough to avoid “excessive” checkpointing • The larger redo log, the slower shutdowns potentially Capacity settings – InnoDB redo log 27
  • 28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • Get current Log sequence number (LSN) and last checkpoint: – SHOW ENGINE INNODB STATUS: – INNODB_METRICS: Capacity settings – InnoDB redo log – Is it large enough? --- LOG --- Log sequence number 602763740 Log flushed up to 602763740 Pages flushed up to 584668961 Last checkpoint at 555157885 mysql> SELECT NAME, COUNT FROM information_schema.INNODB_METRICS WHERE NAME IN ('log_lsn_current', 'log_lsn_last_checkpoint'); +-------------------------+-----------+ | NAME | COUNT | +-------------------------+-----------+ | log_lsn_last_checkpoint | 555157885 | | log_lsn_current | 602763740 | +-------------------------+-----------+ 28
  • 29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • Calculate the amount of used redo log: – Used log = log_lsn_current – log_lsn_last_checkpoint = 602763740 - 555157885 = 47605855 (bytes) • Compare to total size: – Used % = (Used log / Total log) * 100 = (47605855 / (innodb_log_file_size * innodb_log_files_in_group)) * 100 = (47605855 / 100663296) * 100 = 47.29 % Capacity settings – InnoDB redo log – Is it large enough? 29
  • 30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • If the usage reaches 75% an asynchronous flush is triggered – I/O can be so intensive that all other work stalls – Main InnoDB thread may be in the state: flushing buffer pool pages • Ensure you have enough head room to handle peak loads – For example aim at using at most 60% or 70% of the redo log • Important that your monitoring solution monitors the redo log usage • Improvements to the flushing algorithm and new options to control the I/O have been added in newer MySQL versions Capacity settings – InnoDB redo log – Is it large enough? 30
  • 31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • innodb_undo_tablespaces can only be set before initializing the datadir! • If set creates undo log tablespaces outside the system tablespace (ibdata1) • Advantages: – Keeps the system tablespace smaller – Can keep the undo log on faster disks – In MySQL 5.7 the undo log tablespaces can be truncated • Each undo tablespace file is 10M initially • Maximum of 95 undo tablespaces in 5.7 InnoDB Undo Log 31
  • 32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 03. Initial Configuration File • max_connections – be careful setting this too large as each connection requires memory • table_definition_cache – ensure all tables can be in the cache. If you expect 4000 tables, set table_definition_cache > 4000 • table_open_cache – each table can be open more than once • table_open_cache_instances - Typically a good value is 16 Other capacity options 32
  • 33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • Surely enabling caches is a good thing? • Surely the larger caches and buffers the better? Several buffers and caches available 33
  • 34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • Surely enabling caches is a good thing? • Surely the larger caches and buffers the better? • No! Several buffers and caches available 34
  • 35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • It is rarely good to enable the Query Cache – Guarded by a single mutex • Most workloads is best left with the Query Cache disabled (the default): – query_cache_type = 0 • If you think your workload benefits from the Query Cache, test it – The more writes, the less benefit – The more data fitting into the buffer pool, the less benefit – The more complex queries and the larger scans, the more benefit • Often other caching solutions are a better option. Query Cache 35
  • 36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • In MySQL 5.5 and earlier: only used for plain index scans, range index scans, and joins that do not use indexes – No reason to have it larger than the size of each matching row • In MySQL 5.6+ also used for Batched Key Access (BKA) – Queries using BKA can benefit from a larger join buffer • Is the minimum size allocated! • Usually a small global (default for new connections) value is best: 32k-256k • Increase for a session as needed join_buffer_size 36
  • 37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • Like for join_buffer_size a small global value is usually best: 32k-256k • Can monitor using the global status variable Sort_merge_passes: • Aim at a few Sort_merge_passes per second on a busy server • Increase for a session as needed sort_buffer_size mysql> SHOW GLOBAL STATUS LIKE 'Sort_merge_passes'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Sort_merge_passes | 0 | +-------------------+-------+ 1 row in set (0.00 sec) 37
  • 38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 04. Buffers And Caches • Some buffers are allocated in full each time they are used • One query may use several buffers, so large buffers can cause significant memory usage • Memory allocations are relatively expensive • For example Linux glibc malloc changes memory allocation algorithm when crossing threshold (typical 256k or 512k). – Algorithm for larger allocations can be 40 times slower than for smaller allocation Why is it important with small global values? 38
  • 39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 05. Data Consistency Versus Performance • Possible values in order of data safety: – 1: is theoretically the slowest, but with fast SSD it may be around as fast as 2 and 0 – 2: flushed every second (innodb_flush_log_at_timeout in 5.6+) • Transactions may be lost if the OS crashes – 0: MySQL never fsyncs • Transactions may be lost if the OS crashes • Defaults to 1 – flush the redo log after each commit – Required for D in ACID – For this reason it is the recommended value innodb_flush_log_at_trx_commit 39
  • 40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 05. Data Consistency Versus Performance • What if innodb_flush_log_at_trx_commit = 1 is too slow and you want the D in ACID? – Make sure the redo logs are on separate disks • Do not be tempted to have the individual redo logs on different disks – Consider SSD particularly if you have a high commit rate – Battery-backed disk cache also makes flushes cheaper innodb_flush_log_at_trx_commit 40
  • 41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 05. Data Consistency Versus Performance • Specifies how many transactions between each flush of the binary log – 0: when rotating the binary log or when the OS decides – 1: at every transaction commit – this is the safest – N: every N commits • Default value: – MySQL 5.6 and earlier: 0 – MySQL 5.7 and later: 1 • MySQL 5.6 and later support group commit for InnoDB giving less overhead of sync_binlog = 1 sync_binlog 41
  • 42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 05. Data Consistency Versus Performance • sync_binlog != 1 means that slaves most likely will have to be rebuild if the master crashes • But sync_binlog = 0 must be the best performance wise, right? sync_binlog 42
  • 43. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 05. Data Consistency Versus Performance • sync_binlog != 1 means that slaves most likely will have to be rebuild if the master crashes • But sync_binlog = 0 must be the best performance wise, right? – By default: max_binlog_size = 1G – 1G is not a lot of memory nowadays – So the OS may buffer the entire binary log – When rotating the binary log, up to 1G will be flushed to disk • Stalls all other commits until the binary log rotation is done • So in short: sync_binlog = 0 may give the best throughput, but sync_binlog = 1 may give the most predictable performance sync_binlog 43
  • 44. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • The issue may occur at any place in the stack, e.g.: – Application – Application host/hardware – Network between application and host with MySQL – MySQL host/hardware – MySQL • This should also be considered when monitoring • Consider the OS/hardware settings 44
  • 45. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • Make sure you have enough RAM – The active data should fit in the buffer pool – MySQL connections and caches take memory – Extra RAM for • FS cache • Monitoring • RAM disk (tmpfs) Hardware considerations 45
  • 46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • Fast CPU is required for single threaded performance • Recent servers have 32 to 80 cores. • Enable hyper-threading • MySQL now scales beyond 72 cores in 5.7 • Same core count in fewer sockets is better • Faster cores better than more but slower cores Hardware considerations 46
  • 47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • Fast and reliable storage is good for IO bound loads • HDD for sequential reads and writes • Bus-attached SSD for random reads and writes • Big sata or other disk for log files • Several disks! • Life time should be considered Hardware considerations - Storage 47
  • 48. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • L of LAMP • Good on Solaris • Oracle invests on Windows • For pure performance, favor Linux Operating System 48
  • 49. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • I/O Scheduler – Several Linux distributions use the CFQ scheduler by default • OK for reads • Serializes writes! – NOOP and deadline are usually better for MySQL workloads • Deadline is the default I/O scheduler for Oracle Linux I/O settings on the operating system 49
  • 50. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • I/O Scheduler – Check the current scheduler: – Update the scheduler dynamically: – To set at boot time, use the “elevator=deadline” boot option. I/O settings on the operating system shell# cat /sys/block/sda/queue/scheduler noop deadline [cfq] shell# echo deadline > /sys/block/sda/queue/scheduler shell# cat /sys/block/sda/queue/scheduler noop [deadline] cfq 50
  • 51. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • Linux glibc malloc can be a performance bottleneck • Better to use alternative malloc library: – tcmalloc – jemalloc Memory allocation library 51
  • 52. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 06. Consider the Whole Stack • Set alternative malloc library: – mysqld_safe – systemd distributions: • Set LD_PRELOAD in /etc/sysconfig/mysql Memory allocation library [mysqld_safe] malloc-lib = /usr/lib64/libjemalloc.so.1 52
  • 53. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Summary Wrapping it all up 53
  • 54. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Summary • MySQL performance tuning is similar to all other performance tuning: – Premature optimization is bad – Only change one thing at a time and don’t make too large changes – Don’t micro manage – One size does not fit all – Base your decisions on measurements – Understand what an option does before changing it – Understand your system (data) – Understand what you need – Consider the whole stack 54
  • 55. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Thank You Q&A 55
  • 56. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 56

Editor's Notes

  • #9: Probably the first thing to keep in mind is that you should be very careful while following any best practices of performance tuning. The reason is, the environment those practices are applicable to will almost never be identical to yours and what sas said earlier may not hold true any longer for various reaons… like mysql version change or hardware configuration being different etc.
  • #10: Next point is, don’t pay too much attention to the benchmarks provided by hardware and software providers. You should test YOUR environment, your application and benchmark… also keep monitoring and when you apply any changes, make one change at a time.
  • #11: For example read_rnd_buffer_size was in 5.5 and earlier only used by MyISAM. But in 5.6 and later by all storage engines for Multi-Range Optimization.
  • #12: For example read_rnd_buffer_size was in 5.5 and earlier only used by MyISAM. But in 5.6 and later by all storage engines for Multi-Range Optimization.
  • #13: Safety vs performance: e.g. sync_binlog and innodb_flush_at_trx_commit Using the default value also automatically gives you improved values when the default value is changed in new versions. But new options also often has values that are backward compatible so does not take advantage of new features. InnoDB (and NDBCluster) will always have a “PRIMARY KEY” whether specified explicitly or not The PRIMARY KEY can also be a NOT NULL unique index Unsigned integers with auto_increment makes a good PRIMARY KEY for InnoDB. If you use UUID like PRIMARY KEYs for InnoDB, consider re-order the components to have the time component first.
  • #15: Same procedure as when investigating all kinds of issues
  • #16: Everybody knows this but sometimes, we just don’t practice. It’s important to have an action plan and follow it. If something goes wrong, you have a record of what you did.
  • #18: If you start to ignore monitoring alerts because “it’s not important”, sooner or later you will also ignore one that is important I will not go into details here using the monitoring system for performance tuning as there are other talks dedicated to that – see the references later
  • #20: This is one of the reports available in Workbench which gives you details of events which are doing most I/O
  • #23: Used by MySQL Enterprise Monitor Query Analyzer by default in 5.6.14 and later Enabled by default DIGEST_TEXT is a normalized query equivalent to the queries returned by mysqldumpslow Timings are in picoseconds – the sys schema has the format_time() function to convert to human readable text
  • #24: Obviously you may have experiences from previous projects that you may want to take into account But remember, MySQL may have changed since you deployed the last project
  • #25: The InnoDB redo logs may also be a good candidate for spinning disks as it’s sequential I/O; the fsync rate will be deciding factor Binary logs are also I/O intensive, but serial I/O The paths can be reconfigured later
  • #27: Make sure to allow for growth! Can be changed dynamically in 5.7+
  • #28: Drawback of “too large” redo log: slower crash recoveries
  • #29: The INNODB_METRICS log_lsn_% counters are not enabled by default, but it is recommended to enable them. Thus the innodb_monitor_enable = '%‘ recommendation for the initial configuration file.
  • #30: This example uses the default values for the InnoDB redo log size
  • #31: There are other similar main thread states which are not an indication of an asynchronous flush If you have problems with asynchronous flushing, upgrading may help you Side note: you can make incremental backups with MySQL Enterprise Backup using exclusively the redo log – if so make sure it’s big enough to hold the changes between backups.
  • #32: Not really a capacity setting, but important to consider when creating the initial configuration file as innodb_undo_tablespaces cannot be changed later The undo logs can be I/O intensive – with random I/O The maximum allowed number of undo tablespaces were reduced in 5.7 as 32 tablespaces are now reserved for temporary tables
  • #37: Example of option that has changed meaning between releases
  • #38: In 5.5. and earlier the sort buffer was allocated in full each time it was needed
  • #39: Allocation < MALLOC_MMAP_THRESHOLD uses heap, > MALLOC_MMAP_THRESHOLD uses MMAP
  • #40: Have seen cases where 1 was fastest or as fast as 0/2 (redo logs were on SSD) Make sure your operating system and hardware is not lying about the flush-to-disk operation Crash recovery works irrespectively of the setting, but with != 1 some transactions may be missing after the recovery
  • #50: CFQ = Completely Fair Queuing
  • #53: jemalloc was installed from epel