Data
Preparing a database so an AI agent can query it
The agent writes SQL from plain-English questions. I prepared the database so its queries are correct and fast: column descriptions it can read, clean views, a pre-computed table and search indexes.
The problem
Raw operational tables are wide, inconsistently named and full of exceptions. An LLM querying them directly gives slow and wrong answers.
The approach
- Curated PostgreSQL tables with a COMMENT on every column, which the agent reads as its semantic layer.
- Filtered views that apply business rules once, so every query starts from clean data.
- A materialized view with a unique index, refreshed nightly with REFRESH CONCURRENTLY so reads never block.
- Full-text (tsvector + GIN), trigram and lower() indexes for fast name and text search.
What I learned
- A good data layer improves answer quality more than prompt tweaks.
- Pre-computing the expensive parts keeps agent latency predictable.