Fun with the MySQL pager command
Join the DZone community and get the full member experience.
Join For FreeThis post comes from Stephane Combaudon at the MySQL Performance Blog.
Last time I wrote about a few tips that can make you more efficient when using the command line on Unix. Today I want to focus more on pager.The most common usage of pager is to set it to a Unix pager such as
less. It can be very useful to view the result of a command spanning
over many lines (for instance SHOW ENGINE INNODB STATUS
):
mysql> pager less PAGER set to 'less' mysql> show engine innodb status\G [...]
Now you are inside less
and you can easily navigate through the result set (use q to quit, space to scroll down, etc).
Reminder: if you want to leave your custom pager, this is easy, just run pager
:
mysql> pager Default pager wasn't set, using stdout.
Or \n
:
mysql> \n PAGER set to stdout
But the pager command is not restricted to such basic usage! You can pass the output of queries to most Unix programs that are able to work on text. We have discussed the topic, but here are a few more examples.
Discarding the result set
Sometimes you don’t care about the result set, you only want to see timing information. This can be true if you are trying different execution plans for a query by changing indexes. Discarding the result is possible with pager:
mysql> pager cat > /dev/null PAGER set to 'cat > /dev/null' # Trying an execution plan mysql> SELECT ... 1000 rows in set (0.91 sec) # Another execution plan mysql> SELECT ... 1000 rows in set (1.63 sec)
Now it’s much easier to see all the timing information on one screen.
Comparing result sets
Let’s say you are rewriting a query and you want to check if the result set is the same before and after rewrite. Unfortunately, it has a lot of rows:
mysql> SELECT ... [..] 989 rows in set (0.42 sec)
Instead of manually comparing each row, you can calculate a checksum and only compare the checksum:
mysql> pager md5sum PAGER set to 'md5sum' # Original query mysql> SELECT ... 32a1894d773c9b85172969c659175d2d - 1 row in set (0.40 sec) # Rewritten query - wrong mysql> SELECT ... fdb94521558684afedc8148ca724f578 - 1 row in set (0.16 sec)
Hmmm, checksums don’t match, something is wrong. Let’s retry:
# Rewritten query - correct mysql> SELECT ... 32a1894d773c9b85172969c659175d2d - 1 row in set (0.17 sec)
Checksums are identical, the rewritten query is much likely to produce the same result as the original one.
Cleaning up SHOW PROCESSLIST
If you have lots of connections on your MySQL, it’s very difficult to read the output of SHOW PROCESSLIST
.
For instance, if you have several hundreds of connections and you want
to know how many connections are sleeping, manually counting the rows
from the output of SHOW PROCESSLIST
is probably not the best solution. With pager, it is straightforward:
mysql> pager grep Sleep | wc -l PAGER set to 'grep Sleep | wc -l' mysql> show processlist; 337 346 rows in set (0.00 sec)
This should be read as ’337 out 346 connections are sleeping’.
Slightly more complicated now: you want to know the number of connections for each status:
mysql> pager awk -F '|' '{print $6}' | sort | uniq -c | sort -r PAGER set to 'awk -F '|' '{print $6}' | sort | uniq -c | sort -r' mysql> show processlist; 309 Sleep 3 2 Query 2 Binlog Dump 1 Command
Astute readers will have noticed that these questions could have been solved by querying INFORMATION_SCHEMA. For instance, counting the number of sleeping connections can be done with:
mysql> SELECT COUNT(*) FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND='Sleep'; +----------+ | COUNT(*) | +----------+ | 320 | +----------+
and counting the number of connection for each status can be done with:
mysql> SELECT COMMAND,COUNT(*) TOTAL FROM INFORMATION_SCHEMA.PROCESSLIST GROUP BY COMMAND ORDER BY TOTAL DESC; +-------------+-------+ | COMMAND | TOTAL | +-------------+-------+ | Sleep | 344 | | Query | 5 | | Binlog Dump | 2 | +-------------+-------+
True, but:
- It’s nice to know several ways to get the same result
- Some of you may feel more comfortable with writing SQL queries, while others will prefer command line tools
Conclusion
As you can see, pager is your friend! It’s very easy to use and it can solve problems in an elegant and very efficient way. You can even write your custom script (if it is too complicated to fit in a single line) and pass it to the pager.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments