01
The problem with the whole schema in the prompt
Size.500+ tables, most of them junction tables, so almost every question needs several joins. In the fixed workflow, working out the joins alone took 20 to 30 calls.
Confusion.Similar names sit side by side, such as country, country_code and market, and the model picks the wrong one.
Exposure.Every table in the prompt is a table the model may try to query.
02
Three steps, each a tool
List.Names and one line on each view. Cheap, and always the first call.
Describe.Columns, types and allowed values, only for the views the agent plans to use.
Sample.Three real rows, so the model sees how values are actually written.
The view and column names here are examples; the real ones stay internal. The descriptions matter more than the code: they are what the model reads when it chooses.
03
Checking the SQL against what it may touch
Discovery tells the model what exists. A guard makes sure it uses nothing else. Its error messages are written for the model, so it can correct the query and try again.
This version rejects CTEs and UNIONs, which is stricter than needed. The production guard parses them with sqlglot too, and checks every table and column they touch against the catalog, so a column the model made up never reaches the database.
04
What it changed
Calls to work out the joins:from 20 to 30 in the fixed workflow to 2 or 3.
Choosing columns:the fixed workflow rarely got that far. Now the agent reads the column descriptions in one call, and the guard rejects any column that does not exist before the query runs.
05
When not to do this
With a small schema of clearly named tables, put it in the prompt and skip the extra calls. Discovery pays when the schema is large, the names are close, or some tables must stay out of reach.
Related use case
Enterprise AI searchAnswers from your own documents and records.