+ All Categories
Home > Documents > MySQL&Server& PerformanceTuning101 - Khan...

MySQL&Server& PerformanceTuning101 - Khan...

Date post: 14-Aug-2018
Category:
Upload: letram
View: 224 times
Download: 0 times
Share this document with a friend
61
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | MySQL Server Performance Tuning 101 Ligaya Turmelle Principal Technical Support Engineer MySQL [email protected] @lig Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 Sunday, October 16, 16
Transcript

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

MySQL  Server  Performance  Tuning  101

Ligaya  TurmellePrincipal  Technical  Support  Engineer  -­‐  [email protected]@lig

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    | 1Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Safe  Harbor  Statement

The  following  is  intended  to  outline  our  general  product  direc@on.  It  is  intended  for  informa@on  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  func@onality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  @ming  of  any  features  or  func@onality  described  for  Oracle’s  products  remains  at  the  sole  discre@on  of  Oracle.

2Sunday, October 16, 16

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Laying  the  Founda;on

3Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

MySQL  in  a  Nutshell

• Worlds  most  popular  open  source  database

• “M”  of  LAMP

• Main  site:  mysql.com

• Developer  Zone:  dev.mysql.com  OR  mysql.org-­‐ Downloads  and  Labs-­‐ Manuals  and  Bugs

-­‐ Forums  and  Worklogs

4Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Step  0

• General-­‐ No  easy  answers-­‐ Benchmark  and  test

-­‐ Under  allocate

5Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Step  0

• The Server- OS

- Network

- Filesystem

6Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Step  0

• MySQL  Server-­‐ Op@mize  the  queries

-­‐ Database  schema

7Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

MySQL  and  Memory

2

8

Global Per Connection

Server Start As Needed

Large Values Small Values

Allocated Once Allocated 0-N Times

Global Memory + (Max Connections * Per Connection Buffers)

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Current  SeKngs

• mysql>  SHOW  GLOBAL  VARIABLES;

• mysql>  SELECT  *  FROM  INFORMATION_SCHEMA.GLOBAL_VARIABLES;

• (5.7)  mysql>  SELECT  *  FROM  PERFORMANCE_SCHEMA.GLOBAL_VARIABLES;

mysql> SHOW GLOBAL VARIABLES;+-----------------------------------------+------------------| Variable_name | Value +-----------------------------------------+------------------| auto_increment_increment | 1| auto_increment_offset | 1 | autocommit | ON | automatic_sp_privileges | ON | back_log | 80 | basedir | /Users/ligaya/

mysql_installs/mysql-5.6.16-osx10.7-x86_64 | big_tables | OFF | bind_address | * | binlog_cache_size | 32768 | binlog_checksum | CRC32 | binlog_direct_non_transactional_updates | OFF | binlog_format | STATEMENT | binlog_max_flush_queue_time | 0 | binlog_order_commits | ON | binlog_row_image | FULL | binlog_rows_query_log_events | OFF | binlog_stmt_cache_size | 32768 | bulk_insert_buffer_size | 8388608 | character_set_client | latin1 | character_set_connection | latin1

9Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Current  SeKngs

• my.cnf  /  my.ini

# The following options will be passed to all MySQL clients

[client]#password! = your_passwordport! ! = 5626socket!! = /tmp/mysql.5.6.26.sock

# The MySQL server[mysqld]##################### specific to this test instanceport! ! = 5626socket!! = /tmp/mysql.5.6.26.socklog-error = /mysql-5.6.26/data/error.logbasedir = /mysql-5.6.26datadir = /mysql-5.6.26/datasafe-user-createserver-id! = 1####################

10Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Anything  Else?

• RAM?

• Dedicated?• 32/64  bit  OS/MySQL?

• Workload?

• Storage  Engines?

11Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Current  Status

• mysql>  SHOW  GLOBAL  STATUS;

• mysql>  SELECT  *  FROM  INFORMATION_SCHEMA.GLOBAL_STATUS;

• (5.7)  mysql>  SELECT  *  FROM  PERFORMANCE_SCHEMA.GLOBAL_STATUS;

mysql> SHOW GLOBAL STATUS;+--------------------------------------+---------------+| Variable_name | Value |+--------------------------------------+---------------+| Aborted_clients | 9363 || Aborted_connects | 15925 || Binlog_cache_disk_use | 15684 || Binlog_cache_use | 272758675 || Bytes_received | 1219816389366 || Bytes_sent | 5227022143540 || Com_admin_commands | 12803263 || Com_assign_to_keycache | 0 || Com_alter_db | 0 || Com_alter_db_upgrade | 0 || Com_alter_event | 0 || Com_alter_function | 0 || Com_alter_procedure | 0 || Com_alter_server | 0 || Com_alter_table | 37 || Com_alter_tablespace | 0 || Com_analyze | 0 || Com_begin | 33025640 || Com_binlog | 0 |

12Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Danger    Math  Ahead!

13Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Finding  the  Delta

• Uptime - seconds-Ex: 8249391 sec = ~2291.5 hrs = ~ 95.5 days

• Find your rate of change

14Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Example

15Sunday, October 16, 16

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Time  to  Start

16Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• General

• InnoDB  

• MyISAM  -­‐  Older  systems

1

2

3

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• Query  Cache  -­‐  Older  systems  

• Thread  and  Table  Cache

• Session  Level

5

4

6

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• General  

• InnoDB  

• MyISAM  -­‐  Older  systems

1

2

3

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

COM_*

• Counters• Each  command

• Used  to  calculate  the  Delta

| Com_select | 1530076294 || Com_set_option | 18004985 || Com_signal | 0 || Com_show_authors | 0 || Com_show_binlog_events | 0 || Com_show_binlogs | 27498 || Com_show_charsets | 1 || Com_show_collations | 39 || Com_show_contributors | 0 || Com_show_create_db | 3 || Com_show_create_event | 0 || Com_show_create_func | 1120 || Com_show_create_proc | 14499 || Com_show_create_table | 183538 || Com_show_create_trigger | 0 || Com_show_databases | 435 || Com_show_engine_logs | 0 || Com_show_engine_mutex | 0 || Com_show_engine_status | 1180106 || Com_show_events | 0 || Com_show_errors | 0 || Com_show_fields | 2915144 || Com_show_function_status | 29 |

20Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Et  Al.

• Connec@ons  and  Max_used_connec@ons

21Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Et  Al.

• Connec@ons  and  Max_used_connec@ons

• Queries  and  Ques@ons

22Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Et  Al.

• Connec@ons  and  Max_used_connec@ons

• Queries  and  Ques@ons• Slow_queries

23Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Et  Al.

• Connec@ons  and  Max_used_connec@ons

• Queries  and  Ques@ons• Slow_queries• Handler_read_*

24Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

max_connecUons

• Max  permiled  concurrent  connec@ons.• Value  affects  memory  usage• higher  values  (200+)  consider/inves@gate  

-­‐ thread  pooling-­‐ connec@on  pooling

25Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

skip_name_resolve

• Do  not  resolve  host  name  for  client  connec@ons• Removes  DNS  lookups  

-­‐ faster  connec@ons-­‐ no  need  to  worry  about  slow  DNS

• Drawback:  Use  only  IP  addresses

26Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

sql_mode

• Changed  a  lot  over  the  years  and  versions• 20+  modes  available• Dictates  how  the  system  operates

-­‐ Defines  SQL  syntax  supported  and  what  data  valida@on  checks  are  performed-­‐ Examples:

• Mode:  STRICT_TRANS_TABLES  :  If  values  can  not  be  inserted  as  given  into  a  transac@onal  table,  abort  the  statement.

• Mode:  NO_ZERO_DATE  :  Whether  server  permits  ‘0000-­‐00-­‐00’  as  a  valid  date• Mode:  NO_ENGINE_SUBSTITUTION  :  Controls  automa@c  subs@tu@on  of  the  default  storage  

engine  when  CREATE  TABLE  or  ALTER  TABLE  uses  a  storage  engine  that  is  disabled  or  not  compiled  in

27Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• General

• InnoDB  

• MyISAM  -­‐  Older  systems

1

2

3

1

2

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

How  is  InnoDB  doing?

• Efficiency-Innodb_buffer_pool_reads_requests

-Innodb_buffer_pool_reads

29

Formula:1 - (Innodb_buffer_pool_read / Innodb_buffer_pool_read_requests)

= buffer pool hit ratio

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_buffer_pool_size

• Global• Dynamic  (5.7.5)• Caches  data  and  indexes• Larger  values  reduces  IO• Self-­‐contained• Max  value  -­‐  Manual:  80%  ;  Personal:  60-­‐70%  

30Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_buffer_pool_instances

• 5.5+• Problem:  conten@on  for  the  buffer  pool  resources• For  systems  with  large  mul@-­‐GB  buffer  pools• Total  buffer  pool  size  divided  between  this  many  instances• Each  instance

-­‐ Manages  its  own  resources-­‐ Has  its  own  mutex

31Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_log_file_size

• Size  of  file  on  disk• Larger  the  file,  the  less  checkpoint  ac@vity• Size  for  each  log  file• Max  combined  log  file  size

-­‐ 4G  <  5.5-­‐ 5.6  =  512G

• Larger  the  log  file  ➯  longer  recovery  @me

32Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_log_buffer_size

• Global• Buffer  for  wri@ng  to  log  files  on  disk• Large  transac@ons?  Write  heavy?

-­‐ Yes  -­‐  increasing  can  help  with  IO• Default:  8M

33Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_file_per_table

• .ibd  files• Default:  Off  <=  5.6.5  >  On• Easier  to  reclaim  space• TRUNCATE  for  a  table  is  faster• Can  monitor  table  size  on  the  file  system• Can  store  specific  tables  on  different  storage  devices• (5.6)  Transportable  tablespaces

-­‐ (5.7)  Par@@oned  tables  supported

34Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_flushlog_at_trx_commit

• Performance  Vs  Durability• 1  -­‐  Default

-­‐ ACID  Compliant• 2  -­‐  Can  lose  up  to  1  second  of  transac@ons

-­‐ InnoDB  log  buffer  wrilen  to  log  file  at  each  commit  -­‐ flushed  to  disk  once  per  second

• 0  -­‐  Can  lose  up  to  1  second  of  transac@ons-­‐ InnoDB  log  buffer  wrilen  to  log  file  once  per  second  and  then  flushed  to  disk

35Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_flush_method

• For  Unix  and  Linux• Method  used  to  flush  data  and  log  files  to  disk• Dependent  on  hardware• Default:  fdatasync• O_DIRECT  recommended  if  you  use  a  hardware  RAID  controller

36Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_numa_interleave

• New  sewng  (5.6.27)• Only  relevant  to  those  on  NUMA  architecture- shell> dmesg | grep -i numa

• Enables  NUMA  interleave  memory  policy  for  buffer  pool• Not  available  in  generic  tarball  or  Windows  from  MSI  installer

37

Public  bug  reports  associated  with  NUMA:  57241,  72811  &  78953

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

innodb_flush_neighbors

• 5.6+• When  flushing  a  page,  flush  other  dirty  pages  in  same  extent• Default:  1

-­‐ Use  for  spinning  disks• Change  to  0

-­‐ Use  for  SSDs  -­‐ Have  both  SSD  and  spinning  disk

38Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• General

• InnoDB  Basics

• MyISAM  -­‐  Older  systems

1

2

3

1

2

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

How  is  MyISAM  doing?

• Space-­‐ Key_blocks_unused

-­‐ Key_blocks_used• High  water  mark

40

Formula:key_buffer_size - (Key_blocks_unused * key_cache_block_size)

= amount actually in use

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

How  is  MyISAM  doing?

• Efficiency-­‐ Key_read_requests-­‐ Key_reads

41

Formula:Key_reads / Key_read_requests = key cache miss rate

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

How  is  MyISAM  doing?

• Locking-­‐ Table_locks_immediate-­‐ Table_locks_waited

42Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

key_buffer_size

• AKA  key  cache• Global• Index  blocks  only• 25%  -­‐  maybe

43Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• Query  Cache  -­‐  Older  systems  

• Threads  and  Table  Cache

• Session  Level

5

4

6

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

QC  Data  Into  Info

• Hit  rate-­‐ Higher  the  beler

• Invalida@ng  queries-­‐ Bigger  the  diff  -­‐  the  beler

45

Formula:Qcache_hits / (Qcache_hits + Com_select) =

Query Cache Hit Rate

Comparison:Qcache_inserts << Com_select

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

query_cache_size

• Global• Allocated  all  at  once• Default:  disabled• Default  size:  0  <  5.6.8  =>  1M• Max:  100MB

46Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

query_cache_type

• Global  -­‐  but  can  be  session• 3  op@ons

-­‐ 0  “Off”-­‐ 1  “On”-­‐ 2  “On  Demand”

• Default:  1  <  5.6.8  =>  0

47Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Gotchas!

48Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• Query  Cache  -­‐  Older  systems  

• Threads  and  Table  Caches

• Session  Level

5

4

6

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Threads

• Threads  ==  Connec@on• Thread  Cache

-­‐ Threads_cached-­‐ Threads_connected-­‐ Threads_created

50

Formula:Threads_created / Connections = Thread cache miss rate

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

thread_cache_size

• Global  but  grows  as  needed• Number  to  be  cached  un@l  needed• May  increase  performance  with  lots  of  new  connec@ons

-­‐ Or  may  not

51

Formula:100 - ((Threads_created / Connections) * 100) =

Thread cache efficiency

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

table_open_cache

• table_cache  <=  5.1.3  >  table_open_cache• Global  but  expands  as  needed• Number  of  open  tables  for  all  connec@ons

-­‐ higher  max_connec@ons,  more  you  need• Watch  Opened_tables

52Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

table_open_cache_instances

• Reduces  conten@on  between  sessions• Default  1

-­‐ Consider  8  or  16  on  16+  core  systems• 5.6+

53Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

table_definiUon_cache

• Global  but  expands  as  needed• Number  of  table  defini@ons  (.frm  file)  to  hold• Large  number  of  tables  ➯large  defini@on  cache  =  speed  up  table  opening• InnoDB

-­‐ Soz  limit  for  open  table  instances  in  data  dic@onary-­‐ Soz  limit  for  number  of  InnoDB  file  per  table  tablespaces  open  at  1  @me

• 5.1+• Autosizes  >=5.6.8

54Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Areas  We  Will  Cover

• Query  Cache  -­‐  Older  systems  

• Threads  and  Table  Cache

• Session  Level

5

4

6

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Temporary  Tables

56

max_heap_table_size

• Dynamic• Set maximum size of all

MEMORY tables

tmp_table_size

• Dynamic• In-memory - MEMORY table• max size of internal in-

memory temp tables• Becomes on-disk table

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

sort_buffer_size

57

• Dynamic• Doing  a  sort  -­‐  allocates  a  buffer  this  size• Helps  with  ORDER  BY  and/or  GROUP  BY• 256K  is  a  decent  star@ng  place• Watch  Sort_merge_passes

Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

join_buffer_size

• Use  with  care• Use  for

-­‐ Plain  index  scan-­‐ Range  index  scan-­‐ Joins  that  do  not  use  indexes  -­‐  full  table  scan

• One  buffer  for  each  full  join  between  tables

58Sunday, October 16, 16

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

QuesUons?

59Sunday, October 16, 16

Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

MySQL  Server  Performance  Tuning  101

Ligaya  TurmellePrincipal  Technical  Support  Engineer  -­‐  [email protected]@lig  

hlps://legacy.joind.in/16770

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    | 60Sunday, October 16, 16

Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |

Sunday, October 16, 16


Recommended