SlideShare a Scribd company logo
MySQL : maintaining
  (too) big tables
    Frédéric Descamps
th
11 April 2010
Frédéric Descamps

●   lefred on IRC and Twitter
●   Senior Linux and Open Source Consultant
    @inuits.be
●   Certified MySQL DBA since 2007
●   First GNU/Linux distro (as far as I remember):
    Slackware 3.0 (kernel 1.2.13)
●   Blog: http://www.lefred.be
How come we need to
maintain tables ?
●   A bit of history first:


     once upon a time, there
    was a developer who found
    a great idea
Then he decided to create a proof of concept
and he coded, coded, coded a lot...
During the period he
unfortunately didn't discuss
with the DBA's and the OPS




                       But he discussed with
                      many other people like
                      marketing manager, sales
                      manager, regional manager,
                      product manager and all
                      were very happy and asked
                      to code even more !
Then he coded, coded,
coded a lot...
And the result is
Why ?
●   Impossible to stop de database, it's too
    important !
●   There is too much data, tables are huge !!
    •   Tables > 400GB with 500M rows
    •   Primary keys are varchar(32)
●   Impossible to optimize, add an index to a table
    (>4h needed to add an index)
●   How do we alter the schema ?
●   How do we migrate to another version of
    MySQL ?
Analyze and optimize
How big ?
# ls ­lh dayview.ibd 
­rw­rw­­­­ 1 mysql mysql 29G Feb  3 22:20 dayview.ibd
dba's feeling ?
Two very important points to never forget as
    developer :
●   Don't underestimate the complexity of production
    environments
Don't !
●   Never say: “but on my
    laptop the query is
    very fast”
What now ?
●   The plan is to create a new server more
    manageable with minimal downtime of the
    production
     •   Reduce size if possible
     •   Make queries faster
●   The server will be a replica of the current
    production server with some different settings
The battle !
●   Restore last backup

  Ze Plan                       ●   Prepare the new
                                    database
                                ●   Dump & restore
                                    data using maatkit

databasezilla
                databasezilla   ●   Restart the
                   restore          replication
                                ●   Replace the
                                    production db by
                                    the new one (only
                                    downtime here)
Database size
●   Deleting historical data sometimes doesn't help
    really to save some space... ibdata won't shrink
●   Data could be fragmented
     •   To avoid the use of that huge ibdata we add
         this to the configuration of the new server:
innodb_file_per_table
MySQL version
●   Maybe it is time to also change the MySQL
    version to a newer one and even to change
    engine !
●   I advice you to try and use XtraDB from
    percona. This includes corrections and
    interresting patches (like those from Google)
    •   The list is available here :
        http://www.percona.com/docs/wiki/percona-
        xtradb:patch:start
Anything else to
change ?
●   Maybe other settings aren't optimal for your
    production environment. To have an overview
    of them, I'd recommand the use of these two
    tools :
    •   MySQLTuner
    •   tuning-primer
Example 1
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.37-community-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in InnoDB tables: 899G (Tables: 149)
[!!] Total fragmented tables: 82

-------- Performance Metrics -------------------------------------------------
[--] Up for: 23h 46m 1s (112M q [1K qps], 70K conn, TX: 44B, RX: 22B)
[--] Reads / Writes: 51% / 49%
[--] Total buffers: 12.4G global + 152.2M per thread (100 max threads)
[!!] Maximum possible memory usage: 27.3G (173% of installed RAM)
[OK] Slow queries: 0% (963/112M)
[OK] Highest usage of available connections: 52% (52/100)
[OK] Key buffer size / total MyISAM indexes: 256.0M/93.0K
[OK] Key buffer hit rate: 100.0% (215K cached / 3 reads)
[OK] Query cache efficiency: 43.3% (23M cached / 54M selects)
[!!] Query cache prunes per day: 3049150
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 754K sorts)
[!!] Joins performed without indexes: 19547
[OK] Temporary tables created on disk: 0% (1K on disk / 545K total)
[OK] Thread cache hit rate: 99% (53 created / 70K connections)
[OK] Table cache hit rate: 99% (802 open / 809 opened)
[OK] Open file limit used: 1% (59/4K)
[OK] Table locks acquired immediately: 100% (72M immediate / 72M locks)
[!!] InnoDB data size / buffer pool: 899.3G/12.0G
Example 2
MySQL Version 5.1.37-community-log x86_64

Uptime = 1 days 0 hrs 2 min 31 sec
Avg. qps = 1324
Total Questions = 114605776
Threads Connected = 47

SLOW QUERIES
The slow query log is enabled.
Current long_query_time = 10.000000 sec.
You have 972 out of 114605948 that take longer than 10.000000 sec. to complete
Your long_query_time seems to be fine

BINARY UPDATE LOG
The binary update log is enabled
The expire_logs_days is not set.
The mysqld will retain the entire binary log until RESET MASTER or PURGE MASTER LOGS commands are run
manually
Setting expire_logs_days will allow you to remove old binary logs automatically
See http://dev.mysql.com/doc/refman/5.1/en/purge-master-logs.html
Binlog sync is not enabled, you could loose binlog records during a server crash

WORKER THREADS
Current thread_cache_size = 8
Current threads_cached = 4
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine
Example 2 (b)
MAX CONNECTIONS
Current max_connections = 100
Current threads_connected = 47
Historic max_used_connections = 52
The number of used connections is 52% of the configured maximum.
Your max_connections variable seems to be fine.

INNODB STATUS
Current InnoDB index space = 0 bytes
Current InnoDB data space = 0 bytes
Current InnoDB buffer pool free = 0 %
Current innodb_buffer_pool_size = 12.00 G
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 20.06 G
Configured Max Per-thread Buffers : 14.86 G
Configured Max Global Buffers : 12.33 G
Configured Max Memory Limit : 27.20 G
Physical Memory : 15.67 G

nMax memory limit exceeds 90% of physical memory
Example 2 (c)
KEY BUFFER
Current MyISAM index space = 8 K
Current key_buffer_size = 256 M
Key cache miss rate is 1 : 74223
Key buffer free ratio = 81 %
Your key_buffer_size seems to be too high.
Perhaps you can use these resources elsewhere

QUERY CACHE
Query cache is enabled
Current query_cache_size = 64 M
Current query_cache_used = 32 M
Current query_cache_limit = 2 M
Current Query cache Memory fill ratio = 51.30 %
Current query_cache_min_res_unit = 4 K
MySQL won't cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 64 M
Current read_rnd_buffer_size = 16 M
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 64.00 M
You have had 20057 queries where a join could not use an index properly
join_buffer_size >= 4 M
This is not advised
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
Example 2 (d)
OPEN FILES LIMIT
Current open_files_limit = 4096 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine

TABLE CACHE
Current table_open_cache = 1024 tables
Current table_definition_cache = 256 tables
You have a total of 0 tables
You have 843 open tables.
The table_cache value seems to be fine

TEMP TABLES
Current max_heap_table_size = 64 M
Current tmp_table_size = 256 M
Of 554783 temp tables, 0% were created on disk
Effective in-memory tmp_table_size is limited to max_heap_table_size.
Created disk tmp tables ratio seems fine

TABLE SCANS
Current read_buffer_size = 8 M
Current table scan ratio = 162 : 1
read_buffer_size seems to be fine

TABLE LOCKING
Current Lock Wait ratio = 0 : 114608692
Your table locking seems to be fine
Filesystem
●   xfs is much faster to store the binlogs and to
    delete them
Start the replication
     ●   Change to the right binlog and the right
         position before the backup

mysql> show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xxxxx
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: xxxx.000052
          Read_Master_Log_Pos: 426644104
               Relay_Log_File: mysqld1­relay­bin.000173
                Relay_Log_Pos: 426644245
        Relay_Master_Log_File: xxxx.000052
                                                             mysql> CHANGE MASTER TO MASTER_HOST='xxxxx', 
             Slave_IO_Running: Yes                           MASTER_USER='replica', MASTER_PASSWORD='xxxx', 
            Slave_SQL_Running: Yes                           MASTER_PORT=3306, MASTER_LOG_FILE='xxx.000052', 
              Replicate_Do_DB:                               MASTER_LOG_POS=426644104, 
          Replicate_Ignore_DB:                               MASTER_CONNECT_RETRY=10;
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 426644104
              Relay_Log_Space: 426644441
Monitor your production
●   While you will do any work on the database, I
    recommand you to use innotop, you will see
    directly if something is going wrong
●   Use cacti to see obviously strange behavior
Restore the backup
●   I won't explain you how to restore a backup,
    but on huge tables usually you don't have a
    dump of the data but only the files and the
    binlogs
●   So restore the last production backup you have
    on a new server (most of the time we use flash
    copies)
Dump & restore the data
●   Use maatkit parallel to dump and restore the
    data
●   While this operation, be careful to not delete
    the binlogs on te master that you will need to
    recover !
Remove historical data -
(scale back)
●   Now that you have a more performant
    database (as the indexes are rewritten) it's
    time to archive the old data
●   As the tables are still huges I don't recommand
    to do a massive delete of many rows matching
    a query
●   Use (again) maatkit to archive
ptxArchiver
●   I wrote a small script to use mk-archiver that
    follows a defined strucure to archive all the
    records linked together (foreignkeys)
$ ptxArchiver.pl ­t message ­w "timestamp < 
unix_timestamp(date_sub(now(),interval 17 
MONTH))*1000"

<tables>
   <message>
      <message>
        field=id
        p_field=origin
      </message>
   </message>
</tables>
Analyse your system
Keep the result of these
commands regularly:

iostat ­dx 5 5
vmstat 1 10
mpstat 1 10
top ­b ­n 1
cat /proc/meminfo
Analyse your MySQL
Analyse your MySQL
Check regurarly the output of these statements:
●   SHOW ENGINE INNODB STATUS;
    •   Returns information about semaphores, deadlocks, foreign
        keys, transactions, buffer pool, I/O, ...
    •   This info can be automaticaly stored in the mysql log file every
        15secs by creating the following table in mysql schema:

CREATE TABLE innodb_monitor (a INT) ENGINE=INNODB;

●   SHOW GLOBAL STATUS;
●   SHOW TABLE STATUS;
Analyse your MySQL
●   Use mk-query-digest on slow query log, binlogs
    or processlist
    •   mk-query-digest --processlist 127.0.0.1,
●   Don't forget innotop
●   mysqlresources
●   mysqlsla
●   mysqlidxchk
●   mysqlreport
AnalyZe your tables
●   ANALYZE TABLE analyzes and stores the key
    distribution for a table.
●   For InnoDB the table is locked with a write lock.
●   This statement is much faster then OPTIMIZE
    TABLE.
●   For InnoDB the statistics are updated everytime
    that 1/16 of the data has been changed. For
    huge tables it is then needed to update those
    statistics manually more often.
innotop
Partitioning
●   MySQL since 5.1 is able to manage horizontal
    partitioning (records level). Since 5.5 you can
    partition a table using multiple columns in the
    range.
●   Of course to partition the huge tables a
    relevant column should be present in it (which
    is not the case when the records are refering to
    varchar(32) :'-(
Loadays MySQL
Links
●   MySQLTuner - http://blog.mysqltuner.com/
●   tuning-primer - http://www.day32.com/MySQL/
●   ptxArchiver - http://www.lefred.be/?q=node/105
●   maatkit - http://www.maatkit.org
●   mysqlreport, mysqlsla, mysqlidxchx - http://hackmysql.com
●   mysqlresources - http://datacharmer.org/downloads/mysqlresources.zip
●   innotop -
    http://www.xaprb.com/blog/2006/07/02/innotop-mysql-innodb-monitor/
Thank you :-)

More Related Content

PDF
Tool it Up! - Session #3 - MySQL
PPT
MySQL Performance Tuning at COSCUP 2014
PDF
Designing your SaaS Database for Scale with Postgres
PDF
Really Big Elephants: PostgreSQL DW
PDF
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
PDF
What is new in PostgreSQL 14?
PDF
Evolution of MongoDB Replicaset and Its Best Practices
PDF
Managing terabytes: When PostgreSQL gets big
Tool it Up! - Session #3 - MySQL
MySQL Performance Tuning at COSCUP 2014
Designing your SaaS Database for Scale with Postgres
Really Big Elephants: PostgreSQL DW
Countdown to PostgreSQL v9.5 - Foriegn Tables can be part of Inheritance Tree
What is new in PostgreSQL 14?
Evolution of MongoDB Replicaset and Its Best Practices
Managing terabytes: When PostgreSQL gets big

What's hot (20)

PDF
Postgres in Amazon RDS
PDF
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PDF
MySQL Live Migration - Common Scenarios
PDF
PostgreSQL WAL for DBAs
PDF
Micro-batching: High-performance Writes (Adam Zegelin, Instaclustr) | Cassand...
PPTX
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
PDF
Elephants in the Cloud
PPTX
Inside SQL Server In-Memory OLTP
PPTX
Everyday I’m scaling... Cassandra
PPTX
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
PDF
Out of the box replication in postgres 9.4(pg confus)
PPTX
Load testing Cassandra applications
PDF
Overview of Postgres Utility Processes
 
PDF
Speed Up Your Existing Relational Databases with Hazelcast and Speedment
PDF
PostgreSQL 9.6 Performance-Scalability Improvements
PDF
FOSSASIA 2015 - 10 Features your developers are missing when stuck with Propr...
PDF
What is new in MariaDB 10.6?
PDF
Fortify aws aurora_proxy_2019_pleu
PDF
MySQL shell and It's utilities - Praveen GR (Mydbops Team)
PDF
PostgreSQL Replication High Availability Methods
Postgres in Amazon RDS
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
MySQL Live Migration - Common Scenarios
PostgreSQL WAL for DBAs
Micro-batching: High-performance Writes (Adam Zegelin, Instaclustr) | Cassand...
Running 400-node Cassandra + Spark Clusters in Azure (Anubhav Kale, Microsoft...
Elephants in the Cloud
Inside SQL Server In-Memory OLTP
Everyday I’m scaling... Cassandra
SQL Server In-Memory OLTP: What Every SQL Professional Should Know
Out of the box replication in postgres 9.4(pg confus)
Load testing Cassandra applications
Overview of Postgres Utility Processes
 
Speed Up Your Existing Relational Databases with Hazelcast and Speedment
PostgreSQL 9.6 Performance-Scalability Improvements
FOSSASIA 2015 - 10 Features your developers are missing when stuck with Propr...
What is new in MariaDB 10.6?
Fortify aws aurora_proxy_2019_pleu
MySQL shell and It's utilities - Praveen GR (Mydbops Team)
PostgreSQL Replication High Availability Methods
Ad

Viewers also liked (6)

PDF
Mysqlnd Query Cache
PDF
Pdt tics
PDF
Perf tuning2
PPT
Understanding MySql locking issues
PPTX
MySQL Performance Optimization #JDNL13
PDF
MySQL Manchester TT - Performance Tuning
Mysqlnd Query Cache
Pdt tics
Perf tuning2
Understanding MySql locking issues
MySQL Performance Optimization #JDNL13
MySQL Manchester TT - Performance Tuning
Ad

Similar to Loadays MySQL (20)

PDF
The Proper Care and Feeding of MySQL Databases
PDF
Webinar slides: Our Guide to MySQL & MariaDB Performance Tuning
PDF
Proper Care and Feeding of a MySQL Database for Busy Linux Administrators
PDF
The Proper Care and Feeding of a MySQL Database for Busy Linux Admins -- SCaL...
PPTX
Investigate SQL Server Memory Like Sherlock Holmes
PPTX
Oracle Performance On Linux X86 systems
PDF
Backing up Wikipedia Databases
PDF
Linuxfest Northwest Proper Care and Feeding Of a MySQL for Busy Linux Admins
PDF
Percona Live '18 Tutorial: The Accidental DBA
PDF
MariaDB Server Performance Tuning & Optimization
PDF
The Accidental DBA
PDF
Buytaert kris my_sql-pacemaker
PDF
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
PPTX
Taking Splunk to the Next Level - Architecture Breakout Session
PPTX
Intro to Azure SQL database
PDF
Running MySQL on Linux
PDF
Aioug vizag oracle12c_new_features
PPT
Sql server 2005 memory internal
PDF
The Peoper Care and Feeding of a MySQL Server for Busy Linux Admin
PDF
MySQL Performance - Best practices
The Proper Care and Feeding of MySQL Databases
Webinar slides: Our Guide to MySQL & MariaDB Performance Tuning
Proper Care and Feeding of a MySQL Database for Busy Linux Administrators
The Proper Care and Feeding of a MySQL Database for Busy Linux Admins -- SCaL...
Investigate SQL Server Memory Like Sherlock Holmes
Oracle Performance On Linux X86 systems
Backing up Wikipedia Databases
Linuxfest Northwest Proper Care and Feeding Of a MySQL for Busy Linux Admins
Percona Live '18 Tutorial: The Accidental DBA
MariaDB Server Performance Tuning & Optimization
The Accidental DBA
Buytaert kris my_sql-pacemaker
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
Taking Splunk to the Next Level - Architecture Breakout Session
Intro to Azure SQL database
Running MySQL on Linux
Aioug vizag oracle12c_new_features
Sql server 2005 memory internal
The Peoper Care and Feeding of a MySQL Server for Busy Linux Admin
MySQL Performance - Best practices

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Empathic Computing: Creating Shared Understanding
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
cuic standard and advanced reporting.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
sap open course for s4hana steps from ECC to s4
Empathic Computing: Creating Shared Understanding
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
cuic standard and advanced reporting.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx

Loadays MySQL

  • 1. MySQL : maintaining (too) big tables Frédéric Descamps
  • 3. Frédéric Descamps ● lefred on IRC and Twitter ● Senior Linux and Open Source Consultant @inuits.be ● Certified MySQL DBA since 2007 ● First GNU/Linux distro (as far as I remember): Slackware 3.0 (kernel 1.2.13) ● Blog: http://www.lefred.be
  • 4. How come we need to maintain tables ? ● A bit of history first: once upon a time, there was a developer who found a great idea
  • 5. Then he decided to create a proof of concept and he coded, coded, coded a lot...
  • 6. During the period he unfortunately didn't discuss with the DBA's and the OPS But he discussed with many other people like marketing manager, sales manager, regional manager, product manager and all were very happy and asked to code even more !
  • 7. Then he coded, coded, coded a lot...
  • 9. Why ? ● Impossible to stop de database, it's too important ! ● There is too much data, tables are huge !! • Tables > 400GB with 500M rows • Primary keys are varchar(32) ● Impossible to optimize, add an index to a table (>4h needed to add an index) ● How do we alter the schema ? ● How do we migrate to another version of MySQL ?
  • 13. Two very important points to never forget as developer : ● Don't underestimate the complexity of production environments
  • 14. Don't ! ● Never say: “but on my laptop the query is very fast”
  • 15. What now ? ● The plan is to create a new server more manageable with minimal downtime of the production • Reduce size if possible • Make queries faster ● The server will be a replica of the current production server with some different settings
  • 17. Restore last backup Ze Plan ● Prepare the new database ● Dump & restore data using maatkit databasezilla databasezilla ● Restart the restore replication ● Replace the production db by the new one (only downtime here)
  • 18. Database size ● Deleting historical data sometimes doesn't help really to save some space... ibdata won't shrink ● Data could be fragmented • To avoid the use of that huge ibdata we add this to the configuration of the new server: innodb_file_per_table
  • 19. MySQL version ● Maybe it is time to also change the MySQL version to a newer one and even to change engine ! ● I advice you to try and use XtraDB from percona. This includes corrections and interresting patches (like those from Google) • The list is available here : http://www.percona.com/docs/wiki/percona- xtradb:patch:start
  • 20. Anything else to change ? ● Maybe other settings aren't optimal for your production environment. To have an overview of them, I'd recommand the use of these two tools : • MySQLTuner • tuning-primer
  • 21. Example 1 -------- General Statistics -------------------------------------------------- [--] Skipped version check for MySQLTuner script [OK] Currently running supported MySQL version 5.1.37-community-log [OK] Operating on 64-bit architecture -------- Storage Engine Statistics ------------------------------------------- [--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster [--] Data in InnoDB tables: 899G (Tables: 149) [!!] Total fragmented tables: 82 -------- Performance Metrics ------------------------------------------------- [--] Up for: 23h 46m 1s (112M q [1K qps], 70K conn, TX: 44B, RX: 22B) [--] Reads / Writes: 51% / 49% [--] Total buffers: 12.4G global + 152.2M per thread (100 max threads) [!!] Maximum possible memory usage: 27.3G (173% of installed RAM) [OK] Slow queries: 0% (963/112M) [OK] Highest usage of available connections: 52% (52/100) [OK] Key buffer size / total MyISAM indexes: 256.0M/93.0K [OK] Key buffer hit rate: 100.0% (215K cached / 3 reads) [OK] Query cache efficiency: 43.3% (23M cached / 54M selects) [!!] Query cache prunes per day: 3049150 [OK] Sorts requiring temporary tables: 0% (0 temp sorts / 754K sorts) [!!] Joins performed without indexes: 19547 [OK] Temporary tables created on disk: 0% (1K on disk / 545K total) [OK] Thread cache hit rate: 99% (53 created / 70K connections) [OK] Table cache hit rate: 99% (802 open / 809 opened) [OK] Open file limit used: 1% (59/4K) [OK] Table locks acquired immediately: 100% (72M immediate / 72M locks) [!!] InnoDB data size / buffer pool: 899.3G/12.0G
  • 22. Example 2 MySQL Version 5.1.37-community-log x86_64 Uptime = 1 days 0 hrs 2 min 31 sec Avg. qps = 1324 Total Questions = 114605776 Threads Connected = 47 SLOW QUERIES The slow query log is enabled. Current long_query_time = 10.000000 sec. You have 972 out of 114605948 that take longer than 10.000000 sec. to complete Your long_query_time seems to be fine BINARY UPDATE LOG The binary update log is enabled The expire_logs_days is not set. The mysqld will retain the entire binary log until RESET MASTER or PURGE MASTER LOGS commands are run manually Setting expire_logs_days will allow you to remove old binary logs automatically See http://dev.mysql.com/doc/refman/5.1/en/purge-master-logs.html Binlog sync is not enabled, you could loose binlog records during a server crash WORKER THREADS Current thread_cache_size = 8 Current threads_cached = 4 Current threads_per_sec = 0 Historic threads_per_sec = 0 Your thread_cache_size is fine
  • 23. Example 2 (b) MAX CONNECTIONS Current max_connections = 100 Current threads_connected = 47 Historic max_used_connections = 52 The number of used connections is 52% of the configured maximum. Your max_connections variable seems to be fine. INNODB STATUS Current InnoDB index space = 0 bytes Current InnoDB data space = 0 bytes Current InnoDB buffer pool free = 0 % Current innodb_buffer_pool_size = 12.00 G Depending on how much space your innodb indexes take up it may be safe to increase this value to up to 2 / 3 of total system memory MEMORY USAGE Max Memory Ever Allocated : 20.06 G Configured Max Per-thread Buffers : 14.86 G Configured Max Global Buffers : 12.33 G Configured Max Memory Limit : 27.20 G Physical Memory : 15.67 G nMax memory limit exceeds 90% of physical memory
  • 24. Example 2 (c) KEY BUFFER Current MyISAM index space = 8 K Current key_buffer_size = 256 M Key cache miss rate is 1 : 74223 Key buffer free ratio = 81 % Your key_buffer_size seems to be too high. Perhaps you can use these resources elsewhere QUERY CACHE Query cache is enabled Current query_cache_size = 64 M Current query_cache_used = 32 M Current query_cache_limit = 2 M Current Query cache Memory fill ratio = 51.30 % Current query_cache_min_res_unit = 4 K MySQL won't cache query results that are larger than query_cache_limit in size SORT OPERATIONS Current sort_buffer_size = 64 M Current read_rnd_buffer_size = 16 M Sort buffer seems to be fine JOINS Current join_buffer_size = 64.00 M You have had 20057 queries where a join could not use an index properly join_buffer_size >= 4 M This is not advised You should enable "log-queries-not-using-indexes" Then look for non indexed joins in the slow query log.
  • 25. Example 2 (d) OPEN FILES LIMIT Current open_files_limit = 4096 files The open_files_limit should typically be set to at least 2x-3x that of table_cache if you have heavy MyISAM usage. Your open_files_limit value seems to be fine TABLE CACHE Current table_open_cache = 1024 tables Current table_definition_cache = 256 tables You have a total of 0 tables You have 843 open tables. The table_cache value seems to be fine TEMP TABLES Current max_heap_table_size = 64 M Current tmp_table_size = 256 M Of 554783 temp tables, 0% were created on disk Effective in-memory tmp_table_size is limited to max_heap_table_size. Created disk tmp tables ratio seems fine TABLE SCANS Current read_buffer_size = 8 M Current table scan ratio = 162 : 1 read_buffer_size seems to be fine TABLE LOCKING Current Lock Wait ratio = 0 : 114608692 Your table locking seems to be fine
  • 26. Filesystem ● xfs is much faster to store the binlogs and to delete them
  • 27. Start the replication ● Change to the right binlog and the right position before the backup mysql> show slave statusG *************************** 1. row ***************************                Slave_IO_State: Waiting for master to send event                   Master_Host: xxxxx                   Master_User: replica                   Master_Port: 3306                 Connect_Retry: 10               Master_Log_File: xxxx.000052           Read_Master_Log_Pos: 426644104                Relay_Log_File: mysqld1­relay­bin.000173                 Relay_Log_Pos: 426644245         Relay_Master_Log_File: xxxx.000052 mysql> CHANGE MASTER TO MASTER_HOST='xxxxx',               Slave_IO_Running: Yes MASTER_USER='replica', MASTER_PASSWORD='xxxx',              Slave_SQL_Running: Yes MASTER_PORT=3306, MASTER_LOG_FILE='xxx.000052',                Replicate_Do_DB:  MASTER_LOG_POS=426644104,            Replicate_Ignore_DB:  MASTER_CONNECT_RETRY=10;            Replicate_Do_Table:         Replicate_Ignore_Table:        Replicate_Wild_Do_Table:    Replicate_Wild_Ignore_Table:                     Last_Errno: 0                    Last_Error:                   Skip_Counter: 0           Exec_Master_Log_Pos: 426644104               Relay_Log_Space: 426644441
  • 28. Monitor your production ● While you will do any work on the database, I recommand you to use innotop, you will see directly if something is going wrong ● Use cacti to see obviously strange behavior
  • 29. Restore the backup ● I won't explain you how to restore a backup, but on huge tables usually you don't have a dump of the data but only the files and the binlogs ● So restore the last production backup you have on a new server (most of the time we use flash copies)
  • 30. Dump & restore the data ● Use maatkit parallel to dump and restore the data ● While this operation, be careful to not delete the binlogs on te master that you will need to recover !
  • 31. Remove historical data - (scale back) ● Now that you have a more performant database (as the indexes are rewritten) it's time to archive the old data ● As the tables are still huges I don't recommand to do a massive delete of many rows matching a query ● Use (again) maatkit to archive
  • 32. ptxArchiver ● I wrote a small script to use mk-archiver that follows a defined strucure to archive all the records linked together (foreignkeys) $ ptxArchiver.pl ­t message ­w "timestamp <  unix_timestamp(date_sub(now(),interval 17  MONTH))*1000" <tables>    <message>       <message>         field=id         p_field=origin       </message>    </message> </tables>
  • 34. Keep the result of these commands regularly: iostat ­dx 5 5 vmstat 1 10 mpstat 1 10 top ­b ­n 1 cat /proc/meminfo
  • 36. Analyse your MySQL Check regurarly the output of these statements: ● SHOW ENGINE INNODB STATUS; • Returns information about semaphores, deadlocks, foreign keys, transactions, buffer pool, I/O, ... • This info can be automaticaly stored in the mysql log file every 15secs by creating the following table in mysql schema: CREATE TABLE innodb_monitor (a INT) ENGINE=INNODB; ● SHOW GLOBAL STATUS; ● SHOW TABLE STATUS;
  • 37. Analyse your MySQL ● Use mk-query-digest on slow query log, binlogs or processlist • mk-query-digest --processlist 127.0.0.1, ● Don't forget innotop ● mysqlresources ● mysqlsla ● mysqlidxchk ● mysqlreport
  • 38. AnalyZe your tables ● ANALYZE TABLE analyzes and stores the key distribution for a table. ● For InnoDB the table is locked with a write lock. ● This statement is much faster then OPTIMIZE TABLE. ● For InnoDB the statistics are updated everytime that 1/16 of the data has been changed. For huge tables it is then needed to update those statistics manually more often.
  • 40. Partitioning ● MySQL since 5.1 is able to manage horizontal partitioning (records level). Since 5.5 you can partition a table using multiple columns in the range. ● Of course to partition the huge tables a relevant column should be present in it (which is not the case when the records are refering to varchar(32) :'-(
  • 42. Links ● MySQLTuner - http://blog.mysqltuner.com/ ● tuning-primer - http://www.day32.com/MySQL/ ● ptxArchiver - http://www.lefred.be/?q=node/105 ● maatkit - http://www.maatkit.org ● mysqlreport, mysqlsla, mysqlidxchx - http://hackmysql.com ● mysqlresources - http://datacharmer.org/downloads/mysqlresources.zip ● innotop - http://www.xaprb.com/blog/2006/07/02/innotop-mysql-innodb-monitor/