Support Request: SQL Agent for Legacy Database #9598
Unanswered
AdeelKhan-Git
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I am building an AI-powered SQL analytics agent that generates SQL queries against a large legacy database. The database has been developed over many years, so the schema contains inconsistent naming, poorly documented columns, encoded values, and relationships that are not always obvious.
Github link: https://github.com/AdeelKhan-Git/optimize-agno
I would like guidance on the best architecture and agent design for handling these challenges reliably.
1. Legacy / Poorly Documented Schema
The database contains many columns whose meanings are not self-explanatory.
For example:
A column such as
jtypemay contain values likeSandO, where:S= SubscriptionO= Open AccessThere are many similar coded columns throughout the database.
Column names and values often do not provide enough information for an LLM to understand their actual business meaning.
I have created a glossary/metadata tool that can provide definitions and resolve these coded values, but I would like to know whether there is a better recommended architecture for exposing this type of metadata to the SQL agent.
2. Foreign-Key Relationships Are Missing or Inconsistent
The legacy database does not always have proper foreign-key constraints.
For example, I may have:
and:
which logically represent the same relationship, but the database either has no FK constraint or the columns have incompatible SQL data types.
This makes automatic relationship discovery difficult.
The agent therefore needs to understand logical relationships, not just rely on database-defined foreign keys.
What is the recommended approach for providing these logical relationships to an SQL agent?
3. Different SQL Dialects
The system may work with databases such as:
The agent needs to generate dialect-specific SQL correctly.
For example, syntax, functions, identifier quoting, pagination, and date operations can differ between databases.
I currently provide dialect information to the agent, but I would like to know whether the recommended approach is to use separate dialect-specific prompts/tools or a single agent with dialect metadata.
4. Large Schema / Table Selection
The database contains many tables and columns.
Providing the entire schema to the final SQL-generation agent consumes a significant amount of context and can make the model less reliable.
I therefore created a table-selection stage that first determines which tables are relevant and then passes only the selected schema to the SQL-generation agent.
The current architecture is roughly:
I would like guidance on whether this multi-stage architecture is the recommended approach for a legacy database with a large schema.
5. Resolving Business Values Before SQL Generation
Another problem is that users ask questions using business terminology rather than the actual database values.
For example, a user may ask:
But the database stores:
The SQL agent needs to understand that the user's phrase "Open Access" corresponds to
jtype = 'O'.I have implemented tools such as:
get_column_codesresolve_filter_valueget_sample_valuesThe agent can use these tools to resolve values before constructing the
WHEREclause.I would like to know whether this tool-based approach is preferable to putting all possible code mappings directly into the system prompt.
6. Sample Values Are Important
In many legacy tables, the schema alone is insufficient.
For example:
may contain values such as:
The model needs access to actual sample/distinct values to correctly construct filters.
However, exposing too many sample values can significantly increase context usage.
What is the recommended strategy for providing sample values to the agent without unnecessarily increasing the context?
7. Ambiguous Column Names
Some columns have names that are difficult to interpret.
For example, a column may be named:
or contain legacy naming conventions that don't clearly describe the business concept.
The agent sometimes needs additional metadata to determine whether a column should be used for:
I am considering maintaining richer metadata for each column, such as:
Is this a recommended approach for SQL agents operating over legacy databases?
8. Preventing Incorrect JOINs
One of my biggest concerns is incorrect joins.
Because the legacy database does not always contain explicit foreign keys, an LLM might infer a relationship simply because two columns have similar names.
For example:
may be a valid logical relationship, but other similarly named columns may not be.
I want the agent to use verified relationships only, rather than allowing the LLM to freely guess joins.
Would you recommend maintaining a separate relationship/semantic layer containing information such as:
and exposing that to the agent?
9. SQL Generation Reliability
The goal is not simply to generate syntactically valid SQL.
The agent needs to generate SQL that is semantically correct for the legacy database.
For example, this query may be syntactically valid:
but semantically wrong if the database actually stores:
Therefore, I need the agent to understand:
User terminology → Business meaning → Database column → Database value → SQL
rather than simply mapping the user's words directly into SQL.
10. Structured Output Issues with Local LLMs
I am also experimenting with local models through Ollama, including Granite models.
I have encountered cases where the model returns a raw string instead of the expected structured/Pydantic output.
For example, the application expects a structured response for table selection, but sometimes receives something equivalent to:
instead of the expected structured object.
This makes the agent pipeline fail even though the model technically generated a response.
I would like recommendations for reliable structured output handling with local models.
11. Context-Length Management
I am currently experimenting with context sizes such as:
while some models advertise much larger maximum contexts.
The challenge is that the SQL agent's instructions, schema information, glossary information, relationship metadata, sample values, and user query can all consume context.
I would like guidance on how to design the context pipeline efficiently rather than simply increasing the context window.
12. Model Size vs. SQL Accuracy
I am testing different local models, including:
The larger model generally provides better reasoning, but it also requires significantly more GPU memory and can affect latency.
I would like to understand whether for this type of SQL-agent workload it is generally better to:
13. Current Agent Architecture
The current system is approximately:
The main goal is to make this system reliable enough to work with a real-world legacy database where the schema itself cannot be trusted as the complete source of truth.
Questions I Would Like Help With
S = SubscriptionandO = Open Access?The key challenge is that this is not a clean modern database. The AI agent must understand the business meaning behind a legacy schema rather than simply reading table and column names. I would appreciate recommendations for an architecture that makes the SQL generation reliable, grounded, and resistant to incorrect joins, incorrect coded values, and hallucinated schema information.
All reactions