SlideShare a Scribd company logo
1 of 20
Download to read offline
Two database findings
        Tobias Schmidt
          @dagrobie
      github.com/grobie




                          Lightning talk @ Railsberry, Kraków - 19 April 2012
Know your usage patterns
pt-query-digest

# Profile
# Rank Query ID             Response time Calls R/Call Apdx V/M    Item
# ==== ==================   ============== ===== ====== ==== ===== =========
#    1 0x5E796D5A4A7D1CA9   449.4200 40.6% 4763 0.0944 0.99 1.32 ADMIN STATISTICS
#    2 0xDEA06A23CC92009D   101.0927 9.1%    352 0.2872 0.95 1.00 SELECT users
#    3 0xE320654019E45018    99.4647 9.0%    648 0.1535 0.98 1.15 SELECT users
#    4 0x813031B8BBC3B329    28.6658 2.6%    192 0.1493 0.99 16.10 COMMIT
#    5 0xB7CFCFF53D7D16A7    25.2505 2.3%    278 0.0908 0.99 1.90 SELECT oauth2...
# ...
pt-query-digest

# Profile
# Rank Query ID             Response time Calls R/Call Apdx V/M    Item
# ==== ==================   ============== ===== ====== ==== ===== =========
#    1 0x5E796D5A4A7D1CA9   449.4200 40.6% 4763 0.0944 0.99 1.32 ADMIN STATISTICS
#    2 0xDEA06A23CC92009D   101.0927 9.1%    352 0.2872 0.95 1.00 SELECT users
#    3 0xE320654019E45018    99.4647 9.0%    648 0.1535 0.98 1.15 SELECT users
#    4 0x813031B8BBC3B329    28.6658 2.6%    192 0.1493 0.99 16.10 COMMIT
#    5 0xB7CFCFF53D7D16A7    25.2505 2.3%    278 0.0908 0.99 1.90 SELECT oauth2...
# ...



              40% of the time for
             “ADMIN STATISTICS”
COM_STATISTICS

Asks the MySQL server to compile a text
message with some server statistics (uptime,
queries per second, etc.).This packet can be sent
with mysqladmin status. No arguments.

$ mysqladmin status
Uptime: 41729399 Threads: 892 Questions: ... Slow queries: ... ...



                      http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#COM_STATISTICS
ActiveRecord
# active_record/connection_adapters/mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::MysqlAdapter
  def active?
    @connection.stat

    if @connection.respond_to?(:errno)
      @connection.errno.zero?
    else
      true
    end
  end
end
ActiveRecord
# ar/connection_adapters/abstract_adapter.rb
class AR::ConnectionAdapters::AbstractAdapter
  # Checks whether the connection to the database is
  # still active (i.e. not stale). This is done under
  # the hood by calling <tt>active?</tt>.
  # If the connection is no longer active, then this
  # method will reconnect to the database.
  def verify!(*ignored)
    reconnect! unless active?
  end
end
ActiveRecord

# action_controller/dispatcher.rb
class ActionController::Dispatcher
  def self.cleanup_application
    # ...

    ActiveRecord::Base.clear_reloadable_connections!
  end
end
Optimistic health check

# config/initializers/active_record_extensions.rb
class ActiveRecord::ConnectionAdapters::MysqlAdapter
  def active?
    true
  end
end




 additional info: http://www.mysqlperformanceblog.com/2010/05/05/checking-for-a-live-database-connection-considered-harmful/
Know your tools
Lhm
       Large hadron migrator
github.com/soundcloud/large-hadron-migrator
Lhm

• Online schema change tool
• migrate tables with (almost) no downtime
• Replication safe
• Ruby 1.8 / 1.9, ActiveRecord 2.3 / 3.x and
  mysql / mysql2 compatible
Lhm


users
Lhm - prepare

            create temporary table


users            lhmn_users
Lhm - alter

require "lhm"
                                                         alter table
class AddRailsberryToUsers < ActiveRecord::Migration
  def up
    Lhm.change_table :users do |u|
      u.add_column :railsberry, "BOOLEAN DEFAULT true"   lhmn_users
      u.add_index :railsberry
    end
  end
end
Lhm - entangle


        create trigger
users                    lhmn_users
         after insert
         after update
         after delete
Lhm - copy


          copy all rows
users                     lhmn_users
Lhm - rename


             rename
lhma_users            users
Know your usage patterns
    Know your tools
soundcloud.com/jobs

More Related Content

Viewers also liked (10)

Resume 09 3
Resume 09 3Resume 09 3
Resume 09 3
 
Conectivismo
ConectivismoConectivismo
Conectivismo
 
High Frequency Words 1
High Frequency Words 1High Frequency Words 1
High Frequency Words 1
 
Greece bilateral report
Greece bilateral reportGreece bilateral report
Greece bilateral report
 
Shapes presentation
Shapes presentationShapes presentation
Shapes presentation
 
Cz pl meeting
Cz  pl meetingCz  pl meeting
Cz pl meeting
 
Sistemas Operativos
Sistemas OperativosSistemas Operativos
Sistemas Operativos
 
11704
1170411704
11704
 
Algo grande
Algo grandeAlgo grande
Algo grande
 
البورصة المصرية | شركة عربية اون لاين | التحليل الفني | 8-2-2017 | بورصة | ...
البورصة المصرية |  شركة عربية اون لاين | التحليل الفني |  8-2-2017 | بورصة | ...البورصة المصرية |  شركة عربية اون لاين | التحليل الفني |  8-2-2017 | بورصة | ...
البورصة المصرية | شركة عربية اون لاين | التحليل الفني | 8-2-2017 | بورصة | ...
 

Similar to Two database findings

Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails Mohit Jain
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 
Palestra "Do PHP ao Rails" - FOCAI
Palestra "Do PHP ao Rails" - FOCAIPalestra "Do PHP ao Rails" - FOCAI
Palestra "Do PHP ao Rails" - FOCAIAlmir Mendes
 
Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deploymentsOdoo
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2rowensCap
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Plataformatec
 
Rooted 2010 ppp
Rooted 2010 pppRooted 2010 ppp
Rooted 2010 pppnoc_313
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperfNew Relic
 
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...MITRE - ATT&CKcon
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiMuhammed Thanveer M
 

Similar to Two database findings (20)

Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
Palestra "Do PHP ao Rails" - FOCAI
Palestra "Do PHP ao Rails" - FOCAIPalestra "Do PHP ao Rails" - FOCAI
Palestra "Do PHP ao Rails" - FOCAI
 
Rails3 way
Rails3 wayRails3 way
Rails3 way
 
Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deployments
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Ruby meetup-dry
Ruby meetup-dryRuby meetup-dry
Ruby meetup-dry
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
Rooted 2010 ppp
Rooted 2010 pppRooted 2010 ppp
Rooted 2010 ppp
 
Undrop for InnoDB
Undrop for InnoDBUndrop for InnoDB
Undrop for InnoDB
 
Wikilims Road4
Wikilims Road4Wikilims Road4
Wikilims Road4
 
Intro to-rails-webperf
Intro to-rails-webperfIntro to-rails-webperf
Intro to-rails-webperf
 
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...
MITRE ATT&CKcon 2018: From Technique to Detection, Paul Ewing and Ross Wolf, ...
 
Pl sql using_xml
Pl sql using_xmlPl sql using_xml
Pl sql using_xml
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Sherlock holmes for dba’s
Sherlock holmes for dba’sSherlock holmes for dba’s
Sherlock holmes for dba’s
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Stored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayiStored procedures by thanveer danish melayi
Stored procedures by thanveer danish melayi
 

More from Tobias Schmidt

Monitoring microservices with Prometheus
Monitoring microservices with PrometheusMonitoring microservices with Prometheus
Monitoring microservices with PrometheusTobias Schmidt
 
Monitoring Kubernetes with Prometheus
Monitoring Kubernetes with PrometheusMonitoring Kubernetes with Prometheus
Monitoring Kubernetes with PrometheusTobias Schmidt
 
The history of Prometheus at SoundCloud
The history of Prometheus at SoundCloudThe history of Prometheus at SoundCloud
The history of Prometheus at SoundCloudTobias Schmidt
 
Efficient monitoring and alerting
Efficient monitoring and alertingEfficient monitoring and alerting
Efficient monitoring and alertingTobias Schmidt
 
Moving to Kubernetes - Tales from SoundCloud
Moving to Kubernetes - Tales from SoundCloudMoving to Kubernetes - Tales from SoundCloud
Moving to Kubernetes - Tales from SoundCloudTobias Schmidt
 
Prometheus loves Grafana
Prometheus loves GrafanaPrometheus loves Grafana
Prometheus loves GrafanaTobias Schmidt
 
16 months @ SoundCloud
16 months @ SoundCloud16 months @ SoundCloud
16 months @ SoundCloudTobias Schmidt
 

More from Tobias Schmidt (7)

Monitoring microservices with Prometheus
Monitoring microservices with PrometheusMonitoring microservices with Prometheus
Monitoring microservices with Prometheus
 
Monitoring Kubernetes with Prometheus
Monitoring Kubernetes with PrometheusMonitoring Kubernetes with Prometheus
Monitoring Kubernetes with Prometheus
 
The history of Prometheus at SoundCloud
The history of Prometheus at SoundCloudThe history of Prometheus at SoundCloud
The history of Prometheus at SoundCloud
 
Efficient monitoring and alerting
Efficient monitoring and alertingEfficient monitoring and alerting
Efficient monitoring and alerting
 
Moving to Kubernetes - Tales from SoundCloud
Moving to Kubernetes - Tales from SoundCloudMoving to Kubernetes - Tales from SoundCloud
Moving to Kubernetes - Tales from SoundCloud
 
Prometheus loves Grafana
Prometheus loves GrafanaPrometheus loves Grafana
Prometheus loves Grafana
 
16 months @ SoundCloud
16 months @ SoundCloud16 months @ SoundCloud
16 months @ SoundCloud
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Two database findings

  • 1. Two database findings Tobias Schmidt @dagrobie github.com/grobie Lightning talk @ Railsberry, Kraków - 19 April 2012
  • 2. Know your usage patterns
  • 3. pt-query-digest # Profile # Rank Query ID Response time Calls R/Call Apdx V/M Item # ==== ================== ============== ===== ====== ==== ===== ========= # 1 0x5E796D5A4A7D1CA9 449.4200 40.6% 4763 0.0944 0.99 1.32 ADMIN STATISTICS # 2 0xDEA06A23CC92009D 101.0927 9.1% 352 0.2872 0.95 1.00 SELECT users # 3 0xE320654019E45018 99.4647 9.0% 648 0.1535 0.98 1.15 SELECT users # 4 0x813031B8BBC3B329 28.6658 2.6% 192 0.1493 0.99 16.10 COMMIT # 5 0xB7CFCFF53D7D16A7 25.2505 2.3% 278 0.0908 0.99 1.90 SELECT oauth2... # ...
  • 4. pt-query-digest # Profile # Rank Query ID Response time Calls R/Call Apdx V/M Item # ==== ================== ============== ===== ====== ==== ===== ========= # 1 0x5E796D5A4A7D1CA9 449.4200 40.6% 4763 0.0944 0.99 1.32 ADMIN STATISTICS # 2 0xDEA06A23CC92009D 101.0927 9.1% 352 0.2872 0.95 1.00 SELECT users # 3 0xE320654019E45018 99.4647 9.0% 648 0.1535 0.98 1.15 SELECT users # 4 0x813031B8BBC3B329 28.6658 2.6% 192 0.1493 0.99 16.10 COMMIT # 5 0xB7CFCFF53D7D16A7 25.2505 2.3% 278 0.0908 0.99 1.90 SELECT oauth2... # ... 40% of the time for “ADMIN STATISTICS”
  • 5. COM_STATISTICS Asks the MySQL server to compile a text message with some server statistics (uptime, queries per second, etc.).This packet can be sent with mysqladmin status. No arguments. $ mysqladmin status Uptime: 41729399 Threads: 892 Questions: ... Slow queries: ... ... http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#COM_STATISTICS
  • 6. ActiveRecord # active_record/connection_adapters/mysql_adapter.rb class ActiveRecord::ConnectionAdapters::MysqlAdapter def active? @connection.stat if @connection.respond_to?(:errno) @connection.errno.zero? else true end end end
  • 7. ActiveRecord # ar/connection_adapters/abstract_adapter.rb class AR::ConnectionAdapters::AbstractAdapter # Checks whether the connection to the database is # still active (i.e. not stale). This is done under # the hood by calling <tt>active?</tt>. # If the connection is no longer active, then this # method will reconnect to the database. def verify!(*ignored) reconnect! unless active? end end
  • 8. ActiveRecord # action_controller/dispatcher.rb class ActionController::Dispatcher def self.cleanup_application # ... ActiveRecord::Base.clear_reloadable_connections! end end
  • 9. Optimistic health check # config/initializers/active_record_extensions.rb class ActiveRecord::ConnectionAdapters::MysqlAdapter def active? true end end additional info: http://www.mysqlperformanceblog.com/2010/05/05/checking-for-a-live-database-connection-considered-harmful/
  • 11. Lhm Large hadron migrator github.com/soundcloud/large-hadron-migrator
  • 12. Lhm • Online schema change tool • migrate tables with (almost) no downtime • Replication safe • Ruby 1.8 / 1.9, ActiveRecord 2.3 / 3.x and mysql / mysql2 compatible
  • 14. Lhm - prepare create temporary table users lhmn_users
  • 15. Lhm - alter require "lhm" alter table class AddRailsberryToUsers < ActiveRecord::Migration def up Lhm.change_table :users do |u| u.add_column :railsberry, "BOOLEAN DEFAULT true" lhmn_users u.add_index :railsberry end end end
  • 16. Lhm - entangle create trigger users lhmn_users after insert after update after delete
  • 17. Lhm - copy copy all rows users lhmn_users
  • 18. Lhm - rename rename lhma_users users
  • 19. Know your usage patterns Know your tools