# Query Logs

https://docs.wisecp.com/es/query-logs

Record the database queries your installation runs, see how often each one repeats and which line of code asks for it, so you can find what is slowing a page down.

## Opening the Screen

Open **Tools**, go to the action logs and choose **Query Logs**: `{admin}/tools/query-logs`

## What Is on the Screen

The settings card sits at the top and stays there. Everything under it appears only once queries have been recorded.

- **Query Logging Settings**: Two cards: the recording switch and the address list. The badge beside **Query Logging** reads **Enabled** or **Disabled**, and the button under it turns recording on or off.
- **Totals**: Four figures over the list: **Unique Queries**, **Total Executions**, **Call Locations** and **Unique URLs**.
- **Query list**: One entry per distinct query on the left. Each shows a **Max** badge with the worst count in a single page load, the opening of the query, then its total, how many places call it and the time it last ran.
- **Query Details**: The panel on the right, with its own four figures: **Max Per Session**, **Total Executions**, **Call Locations** and **Parameter Sets**. Under them the file hash and a **Copy Details (JSON)** button.
- **Detail sections**: Five foldable sections: **SQL Query** with its own copy button and its first and last run, **Script Executions**, **Call Locations** with the call stack, **Executed URLs** and **Query Parameters**.

## Fields

- **Query Logging**: Off to start with. While it is on, every SELECT query is recorded together with the code that asked for it. The **Enable** and **Disable** button saves at once, with no separate confirmation.
- **Valid IP Addresses**: Restricts recording to the addresses you list. Type an address and press Enter to turn it into a tag, and drag tags to reorder them. Leave it empty to allow every address. **UNKNOWN** is accepted and covers runs with no visitor address, such as scheduled tasks. Click **Save** to store the list; the page reloads.
- **Search box**: Placed over the list, with the hint **Search queries by SQL text, location, or URL...**. It matches the query text, the calling file and function, and the recorded addresses.
- **Sort list**: Reorders the entries: **Max per Session**, **Total Executions**, **Last Executed**, **Unique URLs**, **Call Locations**. It starts on **Max per Session**, the order that puts repeated queries first.

## Tasks

### Start recording

1. Click **Enable** on the **Query Logging** card.
2. Put your own address in **Valid IP Addresses** and click **Save**, so only your own visits are recorded.
3. Use the pages you want to examine, then come back. The entries appear once queries have been recorded.

### Find a query that repeats

1. Leave the sort on **Max per Session**; the worst entry is at the top.
2. Open it and read **Max Per Session**. A high figure means one page load ran the same query that many times.
3. Open **Call Locations**. The file, the line and the call stack say which code asks for it.

### Hand a query to a developer

1. Open the entry and use the copy button inside **SQL Query** for the query on its own.
2. Use **Copy Details (JSON)** at the top for the whole record, with the locations, the addresses and the recorded values.

### Clear the records

1. Click **Clear All Logs** next to the page title. The button appears only where records exist.
2. Confirm with **Yes, Clear All**. Every record is removed and the page reloads.

## Things to Watch

> **This is a diagnostic tool, not something to leave on**
> 
> While recording is on, every SELECT query costs extra time, because the code behind it is worked out and kept. At the end of each request one file is written per distinct query, and nothing clears them later. Turn it on to investigate something specific, then turn it off and clear the records.

> **The address list is a gate, not a preference**
> 
> Where the list is not empty, only visits from those addresses are recorded. A list that does not hold your own address leaves the screen empty while logging looks enabled. Check the list first when nothing arrives.

> **Clearing takes everything at once**
> 
> There is no date and no password on this dialog: it removes every recorded query in one step and cannot be undone. Copy the entries you still need before you clear them.

## Required Privileges

The screen, the two settings and the clearing action all need the `TOOLS_ACTIONS` privilege. Without it the page is not shown.

## Related Articles

- [Error Logs](https://docs.wisecp.com/en/system-error-logs)
- [Module Logs](https://docs.wisecp.com/en/module-logs)
- [Action Logs](https://docs.wisecp.com/en/action-logs)
