Lotus.AI.QueryOptimizer (Lotus v0.16.5)

Copy Markdown View Source

Generates AI-powered optimization suggestions for SQL queries.

Analyzes a query's execution plan and structure to suggest index additions, query rewrites, or structural improvements.

Summary

Functions

Generate optimization suggestions for a SQL query.

Types

optimization_response()

@type optimization_response() :: %{
  suggestions: [map()],
  model: String.t(),
  usage: %{
    prompt_tokens: non_neg_integer(),
    completion_tokens: non_neg_integer(),
    total_tokens: non_neg_integer()
  }
}

Functions

suggest_optimizations(model_string, opts)

@spec suggest_optimizations(
  String.t(),
  keyword()
) :: {:ok, optimization_response()} | {:error, term()}

Generate optimization suggestions for a SQL query.

Runs EXPLAIN on the query to get the execution plan, then sends both the SQL and plan to the AI for analysis.

Options

  • :sql (required) - The SQL query to optimize
  • :data_source (required) - Name of the data source
  • :api_key (required) - API key for the LLM provider
  • :params (optional) - Query parameters (default: [])
  • :search_path (optional) - PostgreSQL search path
  • :temperature (optional) - LLM temperature (default: 0.1)

Returns

  • {:ok, result} - Map with :suggestions, :model, and :usage
  • {:error, term} - Error tuple