Professional Documents
Culture Documents
Best Perf in Products Part2 Arubin 15 Sept 2010
Best Perf in Products Part2 Arubin 15 Sept 2010
Alexander Rubin
Principle Consultant MySQL
Copyright 2010 Oracle The World’s Most Popular Open Source Database
About MySQL
• Invest in MySQL!
• “Make MySQL a Better MySQL”
• Develop, Promote and Support MySQL
• Improve engineering, consulting, and support
• MySQL Sunday at Oracle Open World
• Leverage World-Wide, 24x7 Oracle Support
Rely on MySQL
Agenda
Presentation Overview
MySQL versions
Configuration
Monitoring
Queries Tuning
+ Tips
Resources
Copyright 2010 Oracle The World’s Most Popular Open Source Database
The World’s Most Popular Open Source Database
Overview
Covers
MySQL configuration
Monitoring
Query tuning
Based on what customers are asking
This will be technical
No new tools required; everything you need
comes with MySQL!
You cannot become a performance tuning
wizard in 45 minutes - PT Class is 4 day
class
– http://www.mysql.com/training/courses/performanc
e_tuning.html
MySQL Performance Forum
– http://forums.mysql.com/list.php?24
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL Versions and Performance
Latest GA release: MySQL 5.1.50, InnoDB plugin
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL Configuration
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL Configuration
• Single configuration file: my.cnf (Linux/Unix/Mac),
my.ini (Windows)
• Good example (with comments): my-innodb-heavy-
4g.cnf
• Treat it as an example only
• Variables with different scope:
• Global variables
• Session variables
• Engine Specific variables
• Configure buffers
• Global, allocated per MySQL instance
• Session, allocated per each connections
• Make sense only for InnoDB or MyISAM or other engine
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Table Cache
• table_cache – Defines the number of open tables for all threads that
the server should allow
• Open_table_definitions – Displays the number of cached .frm
files
• Open_tables - Displays the number of currently open tables
• Opened_tables - Displays the number of tables that have been
opened
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Temporary Tables
• tmp_table_size – maximum size for in Memory
temporary tables created by MySQL
• max_heap_table_size - Sets the maximum size to
which MEMORY tables are allowed to grow
• Watch:
• Created_tmp_tables – number of temporary table MySQL
created in RAM
• Created_tmp_disk_tables - number of temporary table
MySQL created on DISK
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Session Variables
• Some session variables control space allocated by
each session (connection)
• Setting these to small can give bad performance
• Setting these too large can cause the server to swap!
• Can be set by connection
• SET SORT_BUFFER_SIZE=8*1024*1024; (8M)
• Set small be default, increase in connections that need it
• sort_buffer_size - Used for ORDER BY, GROUP
• BY, SELECT DISTINCT, UNION DISTINCT
• Monitor Sort_merge_passes < 1-2 an hour optimal
• Usually a problem in a reporting or data warehouse database
• Other important session variables (better to keep small)
• read_rnd_buffer_size - Set to 1/2 sort_buffer_size
• join_buffer_size - (BAD) Watch Select_full_join
• read_buffer_size - Used for full table scans, watch
Select_scan
Copyright 2010 Oracle The World’s Most Popular Open Source Database
InnoDB Concepts: Buffer Pool
The In-Memory
buffer pool writes
and reads data
pages from the DB
server
Read Read
Buffer Database
Disk Storage Pool Server
Write
Write
The In-Memory
buffer pool writes
and reads data
pages from O/S
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Internal InnoDB Buffers and Logs
Log
Buffer
Buffer
Writes to disk Pool
at COMMIT and
checkpoints
records, --------------------
Insert undo
records, Insert undo
redo
redolog
log logs
logs
• InnoDB will perform a dual- records,
records,etc.
etc. Update
Update
undo
undologs
logs
buffer write when a record Ib_logfile files ibdata files
is changed issued under a
transaction
Copyright 2010 Oracle The World’s Most Popular Open Source Database
innodb_flush_log_at_trx_commit
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Log Files
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Other InnoDB settings
• innodb_file_per_table
• Create tablespace (.ibd) per table
• innodb_flush_method=O_DIRECT
• Prevent unnecessary OS buffering
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Storage Medium
MySQL
MySQL
Multiple Tablespaces
Data
Data
mysql
mysql
InnoDB multiple tablespaces
test
test City.ibd
City.ibd
Country.ibd
Country.ibd
world
world
Countrylanguage.ibd
Countrylanguage.ibd
Internal data Multiple
Multipletablespaces
tablespaces
Internal data
requires
Country.frm
dictionary
dictionary
--------------------
Transaction
Transaction requiresthe
the (Metadata)
--------------------
records, innodb_file_per_table
Insert innodb_file_per_table
Insertundo
undo records, clause Country.frm
logs redo
redolog
log clause
logs records, etc. (Metadata)
Update records, etc.
Update
undo logs
undo logs Countrylanguage.frm
ibdata files Ib_logfile files
(Metadata)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
InnoDB configuration example
6-8G RAM, dedicated box, v.5.1.50
ignore-builtin-innodb
plugin-load=innodb=ha_innodb_plugin.so
innodb_buffer_pool_size = 4G # OR 6G
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL Monitoring
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL monitoring
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL monitoring: mysqladmin
• SHOW GLOBAL STATUS – provides global information
since uptime
• Real-time information:
• > mysqladmin .. -uroot -i 10 -r ex
• Will show interactive statistics per 10 seconds, example:
mysqladmin … -uroot -i 10 -r ex| grep tmp
| Created_tmp_disk_tables |0 |
| Created_tmp_files |5 |
| Created_tmp_tables | 543352 |
| Created_tmp_disk_tables |0 |
| Created_tmp_files |0 |
| Created_tmp_tables | 20 |
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Slow Query Log
• Contains text of long running queries
• log queries executing longer than long_query_time server variable (in seconds, but
supports microseconds resolution when logging to file)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Show InnoDB Status: Buffer Pool
----------------------
----------------------
Database pages 19
Modified db pages 0
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Show InnoDB Status: Row Operations
--------------
ROW OPERATIONS
--------------
Main thread process no. 31744, id 1189128544, state: waiting for server activity
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Trick with InnoDB : “are we done yet?”
• Delete from a where t1 > 2000000
• How long it will take? How much rows already deleted? What is
the speed?
• 4M rows in table, deleting half of rows
• Show innodb status:
---TRANSACTION 0 97281, ACTIVE 21 sec…
1179 lock struct(s), .. undo log entries 650910
… delete from a where t1 > 2000000
• 1 undo log entries = 1 row
• Deleted 650K rows for 21 sec, avg. speed: 30K rows/sec
• 2M rows will be deleted for 66.6 sec
• 1 min 6 sec – raw estimate
• mysql> delete from a where t1 > 2000000 ;
• Query OK, 2000000 rows affected (1 min 13.64 sec)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Monitoring GUI tools
• MySQL Enterprise Monitor
• Apply for trial: http://www.mysql.com/trials/
• MySQL Workbench
• http://www.mysql.com/products/workbench/
• Plugins for Nagios
• Lots of other tools
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Query Optimization
Copyright 2010 Oracle The World’s Most Popular Open Source Database
MySQL Optimizer Overview
MySQL DBMS
Query Compiler
Parser
Pars
erSyntax
Analyze
Analyze
Syntax
Preprocess –
Preprocessor
Semantic
Semantic orname resolution
checking,
checking, name
resolution
Parse
Optimizer Tree
Generate
generateOptimal
optimal Query
Query Execution Plans QEP
Execution Plans QE ’s)
’s)
(( P
Query
QueryTransformations
Transformations
Search
Searchforfor
optimal
optimal
execution
execution
plan
plan
Plan
Planrefinement
refinement Query
Execution
QEP Plan
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Query Performance 17.1 Query Optimization
Using Indexes for Optimization
• Benefits of indexes
• Contain sorted values
• Result in less disk I/O
• Specialized indexes
• FULLTEXT
• SPATIAL
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Principles for Index Creation
• Use NOT NULL if possible
• Avoid “over-indexing”
• Over-indexing may slow down writes
• Avoid creating redundant indexes
• Estimate whether indexing is efficient
• Indexes that are not selective enough will not be used (efficiently)
• Choose unique and non-unique indexes appropriately
• Make indexes as small as possible
• Index column prefix rather than entire column
• Use a single ALTER TABLE to perform creation/alteration of multiple
indexes (rebuild the table only once)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Composite Indexes
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Leftmost Index Prefixes (2/2)
• SHOW INDEX displays composite index information
mysql> SHOW INDEX FROM CountryLanguage\G
*************************** 1. row ***************************
Table: CountryLanguage
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: CountryCode
Collation: A
Cardinality: NULL
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
*************************** 2. row ***************************
Table: CountryLanguage
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 2
Column_name: Language
Collation: A
Cardinality: 984
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Duplicate Indexes
• Example 1
CREATE TABLE `dupl_index` (
`i` int(11) NOT NULL,
`a` int(11) DEFAULT NULL,
PRIMARY KEY (`i`),
UNIQUE KEY `i` (`i`), DUPLICATE KEY
KEY `i_2` (`i`) DUPLICATE KEY
• Example 2
CREATE TABLE `dupl_index1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
KEY `i1` (`a`,`b`,`c`),
KEY `i2` (`a`,`b`), DUPLICATE KEY
KEY `i3` (`a`) DUPLICATE KEY
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Using EXPLAIN to Analyze Queries
Copyright 2010 Oracle The World’s Most Popular Open Source Database
How EXPLAIN Works (2/3)
• Shows the efficiency of SELECT statements
mysql> EXPLAIN SELECT * FROM Country WHERE Name = 'France'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: Country
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 239
Extra: Using where
Copyright 2010 Oracle The World’s Most Popular Open Source Database
How EXPLAIN Works (3/3)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (1/9)
• Example of using EXPLAIN to analyze and optimize
a query
• Starting out with no indexes ...
mysql> DESC CountryList;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| Code | char(3) | NO | | | |
| Name | char(52) | NO | | | |
+-------+----------+------+-----+---------+-------+
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (2/9)
• Query used:
mysql> SELECT Co.Name, Ci.Name, Ci.Population
-> FROM CountryList Co, CityList Ci
-> WHERE Co.Code = Ci.CountryCode
-> AND Ci.Population > 8000000;
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (3/9)
• EXPLAIN output:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: Co
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 209
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: Ci
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 4018
Extra: Using where; Using join buffer
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (4/9)
• Example (continued)
• EXPLAIN shows the need for indexing:
• type: ALL – all rows are retrieved
• possible_keys: NULL – there are no indexes whatsoever
• Extra: Using where – criteria tested on data of each
scanned row
• Extra: Using join buffer – A row cache is used to
scan the table for each row of previous table
• Good columns to index are those used for searching, grouping
and sorting
• Use Co.Code and Ci.CountryCode to match rows
• Uses CityList.Population to cull rows
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (5/9)
• Adding indexes:
• ALTER TABLE CountryList ADD PRIMARY KEY (Code)
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| Code | char(3) | NO | PRI | | |
| Name | char(52) | NO | | | |
+-------+----------+------+-----+---------+-------+
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (6/9)
• EXPLAIN after adding indexes:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: Co
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 225
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: Ci
type: ref
possible_keys: CountryCode
key: CountryCode
key_len: 3
ref: world.Co.Code
rows: 7
Extra: Using where
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (7/9)
• Example (continued)
• Indicates which indexes are used:
• possible_keys: CountryCode – there is an index called
CountryCode that might be usable
• key: CountryCode – the index called CountryCode will be
used
• key_len: 3 – the number of bytes per index entry that can
be used (less than total length in case an index prefix is used)
• Indicates how indexes are used
• type: ref – the index is scanned to look if rows match the
combination of values from the previous table
• ref: world.Co.Code – the index (or index prefix) from this
table is compared against this value to look for matches
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (8/9)
• Adding an index for Population too changes the order:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: Ci
type: range
possible_keys: CountryCode,Population
key: Population
key_len: 4
ref: NULL
rows: 10
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: Co
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 3
ref: world.Ci.CountryCode
rows: 1
Extra:
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Analyzing a Query (9/9)
• Example (continued)
• For CityList:
• possible_keys: CountryCode, Population – two
indexes are considered
• key: Population – the index called Population will be used
• type: range – only a partial index scan is required
• rows: 10 – the scan likely yields only 10 rows
• For CountryList
• type: eq_ref – values from the previous table are used to
perform single row lookups in this table
• In this case the range scan drastically reduced number of rows,
reversing the join order as compared to the previous queries
Copyright 2010 Oracle The World’s Most Popular Open Source Database
EXPLAIN for Table Processing (1/2)
• Using where (rows tested for criteria before joining with next table)
• Not exists (scan for an outer joined table that is required not to exist
can be skipped once the first row is found to match)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
EXPLAIN for Table Processing (2/2)
• Indicators for inefficient query Extra output
• Using filesort (extra pass to sort rows required to retrieve
actual rows in sorted order)
• Using temporary (result copied to temporary table, indicates
ORDER BY and GROUP BY with different implied row order)
• Using join buffer (repeated scans buffered, might indicate a
missing join condition or index)
• Indicators of poor performance
• Range checked for each record (a series of range scans
are performed preventing efficient caching of scanned rows)
• Just indicators – it’s not a black and white thing
Copyright 2010 Oracle The World’s Most Popular Open Source Database
How to Avoid “Using Filesort”?
• Covered (combined) index can help. Before:
CREATE TABLE `City` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`CountryCode` char(3) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `CountryCode` (`CountryCode`)
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Full covered index
mysql> alter table City add key
covered_ind (CountryCode, Population, name, District);
Copyright 2010 Oracle The World’s Most Popular Open Source Database
Event, Resources and Q&A
• MySQL Sunday at Oracle Open World, 19 Sept 2010
– http://www.oracle.com/us/openworld/mysql-sunday-078000.html
• “Getting the Best MySQL Performance in Your Products:
Part 1, The Fundamentals” replay
– http://mysql.com/news-and-events/on-demand-webinars/display-od-
552.html
• mysql.com/performance
– http://mysql.com/why-mysql/performance/index.html
• Webinar replay
– See: “Embedded Server for ISVs and OEMs” section on
http://mysql.com/news-and-events/on-demand-webinars/
• Questions?
– http://www.mysql.com/about/contact/sales.html?s=oem
– Phone: USA=+1-866-221-0634; Outside USA = +1-208-327-6494
The presentation 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.
Copyright 2010 Oracle The World’s Most Popular Open Source Database