Websites, applications and hosting for your business.
Recommended Services
Supported Scripts
Mr. Stig follows a gold thread to a specific drawer and examines its catalogue divider.

A page is slow, and the MariaDB slow query log contains a statement with a long execution time. That is useful evidence, but it does not yet connect the statement to the request the visitor is waiting for. It may belong to another task, run infrequently or account for only a small part of the total delay.

Build a verifiable chain: which request ran, which database connection it used, which SQL was recorded and what changed after the intervention. EXPLAIN helps identify unnecessary database work. Reduce that work while preserving the application’s correct result, then check whether the change matters to the original request.

Connect the application to the log entry

Record the time, controlled test route, request identifier and database connection ID. Where supported by the application, a safe identifier in an SQL comment can help correlation. It must not contain customer information or credentials. A connection pool may reuse one connection for multiple requests, so the connection ID alone is insufficient; retain the time window and request context too.

Also examine frequency. A 40-millisecond query repeated many times within one request may matter more than an isolated half-second task elsewhere. Those figures illustrate the reasoning rather than describe a measured installation. Count repetitions and assess their contribution to the actual request instead of choosing a statement solely because it has the largest individual duration.

Use a bounded logging window

Before enabling logging, record the current settings and agree on a duration, file destination and restoration procedure. The destination needs available space and appropriate access outside the public web directory. Logs can contain actual SQL values. Publishing a raw log to ask for help may disclose substantially more information than the investigation requires.

From MariaDB 10.11, the names log_slow_query and log_slow_query_time appear alongside the older slow_query_log and long_query_time aliases. Check the actual version and both global and session values. Changing a global default does not establish that every already-open connection has received the new session setting, particularly when the application retains connections.

SELECT VERSION();
SHOW GLOBAL VARIABLES LIKE 'slow_query_log%';
SHOW GLOBAL VARIABLES LIKE 'log_output';
SHOW GLOBAL VARIABLES LIKE 'long_query_time';
SHOW SESSION VARIABLES LIKE 'long_query_time';

Shared hosting may require the environment administrator to arrange logging. Owning a server does not remove the need for a bounded plan. Avoid indiscriminately logging every query without an index: the volume and logging overhead can materially change system behaviour. Decide which evidence is needed before increasing the amount of SQL being recorded.

A real local before-and-after example

We ran an isolated MariaDB 11.8.9 laboratory on Windows with PHP 8.5.12 and mysqli. The table contained 100,000 synthetic records. Two real local HTTP requests ran the same SELECT before and after an index was added. No customer database or website database was involved. A zero logging threshold deliberately captured both executions; it was a fixture setting, not a production recommendation.

SELECT id, created_at
FROM lab_orders
WHERE customer_id=42 AND status='paid'
ORDER BY created_at DESC
LIMIT 20;

The LAB-BEFORE and LAB-AFTER entries matched application connection IDs 9 and 12. Both returned exactly the same 20 records in the same order. Comparing the result matters: a faster statement that incorrectly omits or reorders data is not a successful optimisation. Establish equivalence before interpreting the execution statistics as a useful improvement.

Laboratory observationBeforeAfter
EXPLAIN accessALL, no selected indexref, customer_status_created
Rows_examined in the log100,02020
Query_time in the log0.009221 seconds0.000521 seconds
Result20 recordsThe same 20 records

This was one controlled run, not a production benchmark. Total local HTTP-handler time also included starting a PHP process. The Query_time difference therefore cannot be presented as the same improvement for a complete webpage, or as a promise for another server and workload. The experiment demonstrates a diagnostic chain and a changed access pattern.

EXPLAIN describes the plan, not every actual cost

Before the change, EXPLAIN showed a full scan and filesort. A composite index on customer_id, status and created_at enabled targeted access. Its column order relates to this SELECT’s filtering and sorting requirements. That is not a general instruction to place every WHERE column into a new index without examining existing indexes and the rest of the workload.

The new EXPLAIN estimated 34 rows, while the log recorded 20 examined rows. One is a planning estimate and the other an execution observation. They are not contradictory measurements of an identical quantity. If you use ANALYZE to obtain actual execution information, remember that it executes the statement instead of merely describing its anticipated plan.

Every index consumes space and adds work to relevant inserts and updates. Adding one to a large live table requires consideration of version, DDL behaviour, resources and possible waits. Arrange suitable backup and recovery checks where needed. A quick index build on a small synthetic table says little about the impact of a production schema change.

Return to the request that started the investigation

Repeat the original route with equivalent data, checking total duration, repeated queries and correct behaviour. If database work improves but the request still waits for an external API, the remaining delay needs a different investigation. Query and lock timings are evidence, rather than a complete map of every wait in the application.

Finally, restore the original logging settings and handle the file according to the agreed access and retention arrangements. In the laboratory, logging was disabled and local services were closed.

Related Stigmahost services

For a real project, Virtual Private Servers address infrastructure, while Web Development provides a route for targeted application work. Choose the service after establishing the diagnosis.

Share this Post