Monitoring Using the MySQL ShellMySQL Shell is the advanced MySQL client, which has many excellent features. In this blog, I am going to explain the MySQL shell commands “\show” and “\watch”. Both commands are very useful to monitor the MySQL process. It provides more insights into the foreground and background threads as well. 

Overview

“\show” and “\watch” are the MySQL shell commands, which can be executed using the Javascript (JS), Python (Py), and SQL interfaces. Both commands are providing the same information, but the difference is you can refresh the results when using the command “\watch”. The refresh interval is two seconds. 

  • \show: Run the specified report using the provided options and arguments.
  • \watch: Run the specified report using the provided options and arguments, and refresh the results at regular intervals.

Below are the available options you can use with the “\show” or “\watch” command to retrieve the data.

  • Query
  • Thread
  • Threads

“\show” with “query”

It will just execute the query provided as an argument within the double quotes and print the result. 

You can also use the same option with the “\watch” command. Let’s say, if you want to monitor the processlist for every two seconds, then you can use the command like

“\show” with “thread”

This option is designed to provide various information about the specific thread. Below are some of the important details you can retrieve from the specific thread. 

  • InnoDB details ( –innodb )
  • Locks Details ( –locks )
  • Prepared statement details ( –prep-stmts )
  • Client connection details ( –client )
  • Session status ( –status ) and session variables details ( –vars )

Example:

I am going to show the example for the below scenario. 

At session1:

My connection id is 121. I have started the transaction and updated the row where “id=3”. But, still not committed or rolled back the transaction.

At session 2:

My connection id is 123. I have started the transaction and tried to update the same row where “id=3”. The query is still executing because the transaction from session 1 is blocking the row ( id = 3 )

Now let’s use the command “\show thread” for both connection IDs (121, 123) and see what information we can get.

General information ( conncetion id = 123 ):

From the general information, you can find some basic information about your id.

InnoDB information:

Using the “–innodb” option, you can find out the information about the InnoDB like transaction state,  thread start time, elapsed time, locked tables, rows, modified rows. 

Locks information:

For connection id 123:

Connection id 123 is from session 2. Which is currently waiting to release the lock from connection id 121 (session 1). Let’s see the “–locks” status for connection id 121.

Here, you can find the details on “Blocking InnoDB Locks”. It blocks the connection id 123 (session 2).

Like the above example, you can explore the other options as well, which are helpful. 

“\show” with “threads”

This is very helpful to know the details about your ongoing threads. It will provide the details about both “FOREGROUND” and “BACKGROUND” threads. There are many columns, which are very useful to know about thread status. You can filter the needed columns with the option “-o”. By executing the command “\show threads –help”, you can find all the available options and their purposes. 

  • It supports the WHERE clause for generating the report
  • It supports ORDER BY for generating the report
  • It supports LIMIT for generating the report. 

Below, I am sharing some examples, which will help you to understand how we can use the “threads” command with the MySQL shell.

  • How to find the running “FOREGROUND” threads details
  • How to find the running “BACKGROUND” threads details
  • How to find the top five threads, which are consuming more memory from a particular user
  • How to find the Query digest details from ongoing threads
  • How to find the top five threads which consumed huge IO operations
  • How to find the top five blocked and blocking threads

I am running the sysbench against the server to get my database loaded. 

How to Find the Running “FOREGROUND” Threads Details

You can use the option “–foreground” to see all the running foreground threads.

How to Find the Running “BACKGROUND” Threads Details

This will give detailed information about the background threads, mostly InnoDB. You can use the flag “–background” to get these details. These details will be really helpful for debugging the performance issues.

How to Find the Top Five Threads, Which are Consuming More Memory From a Particular User

From the below example, I am finding the top five threads, which are consuming more memory from user “root”. 

How to Find the Query Digest Details From Ongoing Threads

You can use the options “digest” and “digesttxt” to find the digest output of the running threads.

How to Find the Top Five Threads Which Consumed Huge IO Operations

Nio → Total number of IO events for the thread.

How to Find the Top Five Blocked and Blocking Threads

  • nblocked  – The number of other threads blocked by the thread
  • nblocking – The number of other threads blocking the thread
  • Ntxrlckd   – The approximate number of rows locked by the current InnoDB transaction

Blocking threads:

Blocked threads:

Like this, you have many options to explore and you can generate the report based on your requirements. I hope this blog post is helpful to understand the “\show” and “\watch” commands from the MySQL shell!

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Andre

Thanks for the nice article. Is it also possible to list the top 5 CPU consuming threads?