MySQL check query execution time, number of executions - Using general log

How to get MySQL Query Time using general log data

Quickly finding the query execution time can be very helpful during scenarios such as load tests. Below are simple set of steps to extract the query time , number of execution based on general log.
  • Set general_log and log_output parameters, this should log all queries to mysql.general_log table
  • SET global general_log = 1;
    SET global log_output = 'table';
  • Create a table with id column included to general log table
  • CREATE TABLE `test.general_log_id` ( id MEDIUMINT NOT NULL AUTO_INCREMENT, `event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `user_host` mediumtext NOT NULL, `thread_id` bigint(21) unsigned NOT NULL, `server_id` int(10) unsigned NOT NULL, `command_type` varchar(64) NOT NULL, `argument` mediumblob NOT NULL, PRIMARY KEY (id) ) ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='General log';
  • Filter out the data from mysql.general_log and insert to create table above, e.g. below filters by load test app server
  • insert into general_log_id(id,event_time,user_host,thread_id,server_id,command_type,argument) select null,event_time,user_host,thread_id,server_id, command_type,argument from mysql.general_log where user_host like '%86.36%' order by event_time;
The inclusion of the ID column can be utilized in various scenarios, one of it is to find the query time for each query using below.
Note : This query time is the difference in which mysql started each of query execution, so this include time taken outside of the database as well.
select * from (SELECT A.id, A.event_time, (B.event_time - A.event_time) AS timedifference, A.argument FROM general_log_id A INNER JOIN general_log_id B ON B.id = (A.id + 1) ORDER BY A.id ASC) as C order by 3;

MySQL query time

How to get MySQL number of query execution

Same extracted data set can be used to get the number of query execution from filtered data set.
e.g. select queries with more than 5 execution
select argument as query, count(*) as noexe from general_log_id where command_type='query' and argument like '%SELECT%' group by query having count(*) >5 order by 2;

Other than the two listed above, above general log data with the addition of ID can be used in many scenarios.

Comments

Popular posts from this blog

ORA-16433: The database or pluggable database must be opened in read/write

Wait for unread message on broadcast channel - Blocking Sessions

ORA-14126 - While splitting max value partition