Database response time depends on how much work each request creates and how many requests compete for the server. For MySQL or MariaDB, performance optimization starts with the queries consuming that capacity, so an improvement reduces measured query cost rather than merely changing a setting.
A slow-query log identifies expensive statements, while an execution plan shows how the database finds and sorts their rows. A frequently repeated query can consume more total time than an occasional slow report. An index that matches the filter and ordering can remove a table scan without requiring more server memory.
A useful comparison keeps the data, query parameters, and application load similar before and after one change. An index also consumes disk space and adds work to inserts and updates, so a faster read must justify that cost. Administrative access to the server logs and permission to inspect the application tables are needed; schema changes belong in an approved maintenance window.
Steps to optimize MySQL or MariaDB performance:
- Connect to the application database with an account allowed to read its tables.
$ mysql -u user -p appdb
On MariaDB installations, the client may be named mariadb. user and appdb stand for the account and database being tuned. The SQL below uses an existing orders_perf table with id, customer_id, created_at, and total columns. These names represent the table and columns used by the selected application query.
- Record server pressure during a representative application load window.
SHOW GLOBAL STATUS WHERE Variable_name IN ('Threads_running','Innodb_buffer_pool_reads','Innodb_buffer_pool_read_requests');
Threads_running is a current count; the two InnoDB values are cumulative counters. Differences between samples over comparable intervals reveal physical reads relative to logical reads. Heavy physical reads can justify a buffer-pool investigation, but a single lifetime ratio does not establish a memory shortage. Increasing memory without spare host RAM can cause swapping.
Related: How to monitor performance metrics in MySQL or MariaDB
Related: How to tune InnoDB buffer pool size in MySQL or MariaDB - Check the active slow-query log destination.
SHOW GLOBAL VARIABLES WHERE Variable_name IN ('slow_query_log','slow_query_log_file','long_query_time','log_output');
A file-based collection needs slow_query_log=ON and FILE in log_output. The threshold determines which completed queries appear; an empty log does not prove that the server has no costly queries. If collection is disabled, the logging guide covers enabling it for a bounded observation window and restoring the previous settings.
Related: How to configure slow query log in MySQL or MariaDB - Inspect the slow log from the affected load window for a repeatedly costly query.
Slow logs can contain customer data and literal credentials. Do not share or analyze unredacted excerpts.
$ sudo tail -n 60 /var/lib/mysql/performance-optimize-slow.log ##### snipped ##### # Query_time: 0.010239 Lock_time: 0.000002 Rows_sent: 5 Rows_examined: 100005 use appdb; SET timestamp=1794182740; SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5; ##### snipped #####
The path comes from slow_query_log_file on the database host. Here, returning five rows required examining 100,005 rows. A short tail helps inspect entries; a summary of the full window is needed to rank repeated queries by accumulated time. mysqldumpslow on MySQL and mariadb-dumpslow on MariaDB can summarize file logs.
Related: How to analyze MySQL or MariaDB slow query log
Tool: MySQL Slow Query Log Analyzer - Run the selected query to record its result and elapsed time before tuning.
SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5;
+-------+-------+ | id | total | +-------+-------+ | 99008 | 90.07 | | 98008 | 80.07 | | 97008 | 70.07 | | 96008 | 60.07 | | 95008 | 50.07 | +-------+-------+ 5 rows in set (0.01 sec)
Elapsed time varies with cache warmth and concurrent activity. Several executions under the same conditions give a better baseline than one cold run.
- Examine the query plan for unnecessary scanning or sorting.
EXPLAIN SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5\G
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: orders_perf partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 49148 filtered: 10.00 Extra: Using where; Using filesorttype: ALL and key: NULL show a table scan in this MySQL plan. The rows value is an estimate, not an observed count. Using filesort describes a separate sorting operation and does not by itself mean that the sort used disk. These signals matter here because one customer's five newest orders should require only a small part of the table.
Related: How to use EXPLAIN to view query plans in MySQL or MariaDB - Inspect the table's existing indexes before adding another one.
SHOW INDEX FROM orders_perf\G
An index beginning with (customer_id, created_at) can support the equality filter followed by the date order. A separate index on each column is not equivalent. If a suitable index already exists, stale statistics or a different query shape may explain the plan; ANALYZE TABLE refreshes statistics and can change subsequent plan choices.
Related: How to create an index in MySQL or MariaDB
Related: How to optimize MySQL or MariaDB query performance - Add the composite index for the selected query.
Building the index requires INDEX permission and can wait for metadata locks, consume disk space, and add load on a busy table. A backup and enough free space are prerequisites for production schema changes. The rollback for this added index is DROP INDEX idx_orders_perf_customer_created ON orders_perf; if later workload measurements show a regression.
CREATE INDEX idx_orders_perf_customer_created ON orders_perf (customer_id, created_at);
This index applies to the query shown, not every slow statement. Routine OPTIMIZE TABLE is unnecessary for this fix and can rebuild an InnoDB table.
Related: How to optimize table structures in MySQL or MariaDB - Confirm that the indexed query reads fewer rows with the server's runtime plan command.
Runtime plan commands execute the query. A costly read still adds load; ANALYZE on MariaDB also accepts data-changing statements, so the read-only SELECT is intentional.
EXPLAIN ANALYZE SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5\G
On MySQL 8.0.18 and newer, the tree should show an index lookup using idx_orders_perf_customer_created and only five actual rows read for this data. Its actual time values are in milliseconds. On MariaDB, use the following statement instead; r_rows reports actual rows read, while rows remains an estimate.
ANALYZE SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5\G
- Compare server pressure over an equivalent load window after the index change.
SHOW GLOBAL STATUS WHERE Variable_name IN ('Threads_running','Innodb_buffer_pool_reads','Innodb_buffer_pool_read_requests');
Comparable intervals exclude index-building time and use similar request rates. Sustained high concurrency or physical reads after query tuning warrant separate connection or memory work, rather than an arbitrary increase to max_connections or the buffer pool.
Related: How to limit active connections in MySQL or MariaDB
Related: How to optimize MySQL or MariaDB server performance - Re-run the original query under the baseline load to confirm the same result at lower cost.
SELECT id, total FROM orders_perf WHERE customer_id = 7 ORDER BY created_at DESC LIMIT 5;
+-------+-------+ | id | total | +-------+-------+ | 99008 | 90.07 | | 98008 | 80.07 | | 97008 | 70.07 | | 96008 | 60.07 | | 95008 | 50.07 | +-------+-------+ 5 rows in set (0.00 sec)
The five results match the baseline and the runtime plan uses the new index. A displayed 0.00 sec is rounded, not zero execution time. The change earns its place when repeated measurements improve application latency or throughput without a write-performance regression.