# `Lotus.AI.QueryOptimizer`
[🔗](https://github.com/typhoonworks/lotus/blob/v0.16.6/lib/lotus/ai/query_optimizer.ex#L1)

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.

# `optimization_response`

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

# `suggest_optimizations`

```elixir
@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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
