SQL Optimizer for Openclaw

SQL Optimizer turns business requirements or slow SQL queries into correct, readable, dialect-specific SQL with actionable performance recommendations.

petehuax
v1.0.0
Oct 12, 2026
0
128
0

Install & Download

1. ClawHub CLI

The fastest way to install a skill directly from the registry.

npx clawhub@latest install sql-optimizer

2. Manual Installation

Copy the skill folder to one of these locations

Global
~/.openclaw/skills/
Workspace
<project>/skills/

Priority: Workspace > Local > Bundled

3. Prompt Installation

Copy this prompt to OpenClaw to install it automatically.

Help me install sql-optimizer using Clawhub. If Clawhub is not installed, install it first (npm i -g clawhub).

Prefer to download?

Get the raw skill files in a ZIP archive.

What is SQL Optimizer?

SQL Optimizer is an Openclaw Skills capability for translating business questions into production-ready SQL and systematically improving existing queries. It clarifies the expected result, filters, grouping, sorting, pagination, deduplication needs, database dialect, schema, indexes, and data volume before generating a solution.

The skill goes beyond rewriting syntax: it evaluates index usage, join order, functions on indexed columns, implicit type conversions, wildcard searches, pagination strategy, and execution plans. It also explains the reason, expected benefit, applicability, and trade-offs of each recommendation, while adding rollback and preview safeguards for UPDATE, DELETE, and DDL operations.

SQL Optimizer Use Cases

  • Write reporting, analytics, and data-retrieval SQL from a natural-language business requirement.
  • Diagnose and optimize a slow query using schema, index, row-count, and execution-plan information.
  • Generate dialect-appropriate SQL for MySQL, PostgreSQL, Oracle, SQL Server, Hive, or ClickHouse.
  • Design composite indexes and explain the recommended column order.
  • Rewrite non-sargable date filters, function-wrapped predicates, implicit conversions, and inefficient joins.
  • Replace deep OFFSET pagination with keyset pagination when appropriate.
  • Compare alternatives such as IN versus EXISTS or OR versus UNION ALL.
  • Safely review UPDATE, DELETE, and DDL statements before execution.

How SQL Optimizer Works

  1. Clarify the requirement: Identify the desired result, filters, grouping dimensions, ordering, pagination, deduplication rules, and whether the request is read-only or modifies data.
  2. Confirm the environment: Determine the database dialect and version, table and column definitions, data types, existing indexes, approximate data volume, and whether read/write separation is used.
  3. State assumptions: If schema details are missing, create a reasonable provisional model and explicitly list assumptions for user confirmation rather than inventing undisclosed fields.
  4. Write the SQL: Prioritize correctness and readability. Use CTEs to express complex logic in clear stages instead of producing deeply nested queries.
  5. Run the optimization checklist: Review SELECT projection, indexed-column functions, implicit conversions, wildcard patterns, composite-index prefixes, join filtering, driving tables, pagination, and IN/EXISTS choices.
  6. Recommend indexes and rewrites: For every recommendation, explain what changes, why it may be faster, when it applies, and what side effects or maintenance costs to consider.
  7. Define verification: Use EXPLAIN or EXPLAIN ANALYZE to validate access paths, estimated rows, index selection, sorting, temporary tables, and full scans.
  8. Add safety controls: For writes, provide a SELECT preview, require a WHERE clause, recommend transactions or backups, and identify rollback or locking risks before execution.

SQL Optimizer Setup

No external package installation is required; SQL Optimizer is an agent-created Openclaw Skills capability. Provide the following context in the request for the most reliable result:

  1. Identify the target database dialect and version.
  2. Include the business requirement or current SQL query.
  3. Provide table names, columns, data types, relationships, existing indexes, and approximate row counts.
  4. Include an execution plan when diagnosing a slow query.
  5. State pagination, consistency, read/write separation, and production-safety requirements.

Example request:

cat <<'REQUEST'
Database: PostgreSQL 16
Goal: List the 20 most recent paid orders for a customer.
Tables: orders(id bigint, customer_id bigint, status text, created_at timestamp)
Indexes: orders_customer_id_status_created_at_idx(customer_id, status, created_at)
Please write the query, review index usage, and explain how to validate it.
REQUEST

For validation, run the generated query in a safe environment with the target database's native plan command:

# PostgreSQL
EXPLAIN (ANALYZE, BUFFERS) <query>;

# MySQL
EXPLAIN ANALYZE <query>;

Never apply generated UPDATE, DELETE, or DDL statements directly to production without previewing affected rows, checking the execution plan, using an appropriate transaction or backup, and reviewing locking and rollback behavior.

SQL Optimizer Data Schema & Taxonomy

SQL Optimizer organizes its analysis around the following logical data model:

Section Contents
Requirement summary Desired output, business rules, filters, grouping, sorting, pagination, and deduplication requirements
Assumptions Missing schema details, inferred relationships, expected field names, and assumptions requiring confirmation
Database context Dialect, version, storage or execution characteristics, read/write separation, and compatibility constraints
Tables and fields Table names, column names, data types, relationships, and relevant cardinality information
Existing indexes Index columns, order, selectivity considerations, and whether a composite leftmost prefix is usable
Recommended SQL A complete, readable, dialect-specific statement with comments where useful
Optimization points Each rewrite with its rationale, expected benefit, applicability, and side effects
Index recommendations Candidate CREATE INDEX statements with column-order reasoning and maintenance implications
Verification plan EXPLAIN or EXPLAIN ANALYZE expectations, including access type, key, rows, filesort, and temporary-table indicators
Safety guidance SELECT previews, WHERE requirements, transaction, backup, LIMIT, lock, and rollback considerations

The skill does not require or imply generated files, migrations, database connections, or persistent storage. Its primary output is a structured response containing SQL, assumptions, recommendations, and validation steps.

SQL Optimizer Advanced Features

  • Supports multiple SQL dialects, including MySQL, PostgreSQL, Oracle, SQL Server, Hive, and ClickHouse, with dialect-specific pagination, date functions, string operations, and syntax.
  • Uses CTEs to make complex query logic modular, readable, and easier to review.
  • Detects common performance issues such as SELECT *, functions on indexed columns, implicit type conversion, leading-wildcard searches, poor composite-index order, and unfiltered joins.
  • Recommends keyset pagination using a previous page's maximum ID instead of expensive deep OFFSET scans.
  • Evaluates when OR can be split into UNION ALL and when EXISTS may be preferable to IN, while noting that actual data distribution determines the best choice.
  • Encourages predicate pushdown before joins and selecting a smaller driving table for large-table joins.
  • Uses EXPLAIN-oriented diagnostics, including access type, selected index, estimated rows, filesort, and temporary-table warnings.
  • Provides index definitions with an explanation of column order, selectivity, leftmost-prefix behavior, and write-maintenance trade-offs.
  • Adds production safety guidance for UPDATE, DELETE, and DDL, including SELECT previews, mandatory WHERE clauses, transactions, backups, LIMIT-based testing, and rollback planning.
  • Follows a self-check process to confirm dialect compatibility, schema consistency, optimization coverage, and clearly documented risks within Openclaw Skills.

SKILL.md


Loading

Related Openclaw Skills

METADATA

Github Stars: 0
forks: 0

Featured*