# Answering questions over large data with analytics tables

Source: https://docs.autessa.com/docs/autessascript/guides/analytics-agent-sql

> Give an agent read-only SQL over millions of rows: keep the data in an ANALYTICS table, provision the describe / query / get-result tools with create-agent-tools, and have the agent describe first, prefer verified queries and always filter on partition columns.

# Answering questions over large data with analytics tables

Give an agent read-only SQL over millions of rows: keep the data in an ANALYTICS table, provision the describe / query / get-result tools with create-agent-tools, and have the agent describe first, prefer verified queries and always filter on partition columns.

**The decision:** the agent must aggregate, group, rank or join over a LOT of rows (sales
lines, events, logs, transactions). A regular AutessaDB table answers row lookups; for this,
use an **analytics table** (dbType ANALYTICS): columnar analytics storage queried with
read-only SQL in **ANSI SQL**.

## The pattern

1. **Create the analytics table** (autessadb.analytics `create`) with typed columns and a
   partition spec on the column most questions filter by — usually a date
   (e.g. MONTH of `order_date`).
2. **Load data** with `load-from-s3` / `load` (CSV, Parquet or JSON files), `load-from-table`
   (copy a regular AutessaDB table) or `append-rows` (small batches; they become queryable
   within seconds). Loads are jobs: poll `job-status` until SUCCEEDED.
3. **Write the semantic layer** with `update-semantics`: a table description, the grain
   ("one row is one order line"), column descriptions and synonyms, named measures
   (`revenue := sum(amount)`), documented joins, and **verified queries** — questions with
   SQL a person checked. This is what makes an agent's SQL right the first time.
   `suggest-semantics` drafts all of this from the schema and a sample (checked, never
   saved): review it with the person, then save the parts they accept.
4. **Provision the tools** with `create-agent-tools` for the tables the agent may use. You
   get three tools bound to exactly those tables: *describe*, *query* and *get result*.
   Attach all three to the agent.
5. **Instruct the agent** with the rules below.

## Rules to put in the agent's instructions

- **Describe first.** Call the describe tool before writing SQL, and use only the SQL
  names and columns it lists.
- **Prefer verified queries.** When a verified query matches the question, adapt it rather
  than writing new SQL.
- **Estimate big queries.** When a query may read a lot (no partition filter, a long date
  range, a join), run it first with `mode=ESTIMATE`: nothing runs, and the result says
  whether it fits the scan limit.
- **Always filter on partition columns** when the question allows (a date range on the
  partition column), so the query reads only the data it needs. A query that would read
  more than the scan limit is refused with `SCAN_LIMIT`; the hint names the columns to
  filter on.
- **Read-only.** Only one SELECT (or WITH ... SELECT) per call. Data changes go through the
  load and append methods, never SQL.
- **Queries are async jobs.** The query tool waits about 25 seconds; a longer query returns
  a `jobId` — call the get-result tool with it (and `page` 1, 2, ... for more rows).
- **Follow `next` and `hint`.** Every tool result carries `next`, the call to make next
  (e.g. get-result with `jobId` and `page=2`); every error carries a `hint` (UNKNOWN_COLUMN
  lists the columns, SCAN_LIMIT names the partition columns to filter on).
- **Aggregate in SQL**, not by fetching thousands of rows into the conversation.
- **Never paste user input into SQL text.** From scripts and apps, put `$name` placeholders
  in the SQL and pass the values in `params`; they are bound as typed literals.

## Verify by executing

Run the query tool once with a question whose answer you know (a COUNT over a known month)
and check the number; then run the agent on the same question and confirm the transcript
shows describe → query with a partition filter. Row and column security still apply: the
agent sees only the rows and columns the person it acts for may see.

## See also

- *data-placement* — when data belongs in a regular table versus an analytics table.
- *agents-tools-managers* — attaching tools to a SIMPLE agent.
- *drive-agent-files* — the same loop for files (search → grep → read).

## Related

- [Autessa SDK overview](/docs/autessascript/sdk.md)

