20
Two database findings Tobias Schmidt @dagrobie github.com/grobie Lightning talk @ Railsberry, Kraków - 19 April 2012

Two database findings

Embed Size (px)

Citation preview

Two database findingsTobias Schmidt

@dagrobiegithub.com/grobie

Lightning talk @ Railsberry, Kraków - 19 April 2012

Know your usage patterns

# 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...# ...

pt-query-digest

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.rbclass ActiveRecord::ConnectionAdapters::MysqlAdapter def active? @connection.stat

if @connection.respond_to?(:errno) @connection.errno.zero? else true end endend

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

ActiveRecord

# action_controller/dispatcher.rbclass ActionController::Dispatcher def self.cleanup_application # ... ActiveRecord::Base.clear_reloadable_connections! endend

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/

Know your tools

LhmLarge 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

users lhmn_users

create temporary table

Lhm - alter

lhmn_users

alter tablerequire "lhm"

class AddRailsberryToUsers < ActiveRecord::Migration def up Lhm.change_table :users do |u| u.add_column :railsberry, "BOOLEAN DEFAULT true" u.add_index :railsberry end endend

Lhm - entangle

users lhmn_userscreate trigger

after insertafter updateafter delete

Lhm - copy

users lhmn_userscopy all rows

Lhm - rename

lhma_users usersrename

Know your usage patterns

Know your tools

soundcloud.com/jobs