Class: MysqlExecutionStep

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Cutie::Tracker::Hook::Abstract, DataMapper::Resource
Defined in:
lib/dm-cutie-extras/hooks/mysql_execution_step.rb

Overview

Tracker Hook for recording mysql query plans

Constant Summary collapse

STATEMENT =
"EXPLAIN EXTENDED %s"

Class Method Summary collapse

Class Method Details

.execute(target_repo, query_str) ⇒ Object



28
29
30
# File 'lib/dm-cutie-extras/hooks/mysql_execution_step.rb', line 28

def self.execute(target_repo, query_str)
  repository(target_repo.to_sym).adapter.query MysqlExecutionStep::STATEMENT % query_str
end

.hook_nameObject



24
# File 'lib/dm-cutie-extras/hooks/mysql_execution_step.rb', line 24

def self.hook_name;            "MySQL Execution Steps"  end

.supported_adaptersObject



26
# File 'lib/dm-cutie-extras/hooks/mysql_execution_step.rb', line 26

def self.supported_adapters;   [:mysql];           end

.supported_statementsObject



25
# File 'lib/dm-cutie-extras/hooks/mysql_execution_step.rb', line 25

def self.supported_statements; [:select];          end

.track(executed_query) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dm-cutie-extras/hooks/mysql_execution_step.rb', line 32

def self.track( executed_query )
  # Perform the EXPLAIN against the original target repo  
  raw_query_plan = MysqlExecutionStep.execute( 
    executed_query.generalized_query.primary_storage.repo_name,
    executed_query.statement 
  )

  #Store the MysqlExecutionSteps in the DM Cutie repo
  DataMapper::Cutie.repo do
    raw_query_plan.each_with_index do |raw_query_step, idx| 
      attribs = raw_query_step.attributes
      attribs.delete(:id) #get rid of the rowID from MySQL
      attribs[:used_key]      = attribs.delete(:key)
      attribs[:used_key_len]  = attribs.delete(:key_len)
      query_step = MysqlExecutionStep.new(attribs)
      query_step.order = idx + 1
      query_step.executed_query = executed_query

      query_step.save
    end
  end
end