SlideShare a Scribd company logo
10x Performance Improvements
                                 in 10 steps
                               A Case Study
                                     Ronald Bradford
                                http://ronaldbradford.com

                                   FOSDEM - 2010.02
Sunday, February 7, 2010
Application
              Typical Web 2.0 social media site (Europe based)

              • Users - Visitors, Free Members, Paying Members
              • Friends
              • User Content - Video, Pictures
              • Forums, Chat, Email

Sunday, February 7, 2010
Server Environment
                 • 1 Master Database Server (MySQL 5.0.x)
                 • 3 Slave Database Servers (MySQL 5.0.x)
                 • 5 Web Servers (Apache/PHP)
                 • 1 Static Content Server (Nginx)
                 • 1 Mail Server

Sunday, February 7, 2010
Step 1


                    Monitor, Monitor, Monitor



Sunday, February 7, 2010
1. Monitor, Monitor, Monitor
              • What’s happened?
              • What’s happening now?
              • What’s going to happen?

                           Past, Present, Future

Sunday, February 7, 2010
1. Monitor, Monitor, Monitor
                                                                            Action 1
              Monitoring Software

              • Installation of Cacti http://www.cacti.net/
                                         -



              • Installation of MySQL Cacti Templates       -
                      http://code.google.com/p/mysql-cacti-templates/

              • (Optional) Installation of MONyog     -   http://www.webyog.com/




Sunday, February 7, 2010
1. Monitor, Monitor, Monitor
                                                          Action 2
              Custom Dashboard

              • Most important - The state of NOW
              • Single Page Alerts - GREEN YELLOW   RED




Sunday, February 7, 2010
Screen print goes here




                                                    Dashboard
                                                     Example
Sunday, February 7, 2010
1. Monitor, Monitor, Monitor
                                                                Action 3
              Alerting Software

              • Installation of Nagios http://www.nagios.org/
                                     -



              • MONyog also has some DB specific alerts




Sunday, February 7, 2010
1. Monitor, Monitor, Monitor
                                             Action 4
              Application Metrics

              • Total page generation time




Sunday, February 7, 2010
Step 2


                             Identify problem SQL



Sunday, February 7, 2010
2. Identify Problem SQL
              Identify SQL Statements

              • Slow Query Log
              • Processlist
              • Binary Log
              • Status Statistics

Sunday, February 7, 2010
2. Identify Problem SQL
              Problems

              • Sampling
              • Granularity
              Solution

              • tcpdump + mk-query-digest

Sunday, February 7, 2010
2. Identify Problem SQL
                                                                              Action 1
              • Install maatkit - http://www.maatkit.org
              • Install OS tcpdump (if necessary)
              • Get sudo access to tcpdump

              http://ronaldbradford.com/blog/take-a-look-at-mk-query-digest-2009-10-08/




Sunday, February 7, 2010
# Rank Query ID           Response time    Calls    R/Call       Item
         # ==== ================== ================ ======= ==========    ====
         #    1 0xB8CE56EEC1A2FBA0    14.0830 26.8%       78   0.180552   SELECT   c u
         #    2 0x195A4D6CB65C4C53     6.7800 12.9%     257    0.026381   SELECT   u
         #    3 0xCD107808735A693C     3.7355 7.1%         8   0.466943   SELECT   c u
         #    4 0xED55DD72AB650884     3.6225 6.9%        77   0.047046   SELECT   u
         #    5 0xE817EFFFF5F6FFFD     3.3616 6.4%      147    0.022868   SELECT   UNION c
         #    6 0x15FD03E7DB5F1B75     2.8842 5.5%         2   1.442116   SELECT   c u
         #    7 0x83027CD415FADB8B     2.8676 5.5%        70   0.040965   SELECT   c u
         #    8 0x1577013C472FD0C6     1.8703 3.6%        61   0.030660   SELECT   c
         #    9 0xE565A2ED3959DF4E     1.3962 2.7%         5   0.279241   SELECT   c t u
         #   10 0xE15AE2542D98CE76     1.3638 2.6%         6   0.227306   SELECT   c
         #   11 0x8A94BB83CB730494     1.2523 2.4%      148    0.008461   SELECT   hv u
         #   12 0x959C3B3A967928A6     1.1663 2.2%         5   0.233261   SELECT   c t u
         #   13 0xBC6E3F701328E95E     1.1122 2.1%         4   0.278044   SELECT   c t u




Sunday, February 7, 2010
# Query 2: 4.94 QPS, 0.13x concurrency, ID 0x195A4D6CB65C4C53 at byte 4851683
    # This item is included in the report because it matches --limit.
    #              pct   total     min     max     avg     95% stddev median
    # Count          3      257
    # Exec time     10       7s   35us   492ms    26ms   189ms    78ms    332us
    # Time range 2009-10-16 11:48:55.896978 to 2009-10-16 11:49:47.760802
    # bytes          2 10.75k       41      43   42.85   42.48    0.67    42.48
    # Errors                  1   none
    # Rows affe      0        0       0      0       0       0        0       0
    # Warning c      0        0       0      0       0       0        0       0
    # Query_time distribution
    #   1us
    # 10us #
    # 100us ################################################################
    #   1ms ####
    # 10ms ###
    # 100ms ########
    #    1s
    # 10s+
    # Tables
    #    SHOW TABLE STATUS LIKE 'u'G
    #    SHOW CREATE TABLE `u`G
    # EXPLAIN
    SELECT ... FROM u ...G
Sunday, February 7, 2010
2. Identify Problem SQL
                                                        Action 2
              • Wrappers to capture SQL
              • Re-run on single/multiple servers
                • e.g. Different slave configurations




Sunday, February 7, 2010
2. Identify Problem SQL
                                                                  Tip

              • Enable General Query Log in Development/Testing
              • Great for testing Batch Jobs




Sunday, February 7, 2010
2. Identify Problem SQL
                                                                     Action 3
              Application Logic

              • Show total master/slave SQL statements executed
              • Show all SQL with execution time (admin user only)
                                                                      Tip

              • Have abstracted class/method to execute ALL SQL

Sunday, February 7, 2010
Step 3


                             Analyze problem SQL



Sunday, February 7, 2010
3. Analyze Problem SQL
              • Query Execution Plan (QEP)
                • EXPLAIN [EXTENDED] SELECT ...
              • Table/Index Structure
                • SHOW CREATE TABLE <tablename>
              • Table Statistics
                • SHOW TABLE STATUS <tablename>
Sunday, February 7, 2010
3. Analyze Problem SQL                                Good

              mysql> EXPLAIN SELECT id FROM example_table WHERE id=1G

              *************************** 1. row ***************************
                         id: 1
                select_type: SIMPLE
                      table: example_table
                       type: const
              possible_keys: PRIMARY
                        key: PRIMARY
                    key_len: 4
                        ref: const
                       rows: 1
                      Extra: Using index


Sunday, February 7, 2010
3. Analyze Problem SQL                                Bad

                     mysql> EXPLAIN SELECT * FROM example_tableG

              *************************** 1. row ***************************
                         id: 1
                select_type: SIMPLE
                      table: example_table
                       type: ALL
              possible_keys: NULL
                        key: NULL
                    key_len: NULL
                        ref: NULL
                       rows: 59
                      Extra:


Sunday, February 7, 2010
3. Analyze Problem SQL                       Tip

              • SQL Commenting
                 • Identify batch statement SQL
                 • Identify cached SQL

                      SELECT /* Cache: 10m */ ....
                      SELECT /* Batch: EOD report */ ...
                      SELECT /* Func: 123 */   ....

Sunday, February 7, 2010
Step 4


                               The Art of Indexes



Sunday, February 7, 2010
4. The Art of Indexes
              • Different Types
                • Column
                • Concatenated
                • Covering
                • Partial
              http://ronaldbradford.com/blog/understanding-different-mysql-index-implementations-2009-07-22/

Sunday, February 7, 2010
4. The Art of Indexes
                                      Action 1
              • EXPLAIN Output
                • Possible keys
                • Key used
                • Key length
                • Using Index

Sunday, February 7, 2010
4. The Art of Indexes                     Tip

              • Generally only 1 index used per table
              • Make column NOT NULL when possible
              • Statistics affects indexes
              • Storage engines affect operations


Sunday, February 7, 2010
Before (7.88 seconds)                  After (0.04 seconds)
    *************************** 2. row **   *************************** 2. row ***
               id: 2                                   id: 2
      select_type: DEPENDENT SUBQUERY         select_type: DEPENDENT SUBQUERY
            table: h_p                              table: h_p
             type: ALL                               type: index_subquery
    possible_keys: NULL                     possible_keys: UId
              key: NULL                               key: UId
          key_len: NULL                           key_len: 4
              ref: NULL                               ref: func
             rows: 33789                             rows: 2
            Extra: Using where                      Extra: Using index



                           ALTER TABLE h_p ADD INDEX (UId);


Sunday, February 7, 2010
mysql> explain SELECT UID, FUID, COUNT(*) AS Count FROM f
                                      GROUP BY UID, FUID ORDER BY Count DESC LIMIT 2000G
                       *************************** 1. row ***************************
                                  id: 1
                         select_type: SIMPLE
                               table: f
                                type: index
                       possible_keys: NULL
                                 key: UID
                             key_len: 8
                                 ref: NULL
                                rows: 2151326
                               Extra: Using index; Using temporary; Using filesort



                    ALTER TABLE f DROP INDEX UID,
                    ADD INDEX (UID,FUID)

Sunday, February 7, 2010
4. The Art of Indexes



                           Indexes can hurt performance


Sunday, February 7, 2010
Step 5


                            Offloading Master Load



Sunday, February 7, 2010
5. Offloading Master Load
              • Identify statements for READ ONLY slave(s)
                • e.g. Long running batch statements



                     Single point v scalable solution

Sunday, February 7, 2010
Step 6


                                    Improving SQL



Sunday, February 7, 2010
6. Improving SQL
              • Poor SQL Examples
                 • ORDER BY RAND()
                 • SELECT *
                 • Lookup joins
                 • ORDER BY
                           The database is best for storing
                            and retrieving data not logic
Sunday, February 7, 2010
Step 7


                                Storage Engines



Sunday, February 7, 2010
7. Storage Engines
              • MyISAM is default
              • Table level locking
                • Concurrent SELECT statements
                • INSERT/UPDATE/DELETE blocked by long running SELECT
                • All SELECT’s blocked by INSERT/UPDATE/DELETE
              • Supports FULLTEXT
Sunday, February 7, 2010
7. Storage Engines
              • InnoDB supports transactions
              • Row level locking with MVCC
              • Does not support FULLTEXT
              • Different memory management
              • Different system variables

Sunday, February 7, 2010
7. Storage Engines
              • There are other storage engines
                • Memory
                • Archive
                • Blackhole
                • Third party

Sunday, February 7, 2010
7. Storage Engines
              Using Multiple Engines

              • Different memory management
              • Different system variables
              • Different monitoring
              • Affects backup strategy

Sunday, February 7, 2010
7. Storage Engines
                                                   Action 1
              • Configure InnoDB correctly
                • innodb_buffer_pool_size
                • innodb_log_file_size
                • innodb_flush_log_at_trx_commit



Sunday, February 7, 2010
7. Storage Engines
                                                   Action 2
              • Converted the two primary tables
                 • Users
                 • Content

                            Locking eliminated

Sunday, February 7, 2010
Step 8


                                    Caching



Sunday, February 7, 2010
8. Caching
                                                                Action 1
              • Memcache is your friend http://memcached.org/
                                       -



                • Cache query results
                • Cache lookup data (eliminate joins)
                • Cache aggregated per user information
              • Caching Page Content
                • Top rated (e.g. for 5 minutes)
Sunday, February 7, 2010
8. Caching
                                                            Action 2
              • MySQL has a Query Cache
                • Determine the real benefit
                • Turn on or off dynamically
                • SET GLOBAL query_cache_size   = 1024*1024*32;




Sunday, February 7, 2010
8. Caching                                   Tip




                              The best performance
                             improvement for an SQL
                           statement is to eliminate it.


Sunday, February 7, 2010
Step 9


                                    Sharding



Sunday, February 7, 2010
9. Sharding
              • Application level horizontal and vertical partitioning
              • Vertical Partitioning
                • Grouping like structures together (e.g. logging, forums)
              • Horizontal Partitioning
                • Affecting a smaller set of users (i.e. not 100%)

Sunday, February 7, 2010
9. Sharding
                                                                Action 1
              • Separate Logging
                 • Reduced replication load on primary server




Sunday, February 7, 2010
Step 10


                            Database Management



Sunday, February 7, 2010
10. Database Management
              Database Maintenance

              • Adding indexes (e.g. ALTER)
              • OPTIMIZE TABLE
              • Archive/purging data (e.g DELETE)

                           Blocking Operations
Sunday, February 7, 2010
10. Database Maintenance
                                                        Action 1
              • Automate slave inclusion/exclusion
              • Ability to apply DB changes to slaves
              • Master still a problem




Sunday, February 7, 2010
10. Database Maintenance
                                                           Action 2
              • Install Fail-Over Master Server
                • Slave + Master features
                • Master extra configuration
              • Scripts to switch slaves
              • Scripts to enable/disable Master(s)
              • Scripts to change application connection
Sunday, February 7, 2010
10. Database Maintenance

                              Higher Availability
                                      &
                           Testing Disaster Recovery

Sunday, February 7, 2010
Bonus




                           Front End Improvements



Sunday, February 7, 2010
11. Front End Improvements
              • Know your total website load time http://getfirebug.com/
                                                 -



                • How much time is actually database related?
              • Reduce HTML page size - 15% improvement
                • Remove full URL’s, inline css styles
              • Reduce/combine css & js files
              • Identify blocking elements (e.g. js)
Sunday, February 7, 2010
11. Front End Improvements
              • Split static content to different ServerName
              • Spread static content over multiple ServerNames (e.g. 3)
              • Sprites - Combining lightweight images http://spriteme.org/
                                                      -



              • Cookie-less domain name for static content


Sunday, February 7, 2010
Conclusion



Sunday, February 7, 2010
Before
                 • Users experienced slow or unreliable load times
                 • Management could observe, but no quantifiable details
                 • Concern over load for increased growth
                 • Release of some new features on hold


Sunday, February 7, 2010
Now
                 • Users experienced consistent load times (~60ms)
                  • Quantifiable and visible real-time results
                 • Far greater load now supported (Clients + DB)
                 • Better testability and verification for scaling
                 • New features can be deployed

Sunday, February 7, 2010
Consulting Available Now


                           http://ronaldbradford.com



Sunday, February 7, 2010

More Related Content

KEY
10x Performance Improvements
PDF
Capturing, Analyzing, and Optimizing your SQL
PDF
Summary tables with flexviews
PDF
MySQL JSON Functions
PDF
Mysql 56-experiences-bugs-solutions-50mins
PDF
MySQL 5.7 + JSON
PDF
Performance Schema for MySQL troubleshooting
PDF
Deep Dive Hands-on in Java EE 6 - Oredev 2010
10x Performance Improvements
Capturing, Analyzing, and Optimizing your SQL
Summary tables with flexviews
MySQL JSON Functions
Mysql 56-experiences-bugs-solutions-50mins
MySQL 5.7 + JSON
Performance Schema for MySQL troubleshooting
Deep Dive Hands-on in Java EE 6 - Oredev 2010

What's hot (20)

PDF
My sql tutorial-oscon-2012
PDF
Optimizing MySQL
PDF
MySQL Performance schema missing_manual_flossuk
PDF
MySQL Replication Update - DEbconf 2020 presentation
PDF
Performance schema in_my_sql_5.6_pluk2013
PDF
UKOUG 2011: Practical MySQL Tuning
PDF
Perf Tuning Best Practices
PDF
Performance Schema for MySQL Troubleshooting
PDF
Mini Session - Using GDB for Profiling
PDF
Troubleshooting MySQL Performance
PDF
Flexviews materialized views for my sql
PDF
MySQL 8.0 Operational Changes
PDF
In Search of Plan Stability - Part 1
PPT
Developing Information Schema Plugins
PDF
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
PDF
Second Step to the NoSQL Side: MySQL JSON Functions
PDF
Highload Perf Tuning
PDF
Open Source World June '21 -- JSON Within a Relational Database
DOCX
database-querry-student-note
PDF
MySQL8.0.22リリースノートをみてわいわい言う勉強会
My sql tutorial-oscon-2012
Optimizing MySQL
MySQL Performance schema missing_manual_flossuk
MySQL Replication Update - DEbconf 2020 presentation
Performance schema in_my_sql_5.6_pluk2013
UKOUG 2011: Practical MySQL Tuning
Perf Tuning Best Practices
Performance Schema for MySQL Troubleshooting
Mini Session - Using GDB for Profiling
Troubleshooting MySQL Performance
Flexviews materialized views for my sql
MySQL 8.0 Operational Changes
In Search of Plan Stability - Part 1
Developing Information Schema Plugins
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
Second Step to the NoSQL Side: MySQL JSON Functions
Highload Perf Tuning
Open Source World June '21 -- JSON Within a Relational Database
database-querry-student-note
MySQL8.0.22リリースノートをみてわいわい言う勉強会
Ad

Viewers also liked (20)

PDF
Using Apache Spark and MySQL for Data Analysis
ODP
Mysql For Developers
PDF
Capturing, Analyzing and Optimizing MySQL
PPT
淘宝数据库架构演进历程
PDF
PDF
SQL Outer Joins for Fun and Profit
PDF
MySQL High Availability and Disaster Recovery with Continuent, a VMware company
PDF
Мониторинг и отладка MySQL: максимум информации при минимальных потерях
PDF
MySQL InnoDB 源码实现分析(一)
PDF
MySQL High Availability Solutions
PPTX
Mysql参数-GDB
PDF
MySQL InnoDB Cluster - A complete High Availability solution for MySQL
PDF
MySQL High Availability with Group Replication
PDF
MySQL Replication Performance Tuning for Fun and Profit!
PDF
MySQL Group Replication - HandsOn Tutorial
PDF
MySQL innodb cluster and Group Replication in a nutshell - hands-on tutorial ...
PPTX
The nightmare of locking, blocking and isolation levels!
ODP
Mastering InnoDB Diagnostics
PPTX
MySQL aio
PPT
Mysql high availability and scalability
Using Apache Spark and MySQL for Data Analysis
Mysql For Developers
Capturing, Analyzing and Optimizing MySQL
淘宝数据库架构演进历程
SQL Outer Joins for Fun and Profit
MySQL High Availability and Disaster Recovery with Continuent, a VMware company
Мониторинг и отладка MySQL: максимум информации при минимальных потерях
MySQL InnoDB 源码实现分析(一)
MySQL High Availability Solutions
Mysql参数-GDB
MySQL InnoDB Cluster - A complete High Availability solution for MySQL
MySQL High Availability with Group Replication
MySQL Replication Performance Tuning for Fun and Profit!
MySQL Group Replication - HandsOn Tutorial
MySQL innodb cluster and Group Replication in a nutshell - hands-on tutorial ...
The nightmare of locking, blocking and isolation levels!
Mastering InnoDB Diagnostics
MySQL aio
Mysql high availability and scalability
Ad

Similar to 10x Performance Improvements - A Case Study (20)

KEY
10x improvement-mysql-100419105218-phpapp02
PDF
MySQL DW Breakfast
PDF
Databases and the Cloud
PDF
Check Please!
PDF
ISSA Siem Fraud
KEY
Perf Tuning Short
ODP
Beyond PHP - it's not (just) about the code
PDF
Intro To MongoDB
PDF
Debugging and Profiling Symfony Apps
PDF
Sql server common interview questions and answers
PDF
Novedades Denali Integration Services
PPT
System health session
PDF
MySQL optimisation Percona LeMug.fr
PDF
Join-fu: The Art of SQL Tuning for MySQL
PDF
8.4 Upcoming Features
PDF
Sql server common interview questions and answers page 5
PPTX
PDF
SplunkApplicationLoggingBestPractices_Template_2.3.pdf
PDF
How to Design Indexes, Really
PDF
Advanced Oracle Troubleshooting
10x improvement-mysql-100419105218-phpapp02
MySQL DW Breakfast
Databases and the Cloud
Check Please!
ISSA Siem Fraud
Perf Tuning Short
Beyond PHP - it's not (just) about the code
Intro To MongoDB
Debugging and Profiling Symfony Apps
Sql server common interview questions and answers
Novedades Denali Integration Services
System health session
MySQL optimisation Percona LeMug.fr
Join-fu: The Art of SQL Tuning for MySQL
8.4 Upcoming Features
Sql server common interview questions and answers page 5
SplunkApplicationLoggingBestPractices_Template_2.3.pdf
How to Design Indexes, Really
Advanced Oracle Troubleshooting

More from Ronald Bradford (20)

PDF
Successful Scalability Principles - Part 1
PDF
MySQL Backup and Recovery Essentials
PDF
The History and Future of the MySQL ecosystem
PDF
Lessons Learned Managing Large AWS Environments
PDF
Monitoring your technology stack with New Relic
PDF
MySQL Best Practices - OTN
PDF
MySQL Scalability Mistakes - OTN
PDF
My SQL Idiosyncrasies That Bite OTN
PDF
MySQL Best Practices - OTN LAD Tour
PDF
MySQL Idiosyncrasies That Bite SF
PDF
Successful MySQL Scalability
PDF
MySQL Idiosyncrasies That Bite 2010.07
PDF
MySQL Idiosyncrasies That Bite
PDF
LIFTOFF - MySQLCamp for the Oracle DBA
PDF
IGNITION - MySQLCamp for the Oracle DBA
PDF
Dolphins Now And Beyond - FOSDEM 2010
PDF
Drizzle - Status, Principles and Ecosystem
PDF
SQL v No SQL
PDF
MySQL for the Oracle DBA - Object Management
PDF
Know Your Competitor - Oracle 10g Express Edition
Successful Scalability Principles - Part 1
MySQL Backup and Recovery Essentials
The History and Future of the MySQL ecosystem
Lessons Learned Managing Large AWS Environments
Monitoring your technology stack with New Relic
MySQL Best Practices - OTN
MySQL Scalability Mistakes - OTN
My SQL Idiosyncrasies That Bite OTN
MySQL Best Practices - OTN LAD Tour
MySQL Idiosyncrasies That Bite SF
Successful MySQL Scalability
MySQL Idiosyncrasies That Bite 2010.07
MySQL Idiosyncrasies That Bite
LIFTOFF - MySQLCamp for the Oracle DBA
IGNITION - MySQLCamp for the Oracle DBA
Dolphins Now And Beyond - FOSDEM 2010
Drizzle - Status, Principles and Ecosystem
SQL v No SQL
MySQL for the Oracle DBA - Object Management
Know Your Competitor - Oracle 10g Express Edition

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
Understanding_Digital_Forensics_Presentation.pptx
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release

10x Performance Improvements - A Case Study

  • 1. 10x Performance Improvements in 10 steps A Case Study Ronald Bradford http://ronaldbradford.com FOSDEM - 2010.02 Sunday, February 7, 2010
  • 2. Application Typical Web 2.0 social media site (Europe based) • Users - Visitors, Free Members, Paying Members • Friends • User Content - Video, Pictures • Forums, Chat, Email Sunday, February 7, 2010
  • 3. Server Environment • 1 Master Database Server (MySQL 5.0.x) • 3 Slave Database Servers (MySQL 5.0.x) • 5 Web Servers (Apache/PHP) • 1 Static Content Server (Nginx) • 1 Mail Server Sunday, February 7, 2010
  • 4. Step 1 Monitor, Monitor, Monitor Sunday, February 7, 2010
  • 5. 1. Monitor, Monitor, Monitor • What’s happened? • What’s happening now? • What’s going to happen? Past, Present, Future Sunday, February 7, 2010
  • 6. 1. Monitor, Monitor, Monitor Action 1 Monitoring Software • Installation of Cacti http://www.cacti.net/ - • Installation of MySQL Cacti Templates - http://code.google.com/p/mysql-cacti-templates/ • (Optional) Installation of MONyog - http://www.webyog.com/ Sunday, February 7, 2010
  • 7. 1. Monitor, Monitor, Monitor Action 2 Custom Dashboard • Most important - The state of NOW • Single Page Alerts - GREEN YELLOW RED Sunday, February 7, 2010
  • 8. Screen print goes here Dashboard Example Sunday, February 7, 2010
  • 9. 1. Monitor, Monitor, Monitor Action 3 Alerting Software • Installation of Nagios http://www.nagios.org/ - • MONyog also has some DB specific alerts Sunday, February 7, 2010
  • 10. 1. Monitor, Monitor, Monitor Action 4 Application Metrics • Total page generation time Sunday, February 7, 2010
  • 11. Step 2 Identify problem SQL Sunday, February 7, 2010
  • 12. 2. Identify Problem SQL Identify SQL Statements • Slow Query Log • Processlist • Binary Log • Status Statistics Sunday, February 7, 2010
  • 13. 2. Identify Problem SQL Problems • Sampling • Granularity Solution • tcpdump + mk-query-digest Sunday, February 7, 2010
  • 14. 2. Identify Problem SQL Action 1 • Install maatkit - http://www.maatkit.org • Install OS tcpdump (if necessary) • Get sudo access to tcpdump http://ronaldbradford.com/blog/take-a-look-at-mk-query-digest-2009-10-08/ Sunday, February 7, 2010
  • 15. # Rank Query ID Response time Calls R/Call Item # ==== ================== ================ ======= ========== ==== # 1 0xB8CE56EEC1A2FBA0 14.0830 26.8% 78 0.180552 SELECT c u # 2 0x195A4D6CB65C4C53 6.7800 12.9% 257 0.026381 SELECT u # 3 0xCD107808735A693C 3.7355 7.1% 8 0.466943 SELECT c u # 4 0xED55DD72AB650884 3.6225 6.9% 77 0.047046 SELECT u # 5 0xE817EFFFF5F6FFFD 3.3616 6.4% 147 0.022868 SELECT UNION c # 6 0x15FD03E7DB5F1B75 2.8842 5.5% 2 1.442116 SELECT c u # 7 0x83027CD415FADB8B 2.8676 5.5% 70 0.040965 SELECT c u # 8 0x1577013C472FD0C6 1.8703 3.6% 61 0.030660 SELECT c # 9 0xE565A2ED3959DF4E 1.3962 2.7% 5 0.279241 SELECT c t u # 10 0xE15AE2542D98CE76 1.3638 2.6% 6 0.227306 SELECT c # 11 0x8A94BB83CB730494 1.2523 2.4% 148 0.008461 SELECT hv u # 12 0x959C3B3A967928A6 1.1663 2.2% 5 0.233261 SELECT c t u # 13 0xBC6E3F701328E95E 1.1122 2.1% 4 0.278044 SELECT c t u Sunday, February 7, 2010
  • 16. # Query 2: 4.94 QPS, 0.13x concurrency, ID 0x195A4D6CB65C4C53 at byte 4851683 # This item is included in the report because it matches --limit. # pct total min max avg 95% stddev median # Count 3 257 # Exec time 10 7s 35us 492ms 26ms 189ms 78ms 332us # Time range 2009-10-16 11:48:55.896978 to 2009-10-16 11:49:47.760802 # bytes 2 10.75k 41 43 42.85 42.48 0.67 42.48 # Errors 1 none # Rows affe 0 0 0 0 0 0 0 0 # Warning c 0 0 0 0 0 0 0 0 # Query_time distribution # 1us # 10us # # 100us ################################################################ # 1ms #### # 10ms ### # 100ms ######## # 1s # 10s+ # Tables # SHOW TABLE STATUS LIKE 'u'G # SHOW CREATE TABLE `u`G # EXPLAIN SELECT ... FROM u ...G Sunday, February 7, 2010
  • 17. 2. Identify Problem SQL Action 2 • Wrappers to capture SQL • Re-run on single/multiple servers • e.g. Different slave configurations Sunday, February 7, 2010
  • 18. 2. Identify Problem SQL Tip • Enable General Query Log in Development/Testing • Great for testing Batch Jobs Sunday, February 7, 2010
  • 19. 2. Identify Problem SQL Action 3 Application Logic • Show total master/slave SQL statements executed • Show all SQL with execution time (admin user only) Tip • Have abstracted class/method to execute ALL SQL Sunday, February 7, 2010
  • 20. Step 3 Analyze problem SQL Sunday, February 7, 2010
  • 21. 3. Analyze Problem SQL • Query Execution Plan (QEP) • EXPLAIN [EXTENDED] SELECT ... • Table/Index Structure • SHOW CREATE TABLE <tablename> • Table Statistics • SHOW TABLE STATUS <tablename> Sunday, February 7, 2010
  • 22. 3. Analyze Problem SQL Good mysql> EXPLAIN SELECT id FROM example_table WHERE id=1G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: example_table type: const possible_keys: PRIMARY key: PRIMARY key_len: 4 ref: const rows: 1 Extra: Using index Sunday, February 7, 2010
  • 23. 3. Analyze Problem SQL Bad mysql> EXPLAIN SELECT * FROM example_tableG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: example_table type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 59 Extra: Sunday, February 7, 2010
  • 24. 3. Analyze Problem SQL Tip • SQL Commenting • Identify batch statement SQL • Identify cached SQL SELECT /* Cache: 10m */ .... SELECT /* Batch: EOD report */ ... SELECT /* Func: 123 */ .... Sunday, February 7, 2010
  • 25. Step 4 The Art of Indexes Sunday, February 7, 2010
  • 26. 4. The Art of Indexes • Different Types • Column • Concatenated • Covering • Partial http://ronaldbradford.com/blog/understanding-different-mysql-index-implementations-2009-07-22/ Sunday, February 7, 2010
  • 27. 4. The Art of Indexes Action 1 • EXPLAIN Output • Possible keys • Key used • Key length • Using Index Sunday, February 7, 2010
  • 28. 4. The Art of Indexes Tip • Generally only 1 index used per table • Make column NOT NULL when possible • Statistics affects indexes • Storage engines affect operations Sunday, February 7, 2010
  • 29. Before (7.88 seconds) After (0.04 seconds) *************************** 2. row ** *************************** 2. row *** id: 2 id: 2 select_type: DEPENDENT SUBQUERY select_type: DEPENDENT SUBQUERY table: h_p table: h_p type: ALL type: index_subquery possible_keys: NULL possible_keys: UId key: NULL key: UId key_len: NULL key_len: 4 ref: NULL ref: func rows: 33789 rows: 2 Extra: Using where Extra: Using index ALTER TABLE h_p ADD INDEX (UId); Sunday, February 7, 2010
  • 30. mysql> explain SELECT UID, FUID, COUNT(*) AS Count FROM f GROUP BY UID, FUID ORDER BY Count DESC LIMIT 2000G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: f type: index possible_keys: NULL key: UID key_len: 8 ref: NULL rows: 2151326 Extra: Using index; Using temporary; Using filesort ALTER TABLE f DROP INDEX UID, ADD INDEX (UID,FUID) Sunday, February 7, 2010
  • 31. 4. The Art of Indexes Indexes can hurt performance Sunday, February 7, 2010
  • 32. Step 5 Offloading Master Load Sunday, February 7, 2010
  • 33. 5. Offloading Master Load • Identify statements for READ ONLY slave(s) • e.g. Long running batch statements Single point v scalable solution Sunday, February 7, 2010
  • 34. Step 6 Improving SQL Sunday, February 7, 2010
  • 35. 6. Improving SQL • Poor SQL Examples • ORDER BY RAND() • SELECT * • Lookup joins • ORDER BY The database is best for storing and retrieving data not logic Sunday, February 7, 2010
  • 36. Step 7 Storage Engines Sunday, February 7, 2010
  • 37. 7. Storage Engines • MyISAM is default • Table level locking • Concurrent SELECT statements • INSERT/UPDATE/DELETE blocked by long running SELECT • All SELECT’s blocked by INSERT/UPDATE/DELETE • Supports FULLTEXT Sunday, February 7, 2010
  • 38. 7. Storage Engines • InnoDB supports transactions • Row level locking with MVCC • Does not support FULLTEXT • Different memory management • Different system variables Sunday, February 7, 2010
  • 39. 7. Storage Engines • There are other storage engines • Memory • Archive • Blackhole • Third party Sunday, February 7, 2010
  • 40. 7. Storage Engines Using Multiple Engines • Different memory management • Different system variables • Different monitoring • Affects backup strategy Sunday, February 7, 2010
  • 41. 7. Storage Engines Action 1 • Configure InnoDB correctly • innodb_buffer_pool_size • innodb_log_file_size • innodb_flush_log_at_trx_commit Sunday, February 7, 2010
  • 42. 7. Storage Engines Action 2 • Converted the two primary tables • Users • Content Locking eliminated Sunday, February 7, 2010
  • 43. Step 8 Caching Sunday, February 7, 2010
  • 44. 8. Caching Action 1 • Memcache is your friend http://memcached.org/ - • Cache query results • Cache lookup data (eliminate joins) • Cache aggregated per user information • Caching Page Content • Top rated (e.g. for 5 minutes) Sunday, February 7, 2010
  • 45. 8. Caching Action 2 • MySQL has a Query Cache • Determine the real benefit • Turn on or off dynamically • SET GLOBAL query_cache_size = 1024*1024*32; Sunday, February 7, 2010
  • 46. 8. Caching Tip The best performance improvement for an SQL statement is to eliminate it. Sunday, February 7, 2010
  • 47. Step 9 Sharding Sunday, February 7, 2010
  • 48. 9. Sharding • Application level horizontal and vertical partitioning • Vertical Partitioning • Grouping like structures together (e.g. logging, forums) • Horizontal Partitioning • Affecting a smaller set of users (i.e. not 100%) Sunday, February 7, 2010
  • 49. 9. Sharding Action 1 • Separate Logging • Reduced replication load on primary server Sunday, February 7, 2010
  • 50. Step 10 Database Management Sunday, February 7, 2010
  • 51. 10. Database Management Database Maintenance • Adding indexes (e.g. ALTER) • OPTIMIZE TABLE • Archive/purging data (e.g DELETE) Blocking Operations Sunday, February 7, 2010
  • 52. 10. Database Maintenance Action 1 • Automate slave inclusion/exclusion • Ability to apply DB changes to slaves • Master still a problem Sunday, February 7, 2010
  • 53. 10. Database Maintenance Action 2 • Install Fail-Over Master Server • Slave + Master features • Master extra configuration • Scripts to switch slaves • Scripts to enable/disable Master(s) • Scripts to change application connection Sunday, February 7, 2010
  • 54. 10. Database Maintenance Higher Availability & Testing Disaster Recovery Sunday, February 7, 2010
  • 55. Bonus Front End Improvements Sunday, February 7, 2010
  • 56. 11. Front End Improvements • Know your total website load time http://getfirebug.com/ - • How much time is actually database related? • Reduce HTML page size - 15% improvement • Remove full URL’s, inline css styles • Reduce/combine css & js files • Identify blocking elements (e.g. js) Sunday, February 7, 2010
  • 57. 11. Front End Improvements • Split static content to different ServerName • Spread static content over multiple ServerNames (e.g. 3) • Sprites - Combining lightweight images http://spriteme.org/ - • Cookie-less domain name for static content Sunday, February 7, 2010
  • 59. Before • Users experienced slow or unreliable load times • Management could observe, but no quantifiable details • Concern over load for increased growth • Release of some new features on hold Sunday, February 7, 2010
  • 60. Now • Users experienced consistent load times (~60ms) • Quantifiable and visible real-time results • Far greater load now supported (Clients + DB) • Better testability and verification for scaling • New features can be deployed Sunday, February 7, 2010
  • 61. Consulting Available Now http://ronaldbradford.com Sunday, February 7, 2010