How to get the last 10 queries by table name in MySQL
Last 10 queries
I had a situation where I wanted to check if some SQL changes I made were working properly.
It involved a transaction with a bunch of queries. Yes I could verify the data but I wanted to visually see the queries that my scripts were generating.
Turning on the general query log was not an option for me because of the amount of queries that get run every second.
During my DB research I found MySQL’s performance schema.
This query here will show you the last 10 statements that were ran for a particular table in a particular database.
I did see that if you do not prefix your tables with the db name in your query you’ll have to remove the line that looks for the db table.
SELECT event_id, event_name, sql_text, timer_wait FROM performance_schema.events_statements_history_long WHERE sql_text LIKE '%TABLE_NAME%' AND sql_text LIKE '%DATABASE_NAME%' ORDER BY timer_start DESC LIMIT 10;
MySQL’s performance schema tables look awesome. I feel like there is a lot of untapped awesomness there that I need to research to help me as a programmer as well as helping me when I wear by DBA hat.