SlideShare a Scribd company logo
1 of 57
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

What's hot

What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
Geir Høydalsvik
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big Data
Morgan Tocker
 

What's hot (20)

MySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMySQL Cloud Service Deep Dive
MySQL Cloud Service Deep Dive
 
MySQL Enterprise Monitor
MySQL Enterprise MonitorMySQL Enterprise Monitor
MySQL Enterprise Monitor
 
MySQL Enterprise Portfolio
MySQL Enterprise PortfolioMySQL Enterprise Portfolio
MySQL Enterprise Portfolio
 
MySQL Manchester TT - Security
MySQL Manchester TT  - SecurityMySQL Manchester TT  - Security
MySQL Manchester TT - Security
 
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
What's new in MySQL 5.7, Oracle Virtual Technology Summit, 2016
 
Oracle Enterprise Manager for MySQL
Oracle Enterprise Manager for MySQLOracle Enterprise Manager for MySQL
Oracle Enterprise Manager for MySQL
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big Data
 
MySQL Security
MySQL SecurityMySQL Security
MySQL Security
 
MySQL Manchester TT - Performance Tuning
MySQL Manchester TT  - Performance TuningMySQL Manchester TT  - Performance Tuning
MySQL Manchester TT - Performance Tuning
 
MySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document StoreMySQL 5.7 - What's new, How to upgrade and Document Store
MySQL 5.7 - What's new, How to upgrade and Document Store
 
MySQL Enterprise Edition Overview
MySQL Enterprise Edition OverviewMySQL Enterprise Edition Overview
MySQL Enterprise Edition Overview
 
MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...
MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...
MySQL Day Paris 2018 - MySQL InnoDB Cluster; A complete High Availability sol...
 
MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015MySQL 5.7: What's New, Nov. 2015
MySQL 5.7: What's New, Nov. 2015
 
MySQL Document Store
MySQL Document StoreMySQL Document Store
MySQL Document Store
 
Efficient Performance Analysis and Tuning with MySQL Enterprise Monitor
Efficient Performance Analysis and Tuning with MySQL Enterprise MonitorEfficient Performance Analysis and Tuning with MySQL Enterprise Monitor
Efficient Performance Analysis and Tuning with MySQL Enterprise Monitor
 
Why MySQL High Availability Matters
Why MySQL High Availability MattersWhy MySQL High Availability Matters
Why MySQL High Availability Matters
 
Introduction to MySQL
Introduction to MySQLIntroduction to MySQL
Introduction to MySQL
 
MySQL Enterprise Backup
MySQL Enterprise BackupMySQL Enterprise Backup
MySQL Enterprise Backup
 
Netherlands Tech Tour - 06 MySQL Enterprise Monitor
Netherlands Tech Tour - 06 MySQL Enterprise MonitorNetherlands Tech Tour - 06 MySQL Enterprise Monitor
Netherlands Tech Tour - 06 MySQL Enterprise Monitor
 
MySQL for Software-as-a-Service (SaaS)
MySQL for Software-as-a-Service (SaaS)MySQL for Software-as-a-Service (SaaS)
MySQL for Software-as-a-Service (SaaS)
 

Similar to 6 Tips to MySQL Performance Tuning

MySQL Enterprise Monitor
MySQL Enterprise MonitorMySQL Enterprise Monitor
MySQL Enterprise Monitor
Mario Beck
 
01 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv101 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv1
Ivan Ma
 

Similar to 6 Tips to MySQL Performance Tuning (20)

MySQL Tech Tour 2015 - Manage & Tune
MySQL Tech Tour 2015 - Manage & TuneMySQL Tech Tour 2015 - Manage & Tune
MySQL Tech Tour 2015 - Manage & Tune
 
MySQL Enterprise Monitor
MySQL Enterprise MonitorMySQL Enterprise Monitor
MySQL Enterprise Monitor
 
BGOUG17: Cloudy with a chance of MySQL
BGOUG17: Cloudy with a chance of MySQLBGOUG17: Cloudy with a chance of MySQL
BGOUG17: Cloudy with a chance of MySQL
 
Upgrading to my sql 8.0
Upgrading to my sql 8.0Upgrading to my sql 8.0
Upgrading to my sql 8.0
 
MySQL enterprise edition
MySQL enterprise edition MySQL enterprise edition
MySQL enterprise edition
 
Rootconf admin101
Rootconf admin101Rootconf admin101
Rootconf admin101
 
01 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv101 demystifying mysq-lfororacledbaanddeveloperv1
01 demystifying mysq-lfororacledbaanddeveloperv1
 
20190615 hkos-mysql-troubleshootingandperformancev2
20190615 hkos-mysql-troubleshootingandperformancev220190615 hkos-mysql-troubleshootingandperformancev2
20190615 hkos-mysql-troubleshootingandperformancev2
 
2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015
2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015 2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015
2015: Whats New in MySQL 5.7, At Oracle Open World, November 3rd, 2015
 
MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...
MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...
MySQL Webinar Series 3/4 - MySQl Monitoring With Enterprise Monitor + Query A...
 
MySQL :What's New #GIDS16
MySQL :What's New #GIDS16MySQL :What's New #GIDS16
MySQL :What's New #GIDS16
 
MySQL The State of the Dolphin - jun15
MySQL The State of the Dolphin - jun15MySQL The State of the Dolphin - jun15
MySQL The State of the Dolphin - jun15
 
What's New in MySQL 5.7
What's New in MySQL 5.7What's New in MySQL 5.7
What's New in MySQL 5.7
 
MySQL Performance Best Practices
MySQL Performance Best PracticesMySQL Performance Best Practices
MySQL Performance Best Practices
 
MySQL Enterprise Edition - Complete Guide (2019)
MySQL Enterprise Edition - Complete Guide (2019)MySQL Enterprise Edition - Complete Guide (2019)
MySQL Enterprise Edition - Complete Guide (2019)
 
My sql performance tuning course
My sql performance tuning courseMy sql performance tuning course
My sql performance tuning course
 
MySQL Performance Tuning at COSCUP 2014
MySQL Performance Tuning at COSCUP 2014MySQL Performance Tuning at COSCUP 2014
MySQL Performance Tuning at COSCUP 2014
 
MySQL for Oracle DBAs
MySQL for Oracle DBAsMySQL for Oracle DBAs
MySQL for Oracle DBAs
 
Netherlands Tech Tour - 07 MySQL Whats upcoming in 5.7
Netherlands Tech Tour - 07 MySQL Whats upcoming in 5.7Netherlands Tech Tour - 07 MySQL Whats upcoming in 5.7
Netherlands Tech Tour - 07 MySQL Whats upcoming in 5.7
 
Pl17: MySQL 8.0: security
Pl17: MySQL 8.0: securityPl17: MySQL 8.0: security
Pl17: MySQL 8.0: security
 

More from OracleMySQL (7)

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

Recently uploaded

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Same procedure as when investigating all kinds of issues
  7. 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.
  8. 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
  9. This is one of the reports available in Workbench which gives you details of events which are doing most I/O
  10. 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
  11. 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
  12. 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
  13. Make sure to allow for growth! Can be changed dynamically in 5.7+
  14. Drawback of “too large” redo log: slower crash recoveries
  15. 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.
  16. This example uses the default values for the InnoDB redo log size
  17. 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.
  18. 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
  19. Example of option that has changed meaning between releases
  20. In 5.5. and earlier the sort buffer was allocated in full each time it was needed
  21. Allocation < MALLOC_MMAP_THRESHOLD uses heap, > MALLOC_MMAP_THRESHOLD uses MMAP
  22. 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
  23. CFQ = Completely Fair Queuing
  24. jemalloc was installed from epel