mariadb-query-optimization

Diagnose and optimize slow MariaDB queries using EXPLAIN, indexing, and histogram statistics.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-query-optimization-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-query-optimization
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/topical/mariadb-query-optimization
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-query-optimization-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow MariaDB queries often stem from missing indexes, stale statistics, OFFSET pagination, or outdated assumptions about the optimizer. This Skill provides version-accurate guidance for diagnosing and fixing query performance problems without falling into common traps like applying MySQL syntax to MariaDB. ## Core Features & Use Cases - EXPLAIN and ANALYZE analysis: Interpret query plans, spot red flags like full table scans and filesort, and compare estimated versus actual row counts. - Indexing strategies: Apply the leftmost prefix rule, build covering indexes, use IGNORED indexes for testing, and replace MySQL functional indexes with generated columns. - Pagination and statistics: Replace OFFSET with cursor-based pagination and keep histogram statistics fresh with ANALYZE TABLE. - Use Case: A developer notices a paginated endpoint slowing down as data grows. The Skill identifies the OFFSET anti-pattern, rewrites it as a cursor-based query, and recommends a composite index matching the filter and sort columns. ## Quick Start Ask the AI to analyze a slow MariaDB query and suggest index and rewrite improvements based on your server version.

Frequently Asked Questions about mariadb-query-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize a slow query in MariaDB?▼

Start by running EXPLAIN on the slow query and check for type=ALL, NULL keys, or Using filesort in the output. Then run ANALYZE TABLE to refresh statistics, verify index column order matches your predicates, and use ANALYZE to compare estimated versus actual row counts.

How to read EXPLAIN output in MariaDB?▼

Check the type field for ALL (full table scan), key for NULL (no index used), rows for high scan estimates, and Extra for Using filesort or Using temporary. Using index in Extra indicates a covering index, which is the desired outcome.

Does MariaDB support functional indexes like MySQL?▼

MariaDB does not support MySQL's functional key part syntax with doubled parentheses. Instead, create a VIRTUAL generated column with the expression and add an index on that column, then query the generated column directly.

Why is OFFSET pagination slow on large tables?▼

OFFSET scans and discards all skipped rows on every page load, so LIMIT 10 OFFSET 50000 reads 50,010 rows. Cursor-based pagination using WHERE id < last_id seeks directly to the position via the index and stays fast at any depth.

Can I enable Performance Schema at runtime in MariaDB?▼

No, Performance Schema is disabled by default and cannot be enabled at runtime. You must set performance_schema=ON in my.cnf and restart the server, then enable the consumers and instruments you need.

When should I not add an index in MariaDB?▼

Avoid indexing low-cardinality columns like booleans or two-value status fields, since the optimizer prefers a table scan anyway. Also skip indexes on very small tables and rarely queried write-heavy columns, since every index slows INSERT, UPDATE, and DELETE.