Two database findings

963 views
869 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
963
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
3
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Two database findings

  1. 1. Two database findings Tobias Schmidt @dagrobie github.com/grobie Lightning talk @ Railsberry, Kraków - 19 April 2012
  2. 2. Know your usage patterns
  3. 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. 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. 5. COM_STATISTICSAsks the MySQL server to compile a textmessage with some server statistics (uptime,queries per second, etc.).This packet can be sentwith mysqladmin status. No arguments.$ mysqladmin statusUptime: 41729399 Threads: 892 Questions: ... Slow queries: ... ... http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#COM_STATISTICS
  6. 6. ActiveRecord# active_record/connection_adapters/mysql_adapter.rbclass ActiveRecord::ConnectionAdapters::MysqlAdapter def active? @connection.stat if @connection.respond_to?(:errno) @connection.errno.zero? else true end endend
  7. 7. ActiveRecord# ar/connection_adapters/abstract_adapter.rbclass 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? endend
  8. 8. ActiveRecord# action_controller/dispatcher.rbclass ActionController::Dispatcher def self.cleanup_application # ... ActiveRecord::Base.clear_reloadable_connections! endend
  9. 9. Optimistic health check# config/initializers/active_record_extensions.rbclass ActiveRecord::ConnectionAdapters::MysqlAdapter def active? true endend additional info: http://www.mysqlperformanceblog.com/2010/05/05/checking-for-a-live-database-connection-considered-harmful/
  10. 10. Know your tools
  11. 11. Lhm Large hadron migratorgithub.com/soundcloud/large-hadron-migrator
  12. 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
  13. 13. Lhmusers
  14. 14. Lhm - prepare create temporary tableusers lhmn_users
  15. 15. Lhm - alterrequire "lhm" alter tableclass 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 endend
  16. 16. Lhm - entangle create triggerusers lhmn_users after insert after update after delete
  17. 17. Lhm - copy copy all rowsusers lhmn_users
  18. 18. Lhm - rename renamelhma_users users
  19. 19. Know your usage patterns Know your tools
  20. 20. soundcloud.com/jobs

×