Class: NoSE::Plans::QueryPlanner

Inherits:
Object
  • Object
show all
Defined in:
lib/nose/plans/query_planner.rb

Overview

A query planner which can construct a tree of query plans

Instance Method Summary collapse

Constructor Details

#initialize(model, indexes, cost_model) ⇒ QueryPlanner

Returns a new instance of QueryPlanner.



226
227
228
229
230
231
232
# File 'lib/nose/plans/query_planner.rb', line 226

def initialize(model, indexes, cost_model)
  @logger = Logging.logger['nose::query_planner']

  @model = model
  @indexes = indexes
  @cost_model = cost_model
end

Instance Method Details

#find_plans_for_query(query) ⇒ QueryPlanTree

Find a tree of plans for the given query

Returns:

Raises:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/nose/plans/query_planner.rb', line 237

def find_plans_for_query(query)
  state = QueryState.new query, @model
  state.freeze
  tree = QueryPlanTree.new state, @cost_model

  indexes_by_joins = indexes_for_query(query, state.joins)
  find_plans_for_step tree.root, indexes_by_joins

  if tree.root.children.empty?
    tree = QueryPlanTree.new state, @cost_model
    find_plans_for_step tree.root, indexes_by_joins, prune: false
    fail NoPlanException, "#{query.inspect} #{tree.inspect}"
  end

  @logger.debug { "Plans for #{query.inspect}: #{tree.inspect}" }

  tree
end

#min_plan(query) ⇒ QueryPlan

Get the minimum cost plan for executing this query

Returns:



258
259
260
# File 'lib/nose/plans/query_planner.rb', line 258

def min_plan(query)
  find_plans_for_query(query).min
end