Yes, you can view recent query history using the performance_schema in MySQL by querying the events_statements_history or events_statements_history_long tables.
First, check that performance_schema is turned on. By default, it is enabled in newer versions of MySQL. You can verify it by executing the following query:
SHOW VARIABLES LIKE 'performance_schema';
If it's turned off, you'll need to enable it by setting performance_schema=ON in the MySQL configuration file (my.cnf or my.ini) and restarting the server.
To view the most recent SQL queries executed by active sessions, query the events_statements_history table. This table captures a limited number of recent statements.
SELECT event_id, sql_text, timer_wait, current_schema
FROM performance_schema.events_statements_history
ORDER BY event_id DESC;
If you need to view more queries than what is stored in the default history table, you can query the events_statements_history_long table, which maintains a longer history of executed queries.
SELECT event_id, sql_text, timer_wait, current_schema
FROM performance_schema.events_statements_history_long
ORDER BY event_id DESC
LIMIT 10;
Note that the events_statements_history table retains only a limited number of queries (default: 10). For longer histories, use events_statements_history_long (default: 100 rows);
however, these limits can be configured. In addition, capturing extensive query histories may affect server performance.
If you want to deepen your understanding of database concepts and learn about essential database functions, and advanced SQL data manipulation techniques, such as insertion, deletion, and retrieval, go to the
MySQL tutorials.