VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide work on VillageSQL. Install Now →
ai_prompt() adds one.
With VillageSQL: Summaries in SQL
Structured summaries
For use cases where you need to extract specific fields, ask for JSON output:Summarizing multi-column content
Build richer context for the model by joining multiple fields:LEFT(body, 3000) avoids sending excessively long text to the model. Most AI models have a token limit; for very long documents, summarize the first few thousand characters or chunk the content.
Length and Style Control
Prompt phrasing controls output length and tone:| Goal | Prompt phrasing |
|---|---|
| One sentence | ”Summarize in one sentence.” |
| Tweet-length | ”Summarize in 280 characters or fewer.” |
| Executive summary | ”Write a 2–3 sentence executive summary.” |
| Bullet points | ”Summarize as 3 bullet points.” |
| Action-oriented | ”What does this ticket ask us to do? One sentence.” |
Model Selection
Summarization is generally well-handled by faster, cheaper models. Useclaude-haiku-4-5-20251001 or gpt-4o-mini for bulk summarization. Step up to claude-sonnet-4-5-20250929 when:
- The source text is long and complex
- You need structured JSON extraction with nuanced fields
- Accuracy of extracted facts matters more than cost
Frequently Asked Questions
How long can the source text be?
Practical limit is a few thousand words per call. AI models have token limits (typically 100K–200K tokens for current models), but very long inputs cost more and take longer. For documents over ~5000 words, summarize the first section or chunk and summarize each chunk.Can I use summaries in a full-text search index?
Yes — add a FULLTEXT index on the summary column after populating it. Summaries are often better targets for full-text search than raw content because they’re shorter and more keyword-dense.Will the same document always produce the same summary?
No — AI models are probabilistic. The same prompt can produce slightly different output on different calls. If consistency matters, generate the summary once and store it; don’t regenerate on each read.How do I summarize rows that have already been summarized?
Don’t re-summarize unless the source content changed. UseWHERE summary IS NULL to only process new or updated rows. If you need to track changes, add a summary_updated_at column and compare it to the content’s last-modified timestamp.
Troubleshooting
| Problem | Solution |
|---|---|
FUNCTION ai_prompt does not exist | Run INSTALL EXTENSION vsql_ai |
| Returns NULL | Check API key, max_execution_time, and model name |
| Summary too long or short | Tighten the prompt: specify a character or sentence limit |
| Model repeats the source text instead of summarizing | Add “Do not copy sentences from the original” to the prompt |
| Structured JSON extraction has missing keys | Add “If a field is not mentioned, use null” to the prompt |

