Академический Документы
Профессиональный Документы
Культура Документы
performance tuning
&
optimization
James McLaurin
Solution Architect
MariaDB
Agenda
Blog: https://mariadb.com/resources/blog/20-tips-prepare-your-database-black-friday-cyber-monday
Why Tune?
● Configuring threadpool
○ Pool-of-threads, or threadpool, is a MariaDB feature that improves
performance by pooling active threads together instead of the old one thread per
client connection method, which does not scale well for typical web-based
workloads with many short-running queries.
• Don’t do naive math, like “my new server has 2x RAM, it’ll just make all the values
2x the previous ones”.
Performance
Server Hardware and OS Tuning
Server Hardware
•Swappiness
○ Value for propensity of the OS to swap
to disk
○ Defaults are usually 60
○ Commonly set low to 10 or so (not 0)
Linux Settings
•Noatime
○ Mount disks with this option
○ Turns off writing of access time to disk
with every file access
○ Without this option every read becomes
an additional write
Performance
MariaDB Configuration Settings
Configuration Settings
CPU usage A high CPU usage is not a bad thing as long as you don’t reach the limit. None
Memory usage Ideally your entire database should be stored in memory, but this is not always None
possible. Give MySQL as much as you can afford but leave enough for other
processes to function.
Swap usage Swap is for emergencies only, and it should not be used. When used swap is > 128MB.
Network bandwidth Unless doing backups or transferring huge amounts of data, it shouldn’t be the None
bottleneck.
Disk usage Make sure you always have free space for new data, temporary files, snapshot or When database, logs and temp is >
backups. 85% usage.
Disk Monitoring
IO Queue length Tracks how many operations are waiting for disk access. If a query hits the cache, it doesn’t create None
any disk operation. If a query doesn’t hit the cache (i.e. a miss), it will create multiple disk operations.
Average IO wait Time that queue operations have to wait for disk access. None
Average Read/Write Time it takes to finish disk access operations (latency). None
time
Read/Write bandwidth Data transfer from and towards your disk. None
MariaDB Metrics
Uptime Seconds since the server was started. We can use this to detect respawns. When uptime is < 180.
Threads_connected Number of clients currently connected. If none or too high, something is wrong. None
Max_used_connections Max number of connections at a time since server started. When connections usage is > 85%.
(max_used_connections / max_connections) indicates if you could run out soon
of connection slots.
Aborted_connects Number of failed connection attempts. When growing over a period of time When aborted connects/min > 3. (only
either some credentials are wrong or we are being attacked. on not public exposed servers,
otherwise will generate noise)
MariaDB typical errors
common failure points to keep an eye on
(Log files size) Are all log files being rotated? None
(Deleted log files) Were any log files deleted but the file descriptor is still open? None
(Backup space) Do you have enough disk space for backups? None
Monitoring caches, buffers & locks
Innodb_row_lock_waits Number of times InnoDB had to wait before locking a row. None
Innodb_buffer_pool_wait_free Number of times InnoDB had to wait for memory pages to be flushed. If too high, None
innodb_buffer_pool_size is too small for current write load.
Open_tables Number of tables currently open. If this is low and table_cache is high, we can reduce cache None
size. If opposite, we should increase it. If you increase table_cache you might have to increase
available file descriptors for the mysql user.
(Long running transactions) Tracks whether too many transactions are locked by other idle transactions, or because of a None
problem in InnoDB.
(Deadlocks) Deadlocks happen when 2 transactions mutually hold. These are unavoidable in InnoDB and None
apps should deal with them.
Monitoring Queries
Slow_queries Number of queries that took more than long_query_time seconds to execute. Slow queries None
generate excessive disk reads, memory and CPU usage. Check slow_query_log to find them.
Select_full_join Number of full joins needed to answer queries. If too high, improve your indexing or database None
schema.
Created_tmp_disk_tables Number of temporary tables (typically for joins) stored on slow spinning disks, instead of faster None
RAM.
(Full table scans) Number of times the system reads the first row of a table index. Sequential reads might indicate None
Handler_read% a faulty index.
Performance
Query Tuning
Query Tuning
slow-query-log = 1
slow-query-log-file = /var/lib/mysql/mysql-slow.log
long_query_time = 1
The first directive enables the logging of slow queries, while the second one tells MariaDB where to store the actual
log file. Use long_query_time to define the amount of time that is considered long for a MariaDB query to be completed.
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE
Query Analysis MONTH(birth_date)
● Use the Slow Log to find Problem Queries
● Use mysqldumpslow Utility for Manageable
= 8 \G Reports
EXPLAIN SELECT * FROM employees
id: 1
WHERE MONTH(birth_date) = 8 \G ● Use EXPLAIN to see how MariaDB Executes a
id: 1 select_type: SIMPLE Troublesome Query and if Indexes are Used
select_type: SIMPLE table: employees ● Use EXPLAIN EXTENDED and SHOW
table: employees type: ALL
type: ALL WARNINGS to see how MariaDB Rearranges a
possible_keys: NULL possible_keys: NULL Query before Execution
key: NULL key: NULL
key_len: NULL key_len: NULL
ref: NULL
rows: 299587 ref: NULL
Extra: Using where rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE
MONTH(birth_date)
= 8 \G
● Try Not to Query Tune on Production Server
id: 1
select_type: SIMPLE ● Use Test Server with same Hardware and OS
● Use Copy of Production Data Set
Query Tuning Overview
table: employees
● Query Frequency is as Important as Query
type: ALL
possible_keys: NULL Speed
key: NULL ○ Moderately Slow Queries are often a Bigger
key_len: NULL Problem than a Rarely Run Very Slow Query
ref: NULL
rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE ● Indexes Improve Read Performance
MONTH(birth_date) ● Without Index, MariaDB Must Read Every Row —
= 8 \G Full Table Scan
id: 1 ● With Index, MariaDB can jump to Requested
select_type: SIMPLE Rows
table: employees
Indexing type: ALL
● Reduced I/O and Improving Performance
● Index Increase cost of Writes
possible_keys: NULL ● Find Balance
key: NULL ● Index for Speed, but Avoid Indexing Excessively
key_len: NULL or Arbitrarily
ref: NULL ● Remove Dead or Redundant Indexes
rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE ● Keep Indexes as Small as Practical
MONTH(birth_date) ○ Faster since More Likely to Fit in Memory
= 8 \G ○ Rebuilds Faster after Writes
id: 1 ○ PRIMARY KEY should be Minimum Useful
select_type: SIMPLE Size
table: employees ● Use Partial Prefix Indexes for String Columns
Index Size type: ALL ○ May Slow Searches Slightly, but Reduce
possible_keys: NULL Index Size
key: NULL ● Use Index Cardinality (Uniqueness Measure)
key_len: NULL Only If Necessary — Re-evaluate as Data Grows
ref: NULL ○ Low Cardinality Indicates many Duplicates
rows: 299587 ○ High Cardinality is More Useful
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE ● Identify Accurately and Carefully Trouble Spots
MONTH(birth_date) ○ Guessing is Rarely Useful
= 8 \G ● Gather Performance Stats with MariaDB and OS
id: 1 Tools
select_type: SIMPLE ○ SHOW Statements
table: employees ○ PERFORMANCE_SCHEMA
Tools & Statistics
type: ALL ○ CPU, Disk, Network, Memory, & Swap Stats
possible_keys: NULL ● Retain Snapshots of Multiple Stats
key: NULL ○ Data from a Single Point Shows very Little
key_len: NULL ● Automate the Collection of Stats into Logs
ref: NULL ○ Can be Useful for Emergency Tuning
rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE
MONTH(birth_date)
= 8 \G ● Snapshot of mysqld Activity
id: 1 ● mysqld is Multi-Threaded, One Thread per
select_type: SIMPLE ○ Client Connection (i.e., query, transaction) —
SHOW PROCESSLIST
table: employees a "process" is a "thread"
type: ALL ● Accumulate SHOW PROCESSLIST Snapshots to
possible_keys: NULL
key: NULL build History of Thread Activities
key_len: NULL
ref: NULL
rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE ● Returns List of Internal Counters
MONTH(birth_date) ● GLOBAL for System-Wide Status — Since
= 8 \G Start-Up
id: 1 ● SESSION for Local to Client Connection
select_type: SIMPLE FLUSH STATUS Resets Local Counters
SHOW STATUS table: employees
●
● Monitor Changes to Counters to Identify Hot
Global or Sessiontype: ALL Spots
possible_keys: NULL ● Collect Periodically Status Snapshots to Profile
key: NULL Traffic
key_len: NULL
ref: NULL
rows: 299587
Extra: Using where
Query Tuning
EXPLAIN SELECT *
FROM employees
WHERE ● Similar to INFORMATION_SCHEMA , but
MONTH(birth_date) Performance Tuning
= 8 \G ● Monitors MariaDB Server Events
id: 1 ● Function Calls, Operating System Waits, Internal
select_type: SIMPLE Mutexes, I/O Calls
table: employees ● Detailed Query Execution Stages (Parsing,
PERFORMANCE_SCHEMA
type: ALL Statistics, Sorting)
possible_keys: NULL ● Some Features Storage Engine Specific
key: NULL ● Monitoring Lightweight and Requires No
key_len: NULL Dedicated Thread
ref: NULL ● Designed to be Used Iteratively with Successive
rows: 299587 Refinement
Extra: Using where
Thank you
james.mclaurin@mariadb.com.