SQL optimization with AI: 3 main questions
Today, almost every developer has an AI subscription. Many developers already gave a prompt such as “Optimize this SQL query” and got a result, and the result was usually good. (If you did not try it yet, try it now.)
But when you use AI for SQL optimization, you (and your manager) will have 3 questions:
- Is the answer from the LLM correct? Is it the best possible answer?
- How can we integrate AI into our system safely and make it work proactively?
- What happens if the data that we send to the AI leaks? Who else gets this data?
HQbird 2027 gives answers to these 3 questions.
Is the answer from the LLM correct? Is it the best possible answer?
Context. An LLM gives a good answer only when it has the correct context. For SQL optimization, the context is:
- the SQL text
- the execution statistics and the per-table statistics
- the execution plan
- the table sizes and the index information
- the metadata: indexes and constraints
HQbird collects this data and sends to the LLM only the parts that it needs.
Verification. LLM answers are probabilistic, so they can contain errors. Thus, you must verify each optimized query on a real database. As a minimum, the optimized query must return the same dataset as the original query. HQbird also does more checks, to make sure that the new query is really faster and that it is not a different query. HQbird does all these checks safely.
Iterations. An LLM usually needs several iterations to get a good result. HQbird runs several iterations. It uses a test database (a copy of the production database) as a test bench.
Execution chains. HQbird can also analyze and optimize chains of SQL statements. For example: an UPDATE fires a trigger, and the trigger calls a stored procedure. The user sees a “slow UPDATE”, but the real problem is an existence check in the nested stored procedure. HQbird starts a special trace that records the full execution chain. It finds the problem and gives AI optimizations for the specific statement or stored procedure that causes the problem.
How can we integrate AI into our system safely and make it work proactively?
The worst thing you can do is to give an AI agent direct access to the production database. You probably know about cases where an AI agent dropped tables, or even a full database, while it tried to reach a goal. We tested this approach too, and we confirm: it is too dangerous. Because AI is probabilistic, it can make serious errors. A DBA must be very careful with this type of access.
In HQbird, the AI is isolated from the production database. The AI receives only text (SQL, plans, statistics) and returns only text (recommendations). HQbird has 3 modes:
- Text-only analysis. The recommendation is a hypothesis. HQbird shows it as text only.
- Read-only SQL changes. HQbird runs the changed SQL in a read-only transaction with a time limit.
- Test database experiments. HQbird connects to the test database and makes sure that it has no more than 3 connections (default value). Then it asks for your confirmation. After you confirm, it starts a series of experiments: it creates and drops indexes and measures the results. At the end, it gives a final SQL script with recommendations. Special protections prevent access to production databases.
Proactive mode. You can apply optimizations to production through MCP, with a human in the loop and a double safety check: one check by the LLM and one by hard-coded stoppers. We will describe this in a separate email. Stay tuned!
What happens if the data that we send to the AI leaks? Who else gets this data?
This is a serious risk, especially for organizations that keep personal, medical, or financial data, and for companies whose customers fall under GDPR, LGPD, HIPAA, and similar regulations.
The best solution is a local LLM. You do not need an expensive GPU server for this. There are local models for SQL tasks, such as SQLCoder and zen-sql, that run on a modest hardware at an acceptable speed. Your data stays local.
Start your AI journey with Firebird today:
Contact us at [email protected] to schedule a call.
More details and download: HQbird
And, last but not least: today is the last day of 25% discount: purchase HQbird 2027 now to start optimization of your queries today.