UC2005 Advanced MySQL Performance Optimization
UC2005 Advanced MySQL Performance Optimization
com
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Introductions
Peter Zaitsev, MySQL Inc
Senior Performance Engineer MySQL Performance Group Manager MySQL Performance consulting and partner relationships
Tobias Asplund
MySQL Training Class Instructor MySQL Performance Tuning Consultant
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Table of Contents
A bit of Performance/Benchmarking theory Application Architecture issues Schema design and query optimization Sever Settings Optimizations Storage Engine Optimizations Replication Clustering Hardware and OS optimizations Real world application problems
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Question Policy
Interrupt us if something is unclear Keep long generic questions to the end Approach us during the conference Write us: [email protected], [email protected]
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
How long have you been using MySQL ? Did you ever have performance issues with MySQL ? What is your previous database background ?
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Defining Performance
Simple world but many meanings Main objective:
Users (direct or indirect) should be satisfied
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Throughput
Metric: Transactions per time (second/min/hour)
Only some transactions from the mix can be counted
Problems:
starvation - some users can be waiting too long single user may rather need his request served fast
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Response Time/Latency
Metric: Time (milliseconds, seconds, minutes)
derived: average/min/max response time derived 90 percentile response time
Problems:
Counts wall clock time, does not take into account what else is happening
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
Scalability
Metric: Ability to maintain performance with changing
load (incoming requests) database size concurrent connections hardware
Different performance metric maintain performance typically defined as response time When to use
Capacity planning
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
10
Queuing Theory
Multi User applications Request waits in queue before being processed User response time = queueing delay + service time
Non high tech example support call center.
Hockey Stick - queuing delay grows rapidly when system getting close to saturation Need to improve queueing delay or service time to improve performance Improving service time reduces queuing delay
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
11
Direct Measurements
Sum up all query times from web pages
Indirect measurements
CPU usage Number of active queries Disk IO latency Network traffic loadavg etc
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
12
Benchmarks
Great tool to:
Quantify application performance Measure performance effect of the changes Validate Scalability Plan deployment
But
Can be very misleading if done wrong
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
13
Real database size Real input values Similar number of connections Similar think times Effect of caching Similar Server Settings,Hardware, OS, Network etc
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
14
Testing in single user scenario Going without think times Benchmarking on single host
While in real world application works across the ocean
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
15
Query complexity growth may be different Different transactions may have different growth ratio Watch for user behavior changes Does database still fit in memory ?
20% increase in size may slow things down 10 times.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
16
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
17
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
18
Application Architecture
Designing Scalable application Architecture Role of Caching Replication/Partition/Clustering Architectural notes for C/Perl/PHP/Java/.Net Application level performance analyses
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
19
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
20
Architecture Design
Try to localize database operations
to change this we need to fix 15000 queries we need
Scale Out
32 CPU box vs 20 2 CPU boxes The Google Way
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
21
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
22
Magic of Caching
Most applications benefit from some form of caching For many caching is the only optimization needed Many forms of caching
HTTP Server side proxy cache Pre-parsed template cache Object cache in the application Network distributed cache Cache on file system Query cache in MySQL HEAP/MyISAM tables as cache Database buffers cache
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
23
Proxy Cache
External request-response cache Useful when data does not change Must have for static, semi-static web sites Can be just overhead for dynamic only Problems with cache invalidation
Protocol level control may not suite application
Security issues
storing sensitive data on the disk disclosing data to wrong user
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
24
Is not the same as template language cache. Many different variants and tools available. Need to identify which data is static ? - which is not ? Example:
A static homepage, with the exception of rotating success stories
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
25
Object/Functional cache
Cache results of functions or objects
for example user profile
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
26
Example tool:
memcached
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
27
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
28
No invalidation control
query invalidated when involved table is updated
Does not work with prepared statements Works great for many read intensive web applications
As it is typically the only data cache used
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
29
MyISAM
Fast disk based tables
TEMPORARY
Result caching for single session Caching subquery common for many queries
Global
Caching data shared across session Caching search results
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
30
Database/OS Buffers
Data and indexes cached in Database or OS buffers Provided automatically, usually presents
MySQL server and OS Server settings.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
31
Number of Connections
Many Established connections take resources Frequent connection creation take resources
not as much as people tend to think
Use connection pool of limited size Limit number of connections can be established at the time
FastCGI, Server side proxy for web world
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
32
Replication
Board sense getting multiple copies of your data Very powerful tool, especially for read mostly applications MySQL Replication (Will discuss later) Manual replication
more control, tricky to code, can be synchronous
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
33
Partitioning
Local partitioning: MERGE Tables
Logs, each day in its own table.
May need copies of data partitioned different way No waste of resources. Efficient caching Can be mixed with replication for HA
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
34
Clustering
Clustering something automatic to get me HA, performance Manual clustering with MySQL Replication (more later) Clustering with shared/replicated disk/file system
Products from Veritas/Sun/Novell Build your own using Heartbeat Innodb, Read-only MyISAM Does not save from data corruption Active-Passive waste of resources Share Standby box to reduce overhead Switch time can be significant ACID guaranties no transaction loss
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
35
Clustering2
MySQL Cluster (Storage Engine)
Available in MySQL 4.1-max binaries
MySQL 5.0 will have a lot improved version Replication + automatic hash partition
Shared nothing architecture Many MySQL servers using many storage nodes Synchronous replication, row level Requires fast system network for good performance Very much into providing uptime
including online software update
In memory only at this point. With disk backup. Fixed cluster setup can't add more nodes as you grow
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
36
Clustering3
Third party solutions EMIC Application Cluster
Nice convenient tools, easy to use Commercial, Patched MySQL version required Synchronous replication, Statement level Full data copy on each node Limited scalability for writes, good for reads Very transparent. Only need to reconnect No multi statement transactions support Some minor features are not supported
ie server variables
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
37
C/C++ considerations
Native C interface is the fastest interface
reference interface which Java and .NET reimplement Most tested. Used in main test suite, Perl DBI. PHP etc very simple. May like some fancy wrapper around Make sure to use threaded library version if using threads Only one thread can use connection at the same time
use proper locking connection pool shared by threads is good solution
Better to use same as server major version of client library Prepared statements can be faster and safer
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
38
Perl
Use latest DBD driver it supports prepared statements Using with HTTP server use mod_perl or FastCGI Do not forget to free result, statement resources
This is very frequently forgotten in scripting languages
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
39
PHP
Standard MySQL Interface
compatibility
PEAR DB
Slower Compatibility, support multiple databases Object interface, prepared statements like interface PHP5 Presentation
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
40
Java
Centralize code that deals with data base
Change persistence strategies without rewrite
Use connection pooling, do not set pool size too large Do not use autoReconnect=true, catch exceptions
it can lead to hard to catch problems
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
41
.NET
Try to use prepared statements as much as possible. Close all connection you open
Simple but very typical problem
Use ExecuteReader for all queries where you are just iterating over the rows
DataSets are slow and should only be used when you really need access to all of the rows on the client
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
42
Gathering statistics on application level objects Can be combined with server data for deep analyses
MySQL 5.0 SHOW LOCAL STATUS
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
43
Shema design
Optimal schema depends on queries you will run Data size and cardinality matters Storing data outside of database or in serialized for
XML, Images etc
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
44
Normalization
Normalized in simple terms
all objects in their own tables, no redundancy Simple to generate from ER diagram Compact, single update to modify object property Joins are expensive Limited optimizer choices for selection, sorting
select * from customer, orders where customer_id=order_id and order_date=2004-01-01 and customer_name=John Smith
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
45
Non-Normalized
Non-Normalized
Store all customer data with each order Huge size overhead Data updates are complex
To change customer name may need to update many rows. deleted last order no data about customer any more select * from orders where order_date=2004-01-01 and customer_name=John Smith
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
46
Normalisation: Mixed
Using Normalised for OLTP and non-normalised for DSS Materialized Views
No direct support in MySQL but can create MyISAM table
Keep some data non-normalized and pay for updates Use value as key for simple objects
IP Address, State
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
47
Data Types
Use appropriate data type do not store number as string
09 and 9 are same number but different strings
Use NOT NULL if do not plan to store NULLs Use appropriate char length. VARCHAR(64) for name
some buffers are fixed size in memory sorting files, temporary tables are fixed length
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
48
Indexing
Index helps to speed up retrieval but expensive to maintain MySQL can only use prefix of index
key (a,b) .... where b=5 will not use index.
Order of columns in BTREE index matters Avoid duplicated - two indexes on the same column(s) Index, being prefix of other index is rarely good idea
remove index on (a) if you have index on (a,b)
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
49
Indexing
Covering index save data read, faster scans with long rows
select name from person where name like %et% Prefix index for data selective by first few chars key(name(8)) Short keys are better, Integer best Close key values are better than random access locality is much better auto_increment better than uuid() OPTIMIZE TABLE compact and sort indexes ANALYZE TABLE - update statistics
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
50
Index Types
BTREE
default key type for all but HEAP helps = lookups as well as ranges, sorting supported by all storage engines
HASH
Fast, smaller footprint only exists for HEAP storage engine
slow with many non-unique values
Only helpful for full = lookups (no prefix) can be emulated by CRC32() in other storage engines
select * from log where url=http://www.mysql.com and url_crc=crc32(http://www.mysql.com);
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
51
FULLTEXT
MyISAM only. Speed up natural language search Very slow to update for long texts
More to come
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
52
Designing queries
General notes Reading EXPLAIN output Understanding how optimizer works What exactly happens when query is executed Finding problematic queries Checking up against server performance data
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
53
General notes
Know how your queries are executed
On the real data, not on the 10 rows pet table.
Watch for query plan changes with upgrades, data change Do not assume a query that executes fast on other databases will do so on MySQL. Use proper types in text mode queries
int_col=123 and char_col='123'
Use temporary table for caching Sometimes many queries works better than one
and easier to debug when 70K query joining 25 tables
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
54
Reading EXPLAIN
+----+--------------------+---------+--------+---------------+---------+---------+------------------------+------+---------------------------------+
UPDATE, DELETE need to be converted to SELECT For each SELECT MySQL executes from one table looking up in others id, select_type - to which query does this row corresponds table - which table is being accessed Order of tables is significant.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
55
Reading EXPLAIN
type how table is accessed (most frequent)
ALL - full table scan eq_ref - = reference by primary or unique key (1 row) ref - = by non-unique key (multiple rows) range - reference by >, < or compex ranges
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
56
Reading EXPLAIN
ref - The column or constant this key is matched against rows - How many rows will be looked up in this table
Multiply number or rows for tables in single select to estimate complexity
mysql> explain select * from t1,t2 where t1.i=t2.i order by t1.i+t2.i; +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 36864 | Using temporary; Using filesort | | 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 36864 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ 2 rows in set (0.00 sec)
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
57
Some assumptions are being made for missing statistics Optimizer has execution methods to use
full table scan, index scan, range, ref etc
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
58
Simple Example
SELECT City.Population/Country.Population FROM City,Country WHERE CountryCode=Code; MySQL need to select table order
Scanning City and checking Country for each Scanning Country and checking all Cityes for it
In each table orders different keys can be used Search set too large not all possibilities tested Next Step: Optimize order by/group by if present
Should use index to perform sort ? Filesort ? Should use temporary table or sort for group by ?
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
59
Compute row values result values Row is now buffered to be sent to client
as soon as network buffer is full it is sent to client
mysql> explain select City.Population/Country.Population from City,Country where CountryCode=Code; +----+-------------+---------+--------+---------------+---------+---------+------------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+--------+---------------+---------+---------+------------------------+------+-------+ | 1 | SIMPLE | City | ALL | CountryCode | NULL | NULL | NULL | 4079 | | | 1 | SIMPLE | Country | eq_ref | PRIMARY | PRIMARY | 9 | world.City.CountryCode | 1 | | +----+-------------+---------+--------+---------------+---------+---------+------------------------+------+-------+ 2 rows in set (0.00 sec)
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
60
SQL_BUFFER_RESULT
Result will be buffered in temporary table before sending
handle slow clients, unlock MyISAM tables faster
SQL_BIG_RESULT/SQL_SMALL_RESULT
Set if you're expecting large or small result set Affects how group by is optimized, temporary table created
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
61
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
62
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
63
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
64
Kernel Objects
sockets, kernel stacks, file descriptor table File System Cache
Thread Memory
thread stacks, sort_buffer_size, tmp_table_size, read_buffer_size etc
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
65
How much resources do you want MySQL to use ? How many connections are you expecting
thread buffers should not run you out of memory
Special Requirements
Replication ? Audit ? Point in time recovery ?
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
66
General Settings
--character-set
use simple character set (ie latin1) if single language
--join_buffer_size
buffer used for executing joins with no keys. Avoid these
--binlog_cache_size
when --log-bin enabled. Should fit most transactions
--memlock
lock MySQL in memory to avoid swapping
--max_allowed_packet
should be large enough to fit lagest query
--max_connections
number of connections server will allow. May run out of memory if too high
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
67
--sync_binlog
Flush binary log to disk. Reduce corruption chances
--table_cache
Number of tables MySQL can keep open at the same time. Reopening table is expensive
--thread_cache_size
Keep up to this amount of thread cached after disconnect
--tmp_table_size
Max size of memory hash table. Will use disk table for larger sets
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
68
--query_cache_type
Should query cache be disabled/enabled or on demand ?
--query_cache_limit
Maximum result set size to cache. Avoid erasing all query cache by result of large query
--query_cache_wlock_invalidate
Should query cache be invalidated on LOCK TABLES ... WRITE
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
69
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
70
MySQL Status
Aborted_clients - are you closing your connections ?
if no check network and max_allowed_packet
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
71
MySQL Status
Connections number of new connections established
way to high number may ask for connection pooling.
Created_tmp_disk_tables table taking more than tmp_table_size will be converted to MyISAM disk table
if BLOB/TEXT is sellected disk based table is used from start look at increasing tmp_table_size if value is large
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
72
Handler_read_next reverse index scan, rare Handler_read_rnd retrieve row by position Handler_read_rnd_next physically next row
Corresponds to full table scans. Handler_read_rnd_next/Handler_read_rnd approximate average size of full table scan
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
73
Key_blocks_unused number of free keyblocks now Key_read_requests, Key_reads logical and physical key block reads
Key_reads/Key_read_requests miss ratio watch for Key_reads/sec - match against your io system
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
74
Status
Max_used_connections maximum number of connections used
check if it matched max_connections
too low value or sign of overload.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
75
Qcache_hits times result was served from query cache Qcache_inserts times query was stored in query cache
This is overhead. hits/inserts should be large
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
76
Qcache_queries_in_cache number of queries stored in the cache Qcache_total_blocks Total number of blocks in cache
check against query_cache_size to see average size of block
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
77
Server Status
Questions number of questions server got
all of them including malformed queries good rough load indicator for stable load mix
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
78
Server Status
Select_range_check joins when key selection is to be performed for each row
large overhead, check query plan
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
79
Sort_range sorting of the range Sort_scan sorting by scanning, full table scan Sort_rows number of rows sorted
a clue how complex sorts are happening
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
80
Threads_cached - number of threads in thread_cache Threads_connected - number of current connections Threads_created theads created (thread_cache misses)
should be low.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
81
Storage Engines
MyISAM specific Optimizations Innodb specific Optimizations Heap Specific Optimizations Power of multiple Storage Engines Designing your own storage engine
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
82
MyISAM
MyISAM Properties
no transactions, will be corrupted on power down small disk and memory footprint packed indexes, works without indexes, FULLTEXT,RTEE table locks, concurrent inserts read-only packed version only index is cached by MySQL, data by OS Logging applications Read only/read mostly applications Full table scan reporting Bulk data loads, data crunching Read/write with no transactions low concurrency
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
83
set bulk_insert_buffer_size if doing massive inserts, use multiple value inserts. Deleting/updating/adding a lot of data disable indexes
ALTER TABLE t DISABLE KEYS
set myisam_max_[extra]_sort_file_size large so REPAIR TABLE is done by sort, much faster use --myisam_recover do not ever run with corrupted data use merge tables for large historical data. Index tree should fit in cache
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
84
Try optimizing blocking queries Try low_priority_updates=1 waiting updates will not block selects, but may starve forever Vertically partition separate columns you typically update Horizontally partition - users -> users01.... users09
also good help for ALTER TABLE, OPTIMIZE TABLE
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
85
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
86
Good for
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
87
Use DROP TABLE/CREATE TABLE instead of TRUNCATE TABLE (before 5.0) Use SET UNIQUE_CHECKS=0, SET FOREIGN_KEY_CHECKS=0 for data load Try prefix keys - especially efficient as there is no key compression
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
88
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
89
innodb_flush_log_at_trx_commit
1 (slow) will flush (fsync) log at each commit. Truly ACID 2 will only flush log buffer to OS cache on commit
transaction is not lost if only MySQL server crashes may lose few last comited transactions
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
90
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
91
Buffer Pool
Buffer pool size 24576, Free buffers 0, Database pages 23467, Modified db pages 0
Log activity
Row activity Locks information, deadlocks, transaction status, pending operations, a lot more In MySQL 5.0, some variables exported to SHOW STATUS
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
92
Usage:
Cache tables Temporary tables Buffer tables (insert/update buffering)
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
93
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
94
Store constant data in MyISAM, dynamic critical data in Innodb and use Heap for temporary tables. ALTER TABLE tbl ENGINE=<engine>
Conversion back and forth is simple, easy to try
Downsides
Mixed database configuration is more complicated
backup, maintenance, tuning especially optimizer may have hard time.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
95
Examples:
Archive storage engine to deal with huge log files
used by Yahoo
Special distributed storage engines Storage engines for fuzzy matches Storage engine for network lookup (Friendster) Storage engine to read apache log files
MySQL development and support can help with design and implementation.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
96
MySQL Replication
MySQL Replication Architecture Setting up MySQL Replication Replication concepts for your application Bidirectional, Circular replication issues Fallback/Recovery in MySQL Replication
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
97
Replication is asyncronous. Slave has a bit old data. Slave has 2 threads
IO Thread - fetch statemenets from master, store locally SQL Thread - get from local storage and execute
So if master goes down the gap between master and slave is small
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
98
Get master data snapshot to the slave, and binary log position
they must match exactly for replication to work properly
CHANGE MASTER TO MASTER_HOST='host', MASTER_USER='repl', MASTER_PASSWORD='slavepass', MASTER_LOG_FILE='recorded_log_file_name', MASTER_LOG_POS=recorded_log_position;
Run SLAVE START Run SHOW SLAVE STATUS on the slave to ensure it worked
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
99
Use mysqldump master-data Innodb: Use Innodb Hot Backup (commercial tool) User LVM or other volume manager with snapshot
run FLUSH TABLES WITH READ LOCK run SHOW MASTER STATUS, record position create snapshot run UNLOCK TABLES copy snapshot to the slave
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
100
Replication Options
--log-slave-updates log updates from slave thread
useful for chain replication, using slave for backup
--slave-skip-errors - continue replication with such errors --sync_binlog=1 - Sync binlog on each commit
if you want to continue after master restart from crash
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
101
Replication concepts
Master -> Slave
Most simple one, gives some HA and performance
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
102
Bi-Directional Replication
Master1 <-> Master2
Writing to both nodes update conflicts, no detection
Due to asynchronous replication auto increment values collide MySQL 5.0 --auto-increment-offset=N updates can be lost Check if queries can be executed in any order
UPDATE TBL SET val=val+1 WHERE id=5
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
103
Chain,Circular Replication
Chain Replication
Slave1->Slave2->Slave3 Can be used as tree replication if there are too many slaves HA if middle node fails, all below it stop getting updates Complex rule to find proper position for each on recovery
Circular Replication
Slave1->Slave2->Slave3->Slave1 Same problems as in Bi-Directional replication Same HA issues as Chain Replication
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
104
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
105
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
106
Clone the slave from scratch Ignore the problem and hope to be lucky
most commonly used approach :)
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
107
Load Balancing
Balance load across the slaves or partitions
Fall back
Master or slave may day, need proper handling.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
108
Hardware,OS, Deployment
Hardware selection for MySQL Hardware Configuration OS Selection OS Configuration Physical Deployment
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
109
Hardware Selection
CPU: Consider 64bit CPUs
EM64T/Opteron are best price/performance at this point
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
110
Hardware Selection II
System Bus - can be overloaded on high load
different buses of IO, Network may make sense
Network card
Watch for latency, 1Gb Ethernet are good CPU offloading (Checksum generation etc)
check for driver support
Extension possibilities
Can you add more memory ? More disks ?
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
111
Disk IO Subsystem
Need RAID to ensure data security
Slaves could go with RAID0 for improved performance
Multiple channels good with many devices Software RAID1/RAID10 typically good as well
random IO does not eat much of CPU time
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
112
Disk IO Subsystem
Compute your IO needs drive can do (150-250 IO/sec) Test your RAID if it gives you performance it should
SysBench http://sourceforge.net/projects/sysbench
SAN easy to manage but slower than direct disks NAS, NFS Test very carefully
works for logs, binary logs, read only MyISAM a lot of reported problems with Innodb
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
113
Hardware configuration
Mainly make sure it works as it should
sometimes bad drivers are guilty
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
114
OS Selection
MySQL Supports wide range of platforms
Linux, Windows,Solaris are most frequently used
all three work well
Better to use OS MySQL delivers packages for RedHat, Fedora, SuSE, Debian, Gentoo most frequent
Any decent distribution works Get MySQL server from http://www.mysql.com
Ensure vendor can help you we can't fix some OS bugs Watch for good threads support
Kernel level threads library for SMP support Older FreeBSD, NetBSD had some issues
Make sure your memory is addressable by OS Make sure all your hardware is well supported by OS
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
115
OS Configuration
Allow large process sizes
MySQL Server is single process
Allow decent number of open files, especially for MyISAM If possible lock MySQL in memory (ie memlock) Make sure VM is tuned well, to avoid swapping
And Size MySQL buffers well
Tune read-ahead. Too large read-ahead limits random IO performance Set proper IO scheduling configuration (elevator=deadline for Linux 2.6) Use large pages for MySQL process if OS allows ie
--large-pages option in 5.0 for Linux
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
116
OS Configuration
Use Direct IO if using Innodb for Data
Logs and MyISAM are better with buffered O_DIRECT in Linux forcedirectio in Solaris
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
117
Deployment Guidelines
Automate things, especially dealing with many systems Have load statistic gathering and monitoring Use different Database and Web (application) Server
different configuration, quality requirements, scaling
Use binary log so you can do point in time recovery Have slow log enabled to catch slow queries.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
118
MySQL Workloads
MySQL in OLTP Workloads MySQL in DSS/Data warehouse Workloads Batch jobs Loading data Backup and recovery
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
119
OLTP Workloads
Online Transaction Processing
Small Transactions, Queries touching few rows, random access Data size may range from small to huge, not uniform access
Make sure your schema is optimized for such queries If you can fit your working set in memory great Watch for locks (table locks, row locks etc) For large databases check random IO your disks can handle Configure MySQL for your number of connections
Large global buffers (key_buffer, innodb_buffer_pool) Smaller per thread buffers - sort_buffer, read_rnd_buffer
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
120
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
121
Use temporary tables result buffering, data selection Creating shadow tables for operation may make sense
ie small MyISAM table based on Innodb table
Running batch jobs on dedicated Slave Periodic sleep() to avoid resource hog Do some data processing in application
beware mysql_store_result() with large data sets
use mysql_use_result()
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
122
Do not add indexes one by one, add all of them by ALTER TABLE
if you're dropping/adding columns do it in the same command
Parallel loading
myisam_repair_threads=N will build indexes in parallel Innodb does not have matching option. May load different tables at the same time
beware of fragmentation,random IO, increased working set
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
123
MyISAM
loading data in empty table is much faster.
Workaround use ALTER TABLE t DISABLE KEYS before loading data, ALTER TABLE t ENABLE KEYS after check it is the case in SHOW PROCESSLIST myisam_sort_file_size=100G, myisam_max_extra_sort_file_size=100G use large myisam_sort_buffer_size Unique indexes are not build by sort (use large key_buffer_size)
Bulk_insert_buffer_size
Increase if doing bulk inserts in table with data
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
124
Load data in primary key order. Do external sort if needed May watch how load goes in SHOW INNODB STATUS If have unique keys and sure data is unique
SET UNIQUE_CHECKS=0
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
125
Store your binary logs since at least last 2 backups some people archive them forever. Test your backup actually restores valid data Test how long time restoration process takes Textual backups can take very long time to restore Test how long time roll forward recovery takes mysqlbinlog logfile015.bin start-position=123 | mysql It may take up to several hours for each live hour
roll forward recovery is done by single thread
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
126
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
127
Indexed updated in live fashion (slow updates) Really slow when index does not fit in memory Slow with common words search
MATCH (product) AGAINST (video evita IN BOOLEAN MODE) -> MATCH (product) AGAINST (evita) IN BOOLEAN MODE) AND product LIKE %video%;
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
128
Bulk updates in shadow search table for good performance No native stem support may use special field with stemmed text, same works for custom parsed text Index stored in BTREE
fetching data requires random IO OPTIMIZE table improves performance, sorting index
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
129
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
130
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
131
Very fast index speed, 5min vs 24 hours for Mnogosearch Modest (tunable) memory consumption Good relevance ranking (any,all) Fast retrieval from given offset, match counting
displaying result 1000.1010 from 56787
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
132
match all mode MySQL native Full Text search tested just count(*)
count retrieval typically needed anyway
NonCached Cached
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
133
If sequential IDs with no holes use direct lookup SELECT * FROM tbl WHERE id=<rnd 1...N>
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
134
Logging
Logs in database are cool easy reporting using SQL
SELECT AVG(rtime) FROM log WHERE request=search
Limit indexes these are most expensive to update Create multiple tables, easy, fast data purging:
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
135
May make sense to cache string Path in the group table /Products/Electronics/VHS ... LIKE /Products/% will get you all subgroups
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
136
Listing navigation
Common problem directories, forums, blogs etc
show everything from offset 2000 to 2010
SELECT * FROM tbl LIMIT ORDER BY add_time 2000,10
slow
2000 rows has to be scanned and thrown away
works but
Precompute position
SELECT * FROM tbl WHERE POS BETWEEN 2000 and 2010
is fast
hard to do live, may use delayed published new entries can be shown out of order until position counted
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
137
Always full reads - can't get first 100 bytes MyISAM row read done together with BLOB
may use separate table if BLOB is rarely accessed Innodb will skip reading BLOB if it is not requested
Watch for fragmentation, if deleting/updating Memory consumption 3 times size the blob on server Use Binary Protocol - avoid escaping.
MySQL Users Conference 2005 , April 18-21 | Advanced MySQL Optimization Tutorial | MySQL AB 2005 | www.mysql.com
138
Resources
MySQL Online Manual great source for Information
http://dev.mysql.com/doc/mysql/en/index.html