Class: Dexter::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/dexter/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement, fingerprint = nil) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
12
13
14
15
# File 'lib/dexter/query.rb', line 7

def initialize(statement, fingerprint = nil)
  @statement = statement
  unless fingerprint
    fingerprint = PgQuery.fingerprint(statement) rescue "unknown"
  end
  @fingerprint = fingerprint
  @plans = []
  @tables_from_views = []
end

Instance Attribute Details

#callsObject

Returns the value of attribute calls.



5
6
7
# File 'lib/dexter/query.rb', line 5

def calls
  @calls
end

#candidate_tablesObject

Returns the value of attribute candidate_tables.



5
6
7
# File 'lib/dexter/query.rb', line 5

def candidate_tables
  @candidate_tables
end

#candidatesObject

Returns the value of attribute candidates.



5
6
7
# File 'lib/dexter/query.rb', line 5

def candidates
  @candidates
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



3
4
5
# File 'lib/dexter/query.rb', line 3

def fingerprint
  @fingerprint
end

#indexesObject

Returns the value of attribute indexes.



5
6
7
# File 'lib/dexter/query.rb', line 5

def indexes
  @indexes
end

#missing_tablesObject

Returns the value of attribute missing_tables.



5
6
7
# File 'lib/dexter/query.rb', line 5

def missing_tables
  @missing_tables
end

#new_costObject

Returns the value of attribute new_cost.



5
6
7
# File 'lib/dexter/query.rb', line 5

def new_cost
  @new_cost
end

#pass1_indexesObject

Returns the value of attribute pass1_indexes.



5
6
7
# File 'lib/dexter/query.rb', line 5

def pass1_indexes
  @pass1_indexes
end

#pass2_indexesObject

Returns the value of attribute pass2_indexes.



5
6
7
# File 'lib/dexter/query.rb', line 5

def pass2_indexes
  @pass2_indexes
end

#pass3_indexesObject

Returns the value of attribute pass3_indexes.



5
6
7
# File 'lib/dexter/query.rb', line 5

def pass3_indexes
  @pass3_indexes
end

#plansObject (readonly)

Returns the value of attribute plans.



3
4
5
# File 'lib/dexter/query.rb', line 3

def plans
  @plans
end

#statementObject (readonly)

Returns the value of attribute statement.



3
4
5
# File 'lib/dexter/query.rb', line 3

def statement
  @statement
end

#suggest_indexObject

Returns the value of attribute suggest_index.



5
6
7
# File 'lib/dexter/query.rb', line 5

def suggest_index
  @suggest_index
end

#tablesObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dexter/query.rb', line 17

def tables
  @tables ||= begin
    parse ? parse.tables : []
  rescue => e
    # possible pg_query bug
    $stderr.puts "Error extracting tables. Please report to https://github.com/ankane/dexter/issues"
    $stderr.puts "#{e.class.name}: #{e.message}"
    $stderr.puts statement
    []
  end
end

#tables_from_viewsObject

Returns the value of attribute tables_from_views.



5
6
7
# File 'lib/dexter/query.rb', line 5

def tables_from_views
  @tables_from_views
end

#total_timeObject

Returns the value of attribute total_time.



5
6
7
# File 'lib/dexter/query.rb', line 5

def total_time
  @total_time
end

Instance Method Details

#costsObject



37
38
39
# File 'lib/dexter/query.rb', line 37

def costs
  plans.map { |plan| plan["Total Cost"] }
end

#explainable?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dexter/query.rb', line 33

def explainable?
  plans.size >= 3
end

#high_cost?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/dexter/query.rb', line 45

def high_cost?
  initial_cost && initial_cost >= 100
end

#initial_costObject



41
42
43
# File 'lib/dexter/query.rb', line 41

def initial_cost
  costs[0]
end

#treeObject



29
30
31
# File 'lib/dexter/query.rb', line 29

def tree
  parse.tree
end