Zum Inhalt springen
Skillpedia
APIs & Server (Backend)280k · #215

mysql-patterns

Was er macht

Dieses Tool hilft dir, MySQL- und MariaDB-Datenbanken zu erstellen und zu optimieren. Es bietet Muster für die Organisation von Tabellen, das Anlegen effizienter Indizes und das Schreiben schneller Abfragen.

Wann du ihn nutzt

  • ✓Wenn du Tabellenstrukturen und Indizes entwirfst.
  • ✓Wenn du herausfinden willst, warum Datenbankabfragen langsam sind.
  • ✓Wenn du die Verbindung deiner Anwendung zur Datenbank konfigurierst.

Link zum Skill

https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mysql-patterns/SKILL.md

Kopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.

So nutzt du ihn

Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.

Fertige Nachricht zum Einfügen

Nutze diesen Skill, um mir zu helfen: https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mysql-patterns/SKILL.md Lies die SKILL.md unter diesem Link und folge ihren Anweisungen für meine Aufgabe.
Soll deine KI Skills wie diesen selbst finden?Installiere den kostenlosen Skillpedia-MCP: eine Zeile, und sie durchsucht vor jeder Aufgabe 15.000+ Skills.In 1 Minute installieren →

Beispielanfrage an deine KI

“Wie kann ich einen Index erstellen, damit die Suche nach Name und Erstellungsdatum in meiner Tabelle users schneller wird?”

5 neue Skills pro Woche. 1 Minute Lesezeit.

So funktioniert es

Das Tool gibt Empfehlungen auf Basis von Codebeispielen und Erklärungen. Es zeigt, wie man Tabellen definiert, welche Datentypen man verwendet und wie man Abfragen schreibt, die gut performen.

Technischer Inhalt (originale SKILL.md)

Originaltext des Autors, auf Englisch, direkt von GitHub.

MySQL Patterns

Use this skill when working on MySQL or MariaDB schema design, migrations, slow-query investigation, queue-style transactions, connection pools, or production database configuration. Prefer exact version checks before applying a feature-specific pattern because MySQL and MariaDB have diverged in several SQL details.

Activation

  • Designing MySQL or MariaDB tables, indexes, and constraints
  • Reviewing migrations before they run on large production tables
  • Debugging slow queries, lock waits, deadlocks, or connection exhaustion
  • Adding keyset pagination, upserts, full-text search, JSON columns, or queues
  • Configuring application connection pools, read replicas, TLS, or slow logs

Version Check

Start by identifying the engine and version:

SELECT VERSION();
SHOW VARIABLES LIKE 'version_comment';

Keep MySQL and MariaDB guidance separate when syntax differs:

  • MySQL documents row aliases as the replacement for VALUES(col) in ON DUPLICATE KEY UPDATE; VALUES(col) is deprecated there.
  • MariaDB documents VALUES(col) as the supported way to reference inserted values in ON DUPLICATE KEY UPDATE; use it for cross-engine compatibility.
  • SKIP LOCKED is appropriate for queue-like work only. It skips locked rows and can return an inconsistent view, so do not use it for general accounting or integrity-sensitive reads.

Schema Defaults

CREATE TABLE orders (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    account_id BIGINT UNSIGNED NOT NULL,
    status VARCHAR(32) NOT NULL,
    total DECIMAL(15, 2) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at DATETIME NULL,
    PRIMARY KEY (id),
    KEY idx_orders_account_status_created (account_id, status, created_at),
    KEY idx_orders_active (account_id, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Default choices:

Use Case Prefer Avoid
Surrogate primary keys BIGINT UNSIGNED AUTO_INCREMENT INT for tables that can grow beyond 2B rows
UUID lookup keys BINARY(16) with conversion helpers VARCHAR(36) primary keys on hot tables
Money and exact quantities DECIMAL(p, s) FLOAT or DOUBLE
User-facing text utf8mb4 tables and indexes MySQL utf8 / utf8mb3 defaults
Application timestamps DATETIME with UTC managed by the app Assuming DATETIME stores time zone metadata
Soft deletes deleted_at DATETIME NULL plus scoped indexes Filtering soft-deleted rows without an index
Extensible status values lookup table or constrained VARCHAR ENUM when values change often

Indexing

Composite index order usually follows equality predicates first, then range or sort columns:

CREATE INDEX idx_orders_account_status_created
    ON orders (account_id, status, created_at);

SELECT id, total
FROM orders
WHERE account_id = ?
  AND status = 'pending'
  AND created_at >= ?
ORDER BY created_at DESC
LIMIT 50;

Use EXPLAIN before adding or changing an index:

EXPLAIN
SELECT id, total
FROM orders
WHERE account_id = 123 AND status = 'pending'
ORDER BY created_at DESC
LIMIT 50;

Signals to investigate:

Field Risk Signal
type ALL on a large table
key NULL when a selective predicate exists
rows Very high row estimate for an interactive path
Extra Using temporary, Using filesort, or broad Using where

Avoid adding indexes blindly. Each index increases write cost, migration time, backup size, and buffer-pool pressure.

Query Patterns

Upsert

Cross-engine-compatible form:

INSERT INTO user_settings (user_id, setting_key, setting_value)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
    setting_value = VALUES(setting_value),
    updated_at = CURRENT_TIMESTAMP;

MySQL row-alias form:

INSERT INTO user_settings (user_id, setting_key, setting_value)
VALUES (?, ?, ?) AS new
ON DUPLICATE KEY UPDATE
    setting_value = new.setting_value,
    updated_at = CURRENT_TIMESTAMP;

Use the row-alias form only after confirming the target is MySQL. Use VALUES(col) for MariaDB or mixed MySQL/MariaDB fleets.

Keyset Pagination

SELECT id, name, created_at
FROM products
WHERE (created_at, id) < (?, ?)
ORDER BY created_at DESC, id DESC
LIMIT 50;

…

Häufige Fragen

Was sind 'Muster' in diesem Zusammenhang?

Muster sind bewährte, empfohlene Lösungen für typische Probleme bei der Datenbanknutzung, etwa bei der Organisation von Daten oder beim Beschleunigen von Suchen.

Warum muss ich auf die MySQL-/MariaDB-Version achten?

Verschiedene Versionen können kleine Unterschiede beim Verstehen von SQL-Befehlen haben. Deshalb ist es wichtig zu wissen, welche Version du verwendest, damit du das richtige Muster anwendest.

Was ist 'Keyset-Pagination'?

Das ist eine effiziente Methode, Daten seitenweise abzurufen, besonders bei großen Tabellen. Sie ist schneller als die traditionelle Methode mit 'Offset'.

Details

Originalbeschreibung des Autors

MySQL and MariaDB schema, query, indexing, transaction, replication, and connection-pool patterns for production backends. Use when designing MySQL or MariaDB schemas and indexes, or when a query, transaction, or replica lags.

repo: affaan-m/ECC

Beliebtheit

GitHub-Sterne
280k
Platz im Ranking
#215
Lizenz
MIT
Aktualisiert
15.10.2026

Ist dieser Skill nützlich?

0

Anmelden um abzustimmen.

Kommentare

  • …

Weitere Skills aus diesem Repository

Ähnliche Skills