slow-query-analysis

Enable MySQL slow query log and use pt-query-digest to identify the most expensive database queries

openjny/sre-agent-isucon131 installsMITSynced Aug 26

Works with

Claude CodeCursorCodex CLIGitHub CopilotGemini CLI
---
name: slow-query-analysis
description: Enable MySQL slow query log and use pt-query-digest to identify the most expensive database queries
license: MIT
---

# Slow Query Analysis — MySQL

Identify the most expensive MySQL queries using the slow query log and pt-query-digest.

## Enable Slow Query Log

```bash
exec host="vm1" command="sudo mysql -u isucon -pisucon -e \"
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log';
SET GLOBAL long_query_time = 0.01;
\""
```

**Note:** `long_query_time = 0.01` captures queries taking >10ms. For initial analysis, 0.1 (100ms) may be sufficient.

## Install pt-query-digest

```bash
exec host="vm1" command="which pt-query-digest || sudo apt-get install -y percona-toolkit"
```

## Workflow

1. Clear the slow query log:
   ```bash
   exec host="vm1" command="sudo truncate -s 0 /var/log/mysql/slow.log"
   ```

2. Run a benchmark:
   ```
   benchmark_start
   ```

3. Analyze results:
   ```bash
   exec host="vm1" command="sudo pt-query-digest /var/log/mysql/slow.log --limit 10"
   ```

## Reading pt-query-digest Output

### Key Sections

1. **Profile** — Top queries by total time:
   ```
   Rank  Query ID                          Response time  Calls  R/Call
   ====  ================================  =============  =====  ======
      1  0x1234567890ABCDEF...             150.0000 45.0%   500  0.3000
   ```

2. **Per-query details** — Full SQL, EXPLAIN output, index usage

### What to Look For

| Pattern | Issue | Fix |
|---------|-------|-----|
| High R/Call, low Calls | Single slow query | Add index, rewrite query |
| Low R/Call, high Calls | N+1 pattern | Batch or JOIN |
| Full table scan in EXPLAIN | Missing index | Add appropriate index |
| filesort in EXPLAIN | Sorting without index | Add composite index |

## Quick Analysis (without pt-query-digest)

```bash
exec host="vm1" command="sudo tail -200 /var/log/mysql/slow.log | grep -E 'Query_time|^SELECT|^UPDATE|^INSERT'"
```

## Disable After Analysis

To avoid log file growth during production benchmarks:

```bash
exec host="vm1" command="sudo mysql -u isucon -pisucon -e \"SET GLOBAL slow_query_log = 'OFF'\""
```

More Database skills

azure-upgrade

microsoft/azure-skills

Assess and upgrade Azure workloads between plans, tiers, or SKUs, or modernize Azure SDK dependencies in source code. WHEN: upgrade Consumption to Flex Consumption, upgrade Azure Functions plan, change hosting plan, function app SKU, migrate App Service to Container Apps, modernize legacy Azure Java SDKs (com.microsoft.azure to com.azure), migrate Azure Cache for Redis (ACR/ACRE) to Azure Managed Redis (AMR).

413.0k

supabase-postgres-best-practices

supabase/agent-skills

Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pg_cron, pgmq), vector/semantic search (pgvector), and restoring dumps (pg_restore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.

377.3k

prisma-database-setup

prisma/skills

Guides for configuring Prisma with different database providers (PostgreSQL, MySQL, SQLite, MongoDB, etc.). Use when setting up a new project, changing databases, or troubleshooting connection issues. Triggers on "configure postgres", "connect to mysql", "setup mongodb", "sqlite setup".

247.3k

← All Database skills

Check your AI visibility

One URL in, a 0–100 score and the exact fixes out.

RUN THE CHECK

Browse all the tools

15 tools across six categories
13 of them never send your data anywhere

Free · No signup · No trial clock

SEE THE DIRECTORY