Blog

Why some statements are missing from your query reports

Your application is busy. The database server is working hard, and you know which queries the application runs most often. You open your monitoring tool, look at the list of top queries, and the queries you expected are not there. The list shows COMMIT, a few SET statements, and some background queries, but not the main workload. The server is not losing those queries. It runs them. They are simply recorded in a way that query reports built on the Performance Schema cannot see. This post explains why, and what you can do about it.

How query reports usually work

The Performance Schema is a built-in MySQL feature that collects statistics about what the server does. One of its most useful tables is events_statements_summary_by_digest. Many monitoring tools, dashboards, and the views in the sys schema read their top queries lists from this table, directly or indirectly. The table groups queries by their digest. A digest is a normalized form of the query text, with the literal values replaced by ?. For example, SELECT c FROM t WHERE id = 1 and SELECT c FROM t WHERE id = 2 have the same digest, SELECT c FROM t WHERE id = ?. The server adds the counts and times of all queries with the same digest into one row.

The important detail is when the digest is created. The server computes it while it parses the query text, at the start of a normal query. If a statement event has no digest, the server has nothing to group it by, so it does not add the event to the digest table at all.

Two kinds of prepared statements

A prepared statement is a query that is sent to the server once, with placeholders, and then run many times with different values. MySQL supports two kinds of prepared statements, and they behave differently in the Performance Schema. The first kind uses the binary protocol. The client sends a prepare command with the query text, and then an execute command with only the statement number and the parameter values. Many database connectors use this kind when server-side prepared statements are enabled.

The second kind is SQL-level. The client sends the statements PREPARE s1 FROM '...', EXECUTE s1 USING @a, and DEALLOCATE PREPARE s1 as ordinary text queries. This kind is less common in applications, but it is used in scripts, stored procedures, and some tools.

Where binary protocol statements disappear

When the server prepares a binary protocol statement, it does parse the query text. However, that parsing is done separately from the statistics for the prepare command, so the digest is not attached to it. In the Performance Schema, the prepare step is recorded as an event named statement/com/Prepare, with no digest and no SQL text. The execute step is even simpler. The statement was already parsed, so the server does not parse it again. It just fills in the values and runs the statement. The event is named statement/com/Execute, and again it has no digest and no SQL text.

Because neither event has a digest, neither of them is added to events_statements_summary_by_digest. This is the "disappearing query". An application that sends all of its queries as binary protocol prepared statements can be completely invisible in a digest-based report, even if it causes almost all of the load on the server.

Where SQL-level statements hide

SQL-level prepared statements do appear in the digest table, because PREPARE and EXECUTE arrive as ordinary text queries and are parsed normally. But the digest table records the text that was parsed, not the query that ran. In our test, the digest table showed these rows:

+-------------------------+-------+
| Digest text             | Count |
+-------------------------+-------+

| PREPARE `s1` FROM ?     |     1 |
| EXECUTE `s1` USING @?   |     2 |
| DEALLOCATE PREPARE `s1` |     1 |
+-------------------------+-------+

The actual SELECT statement, which did the real work, has no row of its own. Its time and row counts are recorded under EXECUTE `s1` USING @?. The digest text contains only the name of the prepared statement. If an application reuses the same name for different queries, those different queries can end up in the same row, and the report cannot tell them apart.


The slow query log tells a different story

The slow query log works differently. It records each command after it finishes. For binary protocol statements, the prepare step is written as # administrator command: Prepare;, without the query text. The execute step is written with the full query text, and the placeholders are replaced by the actual values, for example SELECT c FROM t WHERE id = 1;. Each execution is a separate entry with its own time and row counts. For SQL-level statements, PREPARE is written as PREPARE s1 FROM ...;, with the query text removed. Each EXECUTE, however, is written as the query that actually ran, with its values.\

How to tell if you are affected

A simple first check is to look at the server's statement counters. The status variables Com_stmt_prepare and Com_stmt_execute count prepare and execute commands. If Com_stmt_execute grows quickly while your digest-based report looks quiet, a large part of your workload is probably not visible in that report.

What you can do

Use the slow query log to see the queries. For binary protocol prepared statements, it is the place where the real query text and the per-execution timing appear. If you need a full picture and run Percona Server rather than MySQL, log_slow_rate_type=query and log_slow_rate_limit=N log one query in N, so you can combine it with long_query_time=0. For many workloads, a sample like this is enough to show which queries matter, including prepared statements.

Review your connector settings. Many connectors can either use server-side prepared statements or prepare the statement on the client and send a normal text query. With client-side preparation, the server receives ordinary queries, and they appear in the digest table as usual. However, it can affect performance and behavior.

Know what your tool reads. If a monitoring tool builds its query list from the digest table, it has this blind spot. A tool that reads the slow query log does not.

Summary

Prepared statements are not lost by MySQL. They are executed, timed, and written to the slow query log with their real text. But binary protocol prepared statements never receive a digest, so they never appear in events_statements_summary_by_digest, and any report built on that table cannot show them. If your query report seems to be missing your most important queries, check whether your application uses server-side prepared statements. If it does, the slow query log is where those queries can be found.

Take care of your MySQL performance.

MySQL audits available from only $129 per server. Learn More
blog comments powered by Disqus