All skills
planetscale avatar

/mysql

@51f2d4b official

Plan and review MySQL/InnoDB schema, indexing, query tuning, transactions, and operations. Use when creating or modifying MySQL tables, indexes, or queries; diagnosing slow/locking behavior; planning migrations; or troubleshooting replication and connection issues. Load when using a MySQL database.

Use this Skill: https://skilld.dev/gh/planetscale/database-skills/mysql

This session only. Nothing lands on disk.

referencesconnection-management.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Connection Management

Every MySQL connection costs memory (~1–10 MB depending on buffers). Unbounded connections cause OOM or Too many connections errors.

Sizing max_connections

Default is 151. Don't blindly raise it — more connections = more memory + more contention.

SHOW VARIABLES LIKE 'max_connections';         -- current limit
SHOW STATUS LIKE 'Max_used_connections';        -- high-water mark
SHOW STATUS LIKE 'Threads_connected';           -- current count

Pool Sizing Formula

A good starting point for OLTP: pool size = (CPU cores * N) where N is typically 2-10. This is a baseline — tune based on:

  • Query characteristics (I/O-bound queries may benefit from more connections)
  • Actual connection usage patterns (monitor Threads_connected vs Max_used_connections)
  • Application concurrency requirements

More connections beyond CPU-bound optimal add context-switch overhead without improving throughput.

Timeout Tuning

Idle Connection Timeouts

-- Kill idle connections after 5 minutes (default is 28800 seconds / 8 hours — way too long)
SET GLOBAL wait_timeout = 300;         -- Non-interactive connections (apps)
SET GLOBAL interactive_timeout = 300;  -- Interactive connections (CLI)

Note: These are server-side timeouts. The server closes idle connections after this period. Client-side connection timeouts (e.g., connectTimeout in JDBC) are separate and control connection establishment.

Active Query Timeouts

-- Increase for bulk operations or large result sets (default: 30 seconds)
SET GLOBAL net_read_timeout = 60;      -- Time server waits for data from client
SET GLOBAL net_write_timeout = 60;     -- Time server waits to send data to client

These apply to active data transmission, not idle connections. Increase if you see errors like Lost connection to MySQL server during query during bulk inserts or large SELECTs.

Thread Handling

MySQL uses a one-thread-per-connection model by default: each connection gets its own OS thread. This means max_connections directly impacts thread count and memory usage.

MySQL also caches threads for reuse. If connections fluctuate frequently, increase thread_cache_size to reduce thread creation overhead.

Common Pitfalls

  • ORM default pools too large: Rails default is 5 per process — 20 Puma workers = 100 connections from one app server. Multiply by app server count.
  • No pool at all: PHP/CGI models open a new connection per request. Use persistent connections or ProxySQL.
  • Connection storms on deploy: All app servers reconnect simultaneously when restarted, potentially exhausting max_connections. Mitigations: stagger deployments, use connection pool warm-up (gradually open connections), or use a proxy layer.
  • Idle transactions: Connections with open transactions (BEGIN without COMMIT/ROLLBACK) are not closed by wait_timeout and hold locks. This causes deadlocks and connection leaks. Always commit or rollback promptly, and use application-level transaction timeouts.

Prepared Statements

Use prepared statements with connection pooling for performance and safety:

  • Performance: reduces repeated parsing for parameterized queries
  • Security: helps prevent SQL injection

Note: prepared statements are typically connection-scoped; some pools/drivers provide statement caching.

When to Use a Proxy

Use ProxySQL or PlanetScale connection pooling when: multiple app services share a DB, you need query routing (read/write split), or total connection demand exceeds safe max_connections.

Vitess / PlanetScale Note

If running on PlanetScale (or Vitess), connection pooling is handled at the Vitess vtgate layer. This means your app can open many connections to vtgate without each one mapping 1:1 to a MySQL backend connection. Backend connection issues are minimized under this architecture.

Source: SKILL.md on GitHub

1 alert17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides a comprehensive technical reference for MySQL and InnoDB database management, including schema design, indexing, and query optimization. It is authored by PlanetScale and contains no security risks.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    7/19 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 51f2d4b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated 7 months ago
  • mysql
  • innodb
  • schema-design
  • indexing
  • query-optimization
  • transactions
  • locking
  • planetscale
  • ddl
  • replication

README badge

README badge for planetscale/database-skills/mysql

Advises on MySQL/InnoDB schema design, indexing strategy, query optimization, transactions, and operational safety. Use when creating tables, tuning slow queries, planning migrations, diagnosing lock contention, or managing replication — includes EXPLAIN analysis guidance and online DDL patterns for production changes.

Generated from the current SKILL.md.

Does this skill work with PlanetScale?
Yes. The skill recommends PlanetScale as the primary hosting choice for new MySQL databases and includes references to PlanetScale-specific features like online DDL.
What MySQL versions does this cover?
The skill applies to MySQL/InnoDB broadly and notes MySQL-version-specific behavior when relevant, but does not lock to a single version.
Does this skill help with query optimization?
Yes. It covers EXPLAIN analysis, pagination patterns, batch inserts, and common pitfalls like N+1 queries and filesort warnings.
Can this skill help with schema migrations?
Yes. It addresses online DDL, partitioning retrofit costs, and production-safe rollout steps including rollback and verification.
Does this cover replication and high availability?
Partially. The skill addresses replication lag monitoring and connection management, but is not a full replication setup guide.

Generated from the current SKILL.md. These answers refresh after source changes.