2

Is there a table/place that stores all the historical queries being run on MySQL?

I want to do an analysis of the historical queries in order to determine what INDEX to create in each table.

FlyFly Wing
  • 109
  • 2
  • 9

1 Answers1

1

You can do that by creating slow_log or general_log tables.

MySQL Server provides a way to show the general query log and the slow query log, if those logs are enabled.

First, check if you already have the two tables slow_log and general_log existing in the MySQL database.

If you don't have them already - then you have to create them. Make sure that you are creating them in the MySQL database.

Create general_log table:

CREATE TABLE `general_log` (
    `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `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` mediumtext NOT NULL) ENGINE = CSV DEFAULT CHARSET = utf8 COMMENT = 'General log'

The general query log is a general record of what mysqld is doing.

There you will find information such as:

  • when clients connect or disconnect
  • each SQL statement received from clients

For slow_log table:

CREATE TABLE `slow_log` (
    `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `user_host` mediumtext NOT NULL,
    `query_time` time NOT NULL,
    `lock_time` time NOT NULL,
    `rows_sent` int(11) NOT NULL,
    `rows_examined` int(11) NOT NULL,
    `db` varchar(512) NOT NULL,
    `last_insert_id` int(11) NOT NULL,
    `insert_id` int(11) NOT NULL,
    `server_id` int(10) unsigned NOT NULL,
    `sql_text` mediumtext NOT NULL,
    `thread_id` bigint (21) unsigned NOT NULL) ENGINE = CSV DEFAULT CHARSET = utf8 COMMENT = 'Slow log'

The slow query log consists of SQL statements that take more than long_query_time seconds to execute and require at leastmin_examined_row_limit rows to be examined.

The slow query log can be used to find queries that take a long time to execute and are therefore candidates for optimization (indexation in your case).

Then you need to enable it (if you don't already have it enabled):

SET global general_log = 1;
SET global log_output = 'table'; 

Now you can view the log by running this query:

SELECT * FROM   mysql.general_log; 

If you want to disable query logging on the database, run this query

SET global general_log = 0; 

Please note that having this turned on comes with some caveats, such as consuming disk space and similar performance considerations, so you can turn it ON and OFF by need, and not keep it always ON.

Read more about these here:

Ben
  • 2,060
  • 9
  • 21
  • 1
    you should add in large letters that this will consume rapidly a lot of disk space – nbk Feb 04 '22 at 13:51
  • You are right, I updated my answer. – Ben Feb 04 '22 at 13:56
  • Hi @Ben, thank you for the help. I have opened MySQL and checked. The default data type for `sql_text` is BLOB, how can I deal with that? – FlyFly Wing Feb 05 '22 at 03:20
  • I'm not sure I understand your question. What is the problem there? – Ben Feb 05 '22 at 09:02
  • @Ben, I did not use `CREATE TABLE` statement, I just used SET global general_log = 1; SET global log_output = 'table'; But the sql_text column is using BLOB data type. I couldn't open it to view it. – FlyFly Wing Feb 05 '22 at 10:35
  • Okay, then you want to convert your blob to something readable. You can do that like this: `SELECT CONVERT(sql_text USING utf8) FROM general_log;` https://stackoverflow.com/questions/948174/how-do-i-convert-from-blob-to-text-in-mysql – Ben Feb 05 '22 at 11:05