Statistics
MySQL Native Driver contains support for gathering statistics on the communication between the client and the server. The statistics gathered are of two main types: Client statisticsConnection statistics
MySQL Native Driver contains support for gathering statistics on the communication between the client and the server. The statistics gathered are of two main types:
- Client statistics
- Connection statistics
When using the mysqli extension, these statistics can be obtained through two API calls:
Statistics are aggregated among all extensions that use the MySQL Native Driver. For example, if the mysqli extension and the PDO MySQL driver are both set-up to use MySQLnd, then function calls from mysqli and method calls from PDO will affect the statistics.
There is no way to find out how much a certain API call of any extension that has been compiled against MySQL Native Driver has impacted a certain statistic.
Retrieving statistics
Client statistics can be retrieved by calling the mysqli_get_client_stats() function.
Connection statistics can be retrieved by calling the mysqli_get_connection_stats() function.
Both functions return an associative array, where the name of a statistic is the key for the corresponding statistical data.
MySQL Native Driver Statistics
Most statistics are associated to a connection, but some are associated to the process in which case this will be mentioned.
The following statistics are produced by the MySQL Native Driver:
bytes_sentNumber of bytes sent from PHP to the MySQL server.
bytes_receivedNumber of bytes received from the MySQL server.
packets_sentNumber of packets sent by the MySQL Client Server protocol.
packets_receivedNumber of packets received from the MySQL Client Server protocol.
protocol_overhead_inMySQL Client Server protocol overhead in bytes for incoming traffic. Currently only the Packet Header (4 bytes) is considered as overhead.
protocol_overhead_in = packets_received * 4protocol_overhead_outMySQL Client Server protocol overhead in bytes for outgoing traffic. Currently only the Packet Header (4 bytes) is considered as overhead.
protocol_overhead_out = packets_received * 4bytes_received_ok_packetTotal size of bytes of MySQL Client Server protocol OK packets received. OK packets can contain a status message. The length of the status message can vary and thus the size of an OK packet is not fixed.
NoteThe total size in bytes includes the size of the header packet (4 bytes, see protocol overhead).
packets_received_okNumber of MySQL Client Server protocol OK packets received.
bytes_received_eof_packetTotal size in bytes of MySQL Client Server protocol EOF packets received. EOF can vary in size depending on the server version. Also, EOF can transport an error message.
packets_received_eofNumber of MySQL Client Server protocol EOF packets.
Like with other packet statistics the number of packets will be increased even if PHP does not receive the expected packet but, for example, an error message.
bytes_received_rset_header_packetTotal size in bytes of MySQL Client Server protocol result set header packets. The size of the packets varies depending on the payload (
LOAD LOCAL INFILE,INSERT,UPDATE,SELECT, error message).packets_received_rset_headerNumber of MySQL Client Server protocol result set header packets.
bytes_received_rset_field_meta_packetTotal size in bytes of MySQL Client Server protocol result set metadata (field information) packets. Of course the size varies with the fields in the result set. The packet may also transport an error or an EOF packet in case of COM_LIST_FIELDS.
packets_received_rset_field_metaNumber of MySQL Client Server protocol result set metadata (field information) packets.
bytes_received_rset_row_packetTotal size in bytes of MySQL Client Server protocol result set row data packets. The packet may also transport an error or an EOF packet. One can compute the number of error and EOF packets by subtracting
rows_fetched_from_server_normalandrows_fetched_from_server_psfrombytes_received_rset_row_packet.packets_received_rset_rowNumber of MySQL Client Server protocol result set row data packets.
bytes_received_prepare_response_packetTotal size in bytes of MySQL Client Server protocol OK for Prepared Statement Initialization packets (prepared statement init packets). The packet may also transport an error. The packet size depends on the MySQL version.
packets_received_prepare_responseNumber of MySQL Client Server protocol OK for Prepared Statement Initialization packets (prepared statement init packets).
bytes_received_change_user_packetTotal size in bytes of MySQL Client Server protocol COM_CHANGE_USER packets. The packet may also transport an error or EOF.
packets_received_change_userNumber of MySQL Client Server protocol COM_CHANGE_USER packets.
packets_sent_commandNumber of MySQL Client Server protocol commands sent from PHP to MySQL.
There is no way to know which specific commands and how many of them have been sent.
bytes_received_real_data_normalNumber of bytes of payload fetched by the PHP client from
mysqlndusing the text protocol.This is the size of the actual data contained in result sets that do not originate from prepared statements and which have been fetched by the PHP client.
Note that although a full result set may have been pulled from MySQL by
mysqlnd, this statistic only counts actual data pulled frommysqlndby the PHP client.An example of a code sequence that will increase the value is as follows:
php$mysqli = new mysqli(); $res = $mysqli->query("SELECT 'abc'"); $res->fetch_assoc(); $res->close();Every fetch operation will increase the value.
However, the statistic will not be increased if the result set is only buffered on the client, but not fetched, such as in the following example:
php$mysqli = new mysqli(); $res = $mysqli->query("SELECT 'abc'"); $res->close();bytes_received_real_data_psNumber of bytes of the payload fetched by the PHP client from
mysqlndusing the prepared statement protocol.This is the size of the actual data contained in result sets that originate from prepared statements and which have been fetched by the PHP client.
The value will not be increased if the result set is not subsequently read by the PHP client.
Note that although a full result set may have been pulled from MySQL by
mysqlnd, this statistic only counts actual data pulled frommysqlndby the PHP client.See also
bytes_received_real_data_normal.
result_set_queriesNumber of queries that have generated a result set. Examples of queries that generate a result set:
SELECT,SHOW.The statistic will not be incremented if there is an error reading the result set header packet from the line.
NoteThis statistic can be used as an indirect measure for the number of queries PHP has sent to MySQL. This could help identifying a client that causes a high database load.
non_result_set_queriesNumber of queries that did not generate a result set. Examples of queries that do not generate a result set:
INSERT,UPDATE,LOAD DATA.The statistic will not be incremented if there is an error reading the result set header packet from the line.
no_index_usedNumber of queries that have generated a result set but did not use an index. (See also the mysqld start option
--log-queries-not-using-indexes).NoteThose queries can be reported via an exception by calling
mysqli_report(MYSQLI_REPORT_INDEX);. It is possible to have them be reported via a warning instead by callingmysqli_report(MYSQLI_REPORT_INDEX ^ MYSQLI_REPORT_STRICT);.bad_index_usedNumber of queries that have generated a result set and did not use a good index. (See also the mysqld start option
--log-slow-queries).slow_queriesSQL statements that took more than
long_query_timeseconds to execute and required at leastmin_examined_row_limitrows to be examined.CautionNot reported through
mysqli_report().buffered_setsNumber of buffered result sets returned by normal (i.e. not via a prepared statement) queries.
Examples of API calls that will buffer result sets on the client:
unbuffered_setsNumber of unbuffered result sets returned by normal (i.e. not via a prepared statement) queries.
Examples of API calls that will not buffer result sets on the client:
ps_buffered_setsNumber of buffered result sets returned by prepared statements.
Examples of API calls that will buffer result sets on the client:
ps_unbuffered_setsNumber of unbuffered result sets returned by prepared statements.
By default prepared statements are unbuffered, thus most prepared statements will be accounted in this statistic.
flushed_normal_setsNumber of result sets returned by normal (i.e. not via a prepared statement) queries with unread data that have been silently flushed.
NoteFlushing happens only with unbuffered result sets.
Unbuffered result sets must be fetched completely before a new query can be run on the connection otherwise MySQL will throw an error. If the application does not fetch all rows from an unbuffered result set, mysqlnd does implicitly fetch the result set to clear the line.
See also
rows_skipped_normal,rows_skipped_ps.Some possible causes for an implicit flush:
- Faulty client application
- Client stopped reading after it found what it was looking for but has made MySQL calculate more records than needed
- Client application has stopped unexpectedly
flushed_ps_setsNumber of result sets from prepared statements with unread data that have been silently flushed.
ps_prepared_never_executedNumber of statements prepared but never executed.
ps_prepared_once_executedNumber of prepared statements executed only once.
rows_fetched_from_server_normalrows_fetched_from_server_psTotal number of result set rows fetched from the server. This includes the rows which were not read by the client but had been implicitly fetched due to flushed unbuffered result sets.
See also
packets_received_rset_row.rows_buffered_from_client_normalTotal number of buffered rows originating from a normal query.
This is the number of rows that have been fetched from MySQL and buffered on client.
Examples of queries that will buffer results:
rows_buffered_from_client_psSame as
rows_buffered_from_client_normalbut for prepared statements.rows_fetched_from_client_normal_bufferedTotal number of rows fetched by the client from a buffered result set created by a normal query.
rows_fetched_from_client_ps_bufferedTotal number of rows fetched by the client from a buffered result set created by a prepared statement.
rows_fetched_from_client_normal_unbufferedTotal number of rows fetched by the client from an unbuffered result set created by a normal query.
rows_fetched_from_client_ps_unbufferedTotal number of rows fetched by the client from an unbuffered result set created by a prepared statement.
rows_fetched_from_client_ps_cursorTotal number of rows fetch by the client from a cursor created by a prepared statement.
rows_skipped_normalrows_skipped_psReserved for future use (currently not supported).
copy_on_write_savedcopy_on_write_performedThis is a process level scope statistic.
With mysqlnd, variables returned by the extensions point into mysqlnd internal network result buffers. If the data are not changed, the fetched data is kept only once in memory. However, any modification to the data will require mysqlnd to perform a copy-on-write operation.
explicit_free_resultimplicit_free_resultThis is a connection and process level scope statistic.
Total number of freed result sets.
proto_text_fetched_nullTotal number of columns of type
MYSQL_TYPE_NULLfetched from a normal query (MySQL text protocol).proto_binary_fetched_nullTotal number of columns of type
MYSQL_TYPE_NULLfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_bitTotal number of columns of type
MYSQL_TYPE_BITfetched from a normal query (MySQL text protocol).proto_binary_fetched_bitTotal number of columns of type
MYSQL_TYPE_BITfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_tinyintTotal number of columns of type
MYSQL_TYPE_TINYfetched from a normal query (MySQL text protocol).proto_binary_fetched_tinyintTotal number of columns of type
MYSQL_TYPE_TINYfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_shortTotal number of columns of type
MYSQL_TYPE_SHORTfetched from a normal query (MySQL text protocol).proto_binary_fetched_shortTotal number of columns of type
MYSQL_TYPE_SHORTfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_int24Total number of columns of type
MYSQL_TYPE_INT24fetched from a normal query (MySQL text protocol).proto_binary_fetched_int24Total number of columns of type
MYSQL_TYPE_INT24fetched from a prepared statement (MySQL binary protocol).proto_text_fetched_intTotal number of columns of type
MYSQL_TYPE_LONGfetched from a normal query (MySQL text protocol).proto_binary_fetched_intTotal number of columns of type
MYSQL_TYPE_LONGfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_bigintTotal number of columns of type
MYSQL_TYPE_LONGLONGfetched from a normal query (MySQL text protocol).proto_binary_fetched_bigintTotal number of columns of type
MYSQL_TYPE_LONGLONGfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_decimalTotal number of columns of type
MYSQL_TYPE_DECIMAL, orMYSQL_TYPE_NEWDECIMALfetched from a normal query (MySQL text protocol).proto_binary_fetched_decimalTotal number of columns of type
MYSQL_TYPE_DECIMAL, orMYSQL_TYPE_NEWDECIMALfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_floatTotal number of columns of type
MYSQL_TYPE_FLOATfetched from a normal query (MySQL text protocol).proto_binary_fetched_floatTotal number of columns of type
MYSQL_TYPE_FLOATfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_doubleTotal number of columns of type
MYSQL_TYPE_DOUBLEfetched from a normal query (MySQL text protocol).proto_binary_fetched_doubleTotal number of columns of type
MYSQL_TYPE_DOUBLEfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_dateTotal number of columns of type
MYSQL_TYPE_DATE, orMYSQL_TYPE_NEWDATEfetched from a normal query (MySQL text protocol).proto_binary_fetched_dateTotal number of columns of type
MYSQL_TYPE_DATE, orMYSQL_TYPE_NEWDATEfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_yearTotal number of columns of type
MYSQL_TYPE_YEARfetched from a normal query (MySQL text protocol).proto_binary_fetched_yearTotal number of columns of type
MYSQL_TYPE_YEARfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_timeTotal number of columns of type
MYSQL_TYPE_TIMEfetched from a normal query (MySQL text protocol).proto_binary_fetched_timeTotal number of columns of type
MYSQL_TYPE_TIMEfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_datetimeTotal number of columns of type
MYSQL_TYPE_DATETIMEfetched from a normal query (MySQL text protocol).proto_binary_fetched_datetimeTotal number of columns of type
MYSQL_TYPE_DATETIMEfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_timestampTotal number of columns of type
MYSQL_TYPE_TIMESTAMPfetched from a normal query (MySQL text protocol).proto_binary_fetched_timestampTotal number of columns of type
MYSQL_TYPE_TIMESTAMPfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_stringTotal number of columns of type
MYSQL_TYPE_STRING,MYSQL_TYPE_VARSTRING, orMYSQL_TYPE_VARCHARfetched from a normal query (MySQL text protocol).proto_binary_fetched_stringTotal number of columns of type
MYSQL_TYPE_STRING,MYSQL_TYPE_VARSTRING, orMYSQL_TYPE_VARCHARfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_blobTotal number of columns of type
MYSQL_TYPE_TINY_BLOB,MYSQL_TYPE_MEDIUM_BLOB,MYSQL_TYPE_LONG_BLOB, orMYSQL_TYPE_BLOBfetched from a normal query (MySQL text protocol).proto_binary_fetched_blobTotal number of columns of type
MYSQL_TYPE_TINY_BLOB,MYSQL_TYPE_MEDIUM_BLOB,MYSQL_TYPE_LONG_BLOB, orMYSQL_TYPE_BLOBfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_enumTotal number of columns of type
MYSQL_TYPE_ENUMfetched from a normal query (MySQL text protocol).proto_binary_fetched_enumTotal number of columns of type
MYSQL_TYPE_ENUMfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_setTotal number of columns of type
MYSQL_TYPE_SETfetched from a normal query (MySQL text protocol).proto_binary_fetched_setTotal number of columns of type
MYSQL_TYPE_SETfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_geometryTotal number of columns of type
MYSQL_TYPE_GEOMETRYfetched from a normal query (MySQL text protocol).proto_binary_fetched_geometryTotal number of columns of type
MYSQL_TYPE_GEOMETRYfetched from a prepared statement (MySQL binary protocol).proto_text_fetched_otherTotal number of columns of types
MYSQL_TYPE_*not listed previously fetched from a normal query (MySQL text protocol).NoteIn theory, this should always be
0.proto_binary_fetched_otherTotal number of columns of type
MYSQL_TYPE_*not listed previously fetched from a prepared statement (MySQL binary protocol).NoteIn theory, this should always be
0.
connect_successTotal number of successful connection attempts.
Noteconnect_successholds the sum of successful persistent and non-persistent connection attempts. Therefore, the number of successful non-persistent connection attempts isconnect_success - pconnect_success.pconnect_successTotal number of successful persistent connection attempts.
connect_failureTotal number of failed connection attempts.
reconnectThis is a process level scope statistic.
active_connectionsThis is a process level scope statistic.
Total number of active persistent and non-persistent connections.
NoteThe total number of active non-persistent connections is
active_connections - active_persistent_connections.active_persistent_connectionsThis is a process level scope statistic.
Total number of active persistent connections.
explicit_closeTotal number of explicitly closed connections.
Examples of code snippets that cause an explicit close
- php
$link = new mysqli(/* ... */); $link->close(/* ... */); - php
$link = new mysqli(/* ... */); $link->connect(/* ... */);
implicit_closeTotal number of implicitly closed connections.
Examples of code snippets that cause an implicit close
- php
$link = new mysqli(/* ... */); $link->real_connect(/* ... */); - php
unset($link) - Persistent connection: pooled connection has been created with real_connect and there may be unknown options set - close implicitly to avoid returning a connection with unknown options
- Persistent connection: ping/change_user fails and ext/mysqli closes the connection
- End of script execution: close connections that have not been closed by the user
disconnect_closeConnection failures indicated by the C API call
mysql_real_connectduring an attempt to establish a connection.in_middle_of_command_closeThis is a process level scope statistic.
A connection has been closed in the middle of a command execution (outstanding result sets not fetched, after sending a query and before retrieving an answer, while fetching data, while transferring data with LOAD DATA).
WarningUnless asynchronous queries are used, this should only happen if the PHP application terminated unexpectedly, and PHP shuts down the connection automatically.
init_command_executed_countTotal number of init command executions. For example:
mysqli_options(MYSQLI_INIT_COMMAND , $value).The number of successful executions is
init_command_executed_count - init_command_failed_count.init_command_failed_countTotal number of failed init commands.
com_quitcom_init_dbcom_querycom_field_listcom_create_dbcom_drop_dbcom_refreshcom_shutdowncom_statisticscom_process_infocom_connectcom_process_killcom_debugcom_pingcom_timecom_delayed_insertcom_change_usercom_binlog_dumpcom_table_dumpcom_connect_outcom_register_slavecom_stmt_preparecom_stmt_executecom_stmt_send_long_datacom_stmt_closecom_stmt_resetcom_stmt_set_optioncom_stmt_fetchcom_daemonTotal number of attempts to send a certain
COM_*command from PHP to MySQL.The statistics are incremented after checking the line and immediately before sending the corresponding MySQL client server protocol packet.
CautionIf MySQLnd fails to send the packet over the wire the statistics will not be decremented. In case of a failure MySQLnd emits a PHP warning Error while sending %s packet. PID=%d.
Usage examples
- Check if PHP sends certain commands to MySQL, for example, check if a client sends
COM_PROCESS_KILL - Calculate the average number of prepared statement executions by comparing
COM_EXECUTEwithCOM_PREPARE - Check if PHP has run any non-prepared SQL statements by checking if
COM_QUERYis zero - Identify PHP scripts that run an excessive number of SQL statements by checking
COM_QUERYandCOM_EXECUTE
- Check if PHP sends certain commands to MySQL, for example, check if a client sends
explicit_stmt_closeimplicit_stmt_closeThis is a process level scope statistic.
Total number of closed prepared statements.
NoteA prepared statement is always explicitly closed. The only time it's closed implicitly is when preparing it fails.
mem_emalloc_countmem_emalloc_ammountmem_ecalloc_countmem_ecalloc_ammountmem_realloc_countmem_realloc_ammountmem_efree_countmem_malloc_countmem_malloc_ammountmem_calloc_countmem_calloc_ammountmem_ealloc_countmem_ealloc_ammountmem_free_countThis is a process level scope statistic.
Memory management calls.
command_buffer_too_smallNumber of network command buffer extensions while sending commands from PHP to MySQL.
MySQLnd allocates an internal command/network buffer of mysqlnd.net_cmd_buffer_size bytes for every connection.
If a MySQL Client Server protocol command, e.g.
COM_QUERY(normal query), does not fit into the buffer, MySQLnd will grow the buffer to what is needed for sending the command. Whenever the buffer gets extended for one connectioncommand_buffer_too_smallwill be incremented by one.If MySQLnd has to grow the buffer beyond its initial size of mysqlnd.net_cmd_buffer_size bytes for almost every connection, considerations to increase the default size should be made to avoid re-allocations.
connection_reusedThe total number of times a persistent connection has been reused.