> For the complete documentation index, see [llms.txt](https://docs.datasentinel.io/manual/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datasentinel.io/manual/ai-connector/analyze-and-tune-workloads/optimize-top-queries.md).

# Optimize your top queries

Surface the queries consuming the most time and resources, then act on them.

Ask the assistant to rank the heaviest queries on an instance, explain why they are expensive, and turn that into concrete tuning actions.

<figure><img src="https://196776047-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDL4xMeOs1Z7fz2LBCNSv%2Fuploads%2Fgit-blob-5ae011c89e95dc88550bbb1d0ced7da3ab935510%2Foptimize-top-queries.svg?alt=media" alt="Animated replay of a chat with an AI assistant, condensed from a real session of about seven minutes. The user asks: Drill into the slowest queries on prod-eu-2 around 10 a.m. this morning. The assistant confirms the window 09:40-10:20 local, reads the platform through the Datasentinel tools (workload, top queries, query details, blocking scenarios, top tables, table details), then returns a workload analysis: four key points, four key findings each with evidence, an action and the expected gain, a captured execution plan with its sequential scans highlighted, the table scan context, and a menu of next steps."><figcaption><p>One prompt, from the question to the tuning actions: a real session of about seven minutes, condensed into one</p></figcaption></figure>

{% hint style="info" %}
The assistant can also retrieve the [execution plans](https://app.gitbook.com/o/DaDQkGCM7r2TghFSBFLQ/s/yE16Xb3IemPxJWydtPOj/features/other-features/execution-plans) of these queries when the [pg\_store\_plans](https://app.gitbook.com/o/DaDQkGCM7r2TghFSBFLQ/s/yE16Xb3IemPxJWydtPOj/getting-started/postgresql-clusters/extensions/pg_store_plans) extension is installed on the monitored instance. Plans are collected automatically alongside query statistics, so the recommendations can point at the actual scan, join, or sort step to fix. To get the plan the planner would choose right now instead, for a statement you paste or for the query on screen, see [Explain execution plans](/manual/ai-connector/analyze-and-tune-workloads/explain-execution-plans.md).
{% endhint %}

**Example prompts:**

* **Rank and drill down:**
  * "Drill into the slowest queries on prod-eu-2 around 10 a.m. this morning."
  * "Show me all the metrics of the heaviest delete on prod-eu-1 today."
  * "Which query spills the most to temp files on prod-eu-1?"
* **Execution plans:**
  * "Show me the captured execution plan of the slowest query on prod-eu-1 this morning."
  * "Did the execution plan of the heaviest select on prod-eu-1 change today?"

{% hint style="info" %}
**Continue from the menu.** Once the ranking is on screen, the next-step menu lets you drill into one query (full SQL, wait events, execution plan), check the tables it hammers, or ask the assistant to prove a finding before you act on it. Pick an option to keep the investigation going without retyping the context.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.datasentinel.io/manual/ai-connector/analyze-and-tune-workloads/optimize-top-queries.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
