Optimizing MySQL Configuration
Peter Zaitsev,CEO
Technical e!inars Series
March "#$"
%%%&percona&com
'gen(a
)
MySQL Configuration Tuning *asics
)
Tools to Configure MySQL
)
Loo+ing at Most ,mportant Options
%%%&percona&com
Things to +no% a!out MySQL
configuration
)
-efault configuration is poor
MySQL (oes not scale it %ith server size
)
.n(erstan( %hat you/re changing
0oogle Copy1Paste %ithout thin+ing can !e !a(
)
'voi( O!sessive Tuning -isor(er
Setting $# settings %ill give 234 of possi!le
performance in 234 cases
)
*e%are of 5Sample Configs6 ,n MySQL
(istri!utions
They are pretty out(ate(
"0* of memory is 5huge6 these (ays 7
%%%&percona&com
Most Options (o not Scale
)
0oing to Server %ith 89 memory you can/t :ust
multiply all configuration varia!les 89
$;0* of memory to $"80* of memory
sort_buffer_size <M* to ="M* is !a( i(ea&
%%%&percona&com
>no% Scope an( .nit
)
sort_buffer_size?$;0
rong@ sort_buffer_size is set per connection
)
table_cache_size?;<M
rong@ table_cache_size is set in elements
not memory size&
%%%&percona&com
Set Aaria!les Locally
)
Many Aaria!les are SESSION
Can !e set for current session only
)
Set varia!le value for session (oing comple9
Bueries instea( of setting it glo!allyC
mysql> set session sort_buffer_size=16*1024*1024;
Query OK, 0 rows ffe!te" #0$00 se!%
%%%&percona&com
'voi( *asic Mista+es
)
Setting varia!les in %rong config file
/etc/mysql/my.cnf instea( of /etc/my.cnf
These (epen( on Linu9 -istro, *e%are
)
-uplicating Options
Last option %ill overri(e previously set
)
Dot +no%ing Synonyms
table_cache is same as table_open_cache
)
.sing %rong section for options
Server rea(s [mysqld], client [mysql]
%%%&percona&com
Config Management Practices
)
>eep Config Eiles in Sync on (ifferent servers
Out of Sync config files is freBuent cause of
mista+es an( confusion
)
>eep Fecor( of Changes
Config files un(er version control is great
't least +eep your changes (ocumente(
%%%&percona&com
-o not let MySQL S%ap
)
'llocating too much memory an( having MySQL
s%apping is a lot %orse than not using all memory
)
Monitor s%apping Gsi1so from vmstat closelyH
)
Start %ith safe !uffer values an( increase them
gra(ually if a lot of memory stays free
pz@ubuntu:~$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 272570 25!21" 51!572 0 0 1 1 20 22 0 0 100 0
0 0 0 2725700 25!21" 51!5#" 0 0 0 0 72 7! 0 0 100 0
0 0 0 2725700 25!21" 51!5#" 0 0 0 ! 70 7$ 0 0 ## 1
0 0 0 2725700 25!21" 51!5#" 0 0 0 0 70 7$ 0 0 100 0
0 0 0 2725700 25!21" 51!5#" 0 0 0 0 70 7$ 0 0 100 0
0 0 0 2725700 25!21" 51!5#" 0 0 0 0 70 72 0 0 100 0
%%%&percona&com
'gen(a
)
MySQL Configuration Tuning *asics
)
ools to !onfi"ure #yS$%
)
Loo+ing at Most ,mportant Options
%%%&percona&com
'utomate( Configuration Tuning
)
Configuration Tuning Tools
Tools %hich claim to create !est configuration !y
loo+ing at status varia!les
)
'(visory Tools
Tools %hich chec+ your config file for typical
mista+es an( omissions
)
*asic configuration creation tools
-o not claim to (o magic !ut can get your
starte( %ith !etter configuration than (efault
%%%&percona&com
mysBtuner
IIIIIIII 0eneral Statistics IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
JIIK S+ippe( version chec+ for MySQLTuner script
JO>K Currently running supporte( MySQL version 3&$&3LIrel$"&8Ilog
JO>K Operating on ;<I!it architecture
IIIIIIII Storage Engine Statistics IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
JIIK StatusC M'rchive I*-* IEe(erate( M,nno-* I,S'M ID-*Cluster
JIIK -ata in My,S'M ta!lesC L=0 GTa!lesC $"8"H
JIIK -ata in ,nno-* ta!lesC $0 GTa!lesC ==8H
JIIK -ata in MEMOFN ta!lesC #* GTa!lesC "H
J@@K Total fragmente( ta!lesC $$#
IIIIIIII Security Fecommen(ations IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
J@@K .ser /O/ has no pass%or( set&
%%%&percona&com
MysBltuner G"H
IIIIIIII Performance Metrics IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
) JIIK .p forC $3L( $#h #m "=s G3==M B J=2&"$2 BpsK, 8M conn, TPC $"#"*, FPC
$<;*H
) JIIK Fea(s 1 ritesC 2L4 1 =4
) JIIK Total !uffersC <&=0 glo!al M "&LM per threa( G"## ma9 threa(sH
) JO>K Ma9imum possi!le memory usageC <&80 G<#4 of installe( F'MH
) J@@K Slo% BueriesC L4 G<$M13==MH
) JO>K Qighest usage of availa!le connectionsC 3<4 G$#21"##H
) JO>K >ey !uffer size 1 total My,S'M in(e9esC <$&=0
) JO>K >ey !uffer hit rateC $## G<3* cache( 1 ;M rea(sH
) J@@K Query cache is (isa!le(
) JO>K Sorts reBuiring temporary ta!lesC #4 G<<> temp sorts 1 ;#M sortsH
) J@@K Roins performe( %ithout in(e9esC "33;83
) J@@K Temporary ta!les create( on (is+C <$4 G"3M on (is+ 1 ;$M totalH
) JO>K Threa( cache hit rateC 2$4 GL2$> create( 1 8M connectionsH
) J@@K Ta!le cache hit rateC "4 G$> open 1 32> opene(H
) JO>K Open file limit use(C ="4 G">18>H
) JO>K Ta!le loc+s acBuire( imme(iatelyC 224 G<=;M imme(iate 1 <=;M loc+sH
) J@@K ,nno-* (ata size 1 !uffer poolC $&$01"3;&#M
)
%%%&percona&com
mysBltunerG=H
IIIIIIII Fecommen(ations IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
) 0eneral recommen(ationsC
) Fun OPT,M,ZE T'*LE to (efragment ta!les for !etter performance
) '(:ust your :oin Bueries to al%ays utilize in(e9es
) hen ma+ing a(:ustments, ma+e tmpSta!leSsize1ma9SheapSta!leSsize eBual
) Fe(uce your SELECT -,ST,DCT Bueries %ithout L,M,T clauses
) ,ncrease ta!leScache gra(ually to avoi( file (escriptor limits
) Aaria!les to a(:ustC
) BueryScacheSsize GT? 8MH
) :oinS!ufferSsize GT $"8&#>, or al%ays use in(e9es %ith :oinsH
) tmpSta!leSsize GT $;MH
) ma9SheapSta!leSsize GT $;MH
) ta!leScache GT <#2;H
) inno(!S!ufferSpoolSsize GT? $0H
%%%&percona&com
ptIvaria!leIa(visor
) U 'FD inno(!SflushSlogSatStr9ScommitI$C ,nno-* is not configure( in strictly 'C,- mo(e&
)
) U DOTE inno(!Sma9S(irtySpagesSpctC The inno(!Sma9S(irtySpagesSpct is lo%er than the (efault&
)
) U DOTE logS%arningsI"C LogS%arnings must !e set greater than $ to log unusual events such as
a!orte( connections&
)
) U DOTE ma9SconnectSerrorsC ma9SconnectSerrors shoul( pro!a!ly !e set as large as your
platform allo%s&
)
) U 'FD ol(Spass%or(sC Ol(Istyle pass%or(s are insecure&
)
) U 'FD slaveSnetStimeoutC This varia!le is set too high&
)
) U DOTE inno(!S(ataSfileSpathC 'utoIe9ten(ing ,nno-* files can consume a lot of (is+ space that is
very (ifficult to reclaim later&
)
) U 'FD myisamSrecoverSoptionsC myisamSrecoverSoptions shoul( !e set to some value such as
*'C>.P,EOFCE to ensure that ta!le corruption is notice(&
)
) U 'FD syncS!inlogC *inary logging is ena!le(, !ut syncS!inlog isn/t configure( so that every
transaction is flushe( to the !inary log for (ura!ility&
%%%&percona&com
tools&percona&com
%%%&percona&com
Tools&percona&com
%%%&percona&com
Tools&percona&com
%%%&percona&com
'gen(a
)
MySQL Configuration Tuning *asics
)
Tools to Configure MySQL
)
%oo&in" at #ost Important Options
%%%&percona&com
Lets loo+ at the options no%
)
-ifferent classes of optionsC
0eneral Options
My,S'M
,nno(!
Aisi!ility an( Logging
%%%&percona&com
0etting Status Aaria!les
)
e refer to S'O( )%O*+% S+,S output in
many (escriptions
)
Percona Tool+it too ptIme9t is helpful
)
pt-me.t -r -- mysqladmin e.t -i/00 -c1
) '!orte(Sclients $"8 # #
'!orte(Sconnects 2#2 # #
*inlogScacheS(is+Suse = # #
*inlogScacheSuse ";"83L # #
*ytesSreceive( $<;3$82#";8$ 38#2L; <32$$=
*ytesSsent $"#"28=#<2<"; $<$L88; $#$8;$L
%%%&percona&com
0eneral Options
)
ma._connections
Qo% many connections to allo% 7 atch
ma._used_connections status value
)
thread_cache
Cache to prevent e9cessive threa( creation
3#I$## is goo( value& atch threads_created
)
table_cache/table_open_cache
Cache of opene( ta!le instances
Single ta!le may have multiple entries
atch opened_tables status value
Start %ith <#2;
MySQL %ill only use as nee(e( any%ay&
%%%&percona&com
0eneral Options
)
open_files_limit
My,S'M ta!les reBuire up to " file han(lers
Each connection is file han(ler too
Safe to set to ;33=3 in most systems
)
table_definition_cache
Cache ta!le (efinitions GCFE'TE T'*LEH
Only one entry per ta!le
atch Opened_table_definitions
Set to num!er of ta!les M $#4 unless 3#>M
ta!les
%%%&percona&com
0eneral Options
)
bac&_lo"
Dee( a(:ustment if many connections1sec
"#<8 is reasona!le value
)
ma._allo2ed_pac&et
Limits ma9imum size of Buery
Limits internal string varia!le size
$;M* is a goo( value
)
ma._connect_errors
Prevent pass%or( !rute force attac+
Can cause 5Qost *loc+e(6 error messages
Aalue aroun( $###### is goo(
%%%&percona&com
0eneral Options
)
s&ip_name_resol3e
'voi( -DS loo+up on connection& Easter an(
Safer
-o not use host names in )4+Ns
)
old_pass2ords
Shoul( NO !e ena!le(& ill cause insecure
pass%or( hash to !e use(&
%%%&percona&com
0eneral Options
)
lo"_bin
Ena!le for replication an( point in time recovery
Set to 5mysBlI!in6 to avoi( (efault naming
)
sync_binlo"
Ma+e *inlog (ura!le& Set to $ if have F',-
%ith **. or Elash
Can !e really performance +iller %ith slo%
(rives&
)
e.pire_lo"_days
Purge ol( !inary logs after this num!er of (ays
$< G" %ee+sH is a goo( value %ith %ee+ly
!ac+ups&
%%%&percona&com
0eneral Options
)
tmp_table_size
)
ma._heap_table_size
Typically set to same value G%or+loa( !ase(H
!reated_tmp_dis&_tables status varia!le
*e%are *LO*1TEPT fiel(s cause on (is+
ta!le %ith any size&
)
query_cache_size
Ena!le Buery cache only if it is teste( to provi(e
significant gains
Often causes stalls an( contention
-o not set over 3$"M*
%%%&percona&com
0eneral Options
)
sort_buffer_size
,n memory !uffer use( for sorting
atch sort_mer"e_passes
Consi(er setting for session for large Bueries
Aalues up to $M* are goo( (efault
Large values hurt performance of small Bueries
)
5oin_buffer_size
Qelps performance of Roins %ith no in(e9es
*etter get ri( of such Roins @
8M* can !e reasona!le value
)
default_stora"e_en"ine
.se this engine for ta!les if not specifie(
%%%&percona&com
0eneral Options
)
read_rnd_buffer_size
*uffer for rea(ing ro%s in sorte( offer
Specifies Ma9imum Aalue
Aalues aroun( $;M* often ma+e sense
-o not mi9 %ith read_buffer_size
)
mpdir
Specify location of temporary (irectory
Tmpfs often goo( choice unless very large
temporary space is nee(e(&
tmpdir6/de3/shm
%%%&percona&com
My,S'M options
)
&ey_buffer_size
Cache My,S'M ,n(e9es&
-oes Dot cache (ata&
.p to =#4 of memory if using My,S'M only
)
myisam_reco3er
'utomatically repair corrupte( My,S'M ta!les
after crash& *+!7,89:O4!E is a goo( value&
)
myisam_sort_buffer_size
*uffer use( for !uil(ing My,S'M in(e9es !y
Sort& 8M*I"3;M* are goo( values&
%%%&percona&com
My,S'M Options
)
lo2_priority_updates
'llo% higher concurrency for SELECTs
May starve up(ate Bueries
)
bul&_insert_buffer_size
*uffer to optimize *ul+ ,nserts
Aalues of V of &ey_buffer_size ma+e sense
Dote it is per connection value
%%%&percona&com
,nno(! W Memory Settings
)
innodb_buffer_pool_size
The most important setting& Often 8#4M of
memory is allocate( here&
)
innodb_buffer_pool_instances
Fe(uce contention& Set to <M in MySQL 3&3M
)
innodb_lo"_buffer_size
*uffer for log files& 0oo( Aalues <M*I$"8M*
Dot only re(uce %rites !ut help contention
)
innodb_ibuf_ma._size
Control size of ,nsert !uffer& -efault is X of
*uffer pool& Smaller values are goo( for SS-
%%%&percona&com
,nno(! ,O Options
)
innodb_flush_lo"_at_tr._commit
Control -ura!ility
$?flush an( syncY "?flushY #?neither
)
Innodb_flush_method
Controls ho% ,nno(! Performs ,O
O_;I4E! goo( value for most servers
)
innodb_auto_lru_dump
Percona Server Eeature to %armup Buic+ly
=## Gsecon(sH is a goo( value
)
innodb_io_capacity
Controls ,nno(! 'ssumption a!out -is+
Performance& ,ncrease for faster (rives&
%%%&percona&com
,nno(! ,O Options
)
innodb_read_io_threads
)
innodb_2rite_io_threads
Control num!er of threa(s (oing rea(s an(
%rites
MySQL 3&3 has async ,O so very high values
might not !e nee(e(
< is goo( (efault& Qigher for large ,O systems&
)
innodb_flush_nei"hbor_pa"es
Percona Server feature to control ho% flushing
%or+s
-isa!le Gset to #H for SS-
%%%&percona&com
Other ,nno(! Options
)
innodb_lo"_file_size
Size of re(o log file& Larger logs !etter
performance !ut longer recovery&
)
innodb_lo"_files_in_"roup
Leave at " %hich is (efault&
)
innodb_file_per_table
Store each ,nno(! ta!le in separate file& .sually
0oo( choice
)
innodb6force
Ena!le so MySQL (oes not start if ,nno(! coul(
not initialize& Other%ise it might start !ut error on
access to all ,nno(! ta!les&
%%%&percona&com
Other ,nno(! Options
)
innodb_data_file_path
Settings for ,nno(! System Ta!lespace
.se one file& Limit gro%th as you can/t shrin+ it
ibdata/</0#<autoe.tend<ma.</0)
)
innodb_loc&_2ait_timeout
Qo% long to %ait for ro% level loc+s !efore
!ailing out 7
)
innodb_old_bloc&s_time
Qelps to ma+e !uffer pool scan resistant
Aalues aroun( $### ma+e sense
%%%&percona&com
Other ,nno(! Options
)
innodb_file_format
hich file format ,nno(! %ill use
5'ntelope6 is (efault legacy format
5*arracu(a6 allo%s to use ne% features li+e
compression
)
innodb_stats_on_metadata
.p(ate statistics on meta (ata access
Such as ,nformationSschema Bueries
Typically !est (isa!le( for more %or+loa(s
Set to #
,nno(! %ill still refresh stats %hen ta!le
changes significantly
%%%&percona&com
Aisi!ility Options
)
performance_schema
Ena!le Performance Schema in MySQL 3&3M
atch potential overhea(&
)
lo"_slo2_queries
Ena!le Slo% Query Log& Ol( !ut very helpful&
)
lon"_query_time
Especially %ith longSBueryStime set to #
perio(ically to get sample of the loa(
)
lo"_slo2_3erbosity6full
0et a lot more (ata a!out Bueries in 8ercona
Ser3er
%%%&percona&com
Aisi!ility Options
)
lo2_2arnin"s6=
0et %arnings a!out (isconnects an( other
minor issues in error log&
More information !ut it can get spammy
)
userstat_runnin"6/
0et a(vance( Ta!le an( ,n(e9 usage statistics
in Percona Server an( Maria-*
%%%&percona&com
Summary
)
Many Options to chose from @
)
Close to <## varia!les availa!le ,n latest versions
)
Femem!er in most cases you (o not nee( to tune
more than a fe%
)
Consi(er starting %ith config file generate( !y
httpC11tools&percona&com
't least it %ill sho% you %hich options to pay
attention to first&
%%%&percona&com
4esources
Qigh Performance MySQL
Thir( e(ition in a matter of (ays@
httpC11%%%&highperfmysBl&com1
Percona Training
httpC11%%%&percona&com1training1
-allas,TP May$<I$L
Percona e!inars
httpC11%%%&percona&com1%e!inars1
Mar "8C Monitoring MySQL %ith Percona Monitoring Plugins
'pr <C Percona Ptra-* Cluster
%%%&percona&com
4esources
Percona Tool+it
ptIvisualIe9plain tool ma+es EPPL',D rea(a!le
httpC11%%%&percona&com1soft%are1
Percona Online Tools
httpC11tools&percona&com1
0enerate my&cnf files easily
0et a(vice on SQL
Percona Career Center GNes, e/re Qiring@H
www.percona.com/about-us/careers/
%%%&percona&com1live
.se 5e!inarIPL6 co(e for $#4 off
#yS$% !onference > E.po
+pril /0-/=9 Santa !lara
8eter ?aitse3
pz@percona.com