Fun with the MySQL pager command

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):

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:

Or \n:

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:

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:

Instead of manually comparing each row, you can calculate a checksum and only compare the checksum:

Hmmm, checksums don’t match, something is wrong. Let’s retry:

Checksums are identical, the rewritten query is much likely to produce the same result as the original one.


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:

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:

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:

and counting the number of connection for each status can be done with:

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


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.

Share this post

Comments (8)

  • Henk Jan Agteresch

    For the lazy admins, use \P. Saves a few keystrokes 🙂

    January 21, 2013 at 4:29 am
    • sakthi sri

      ha ha ha lolll!!!

      November 4, 2016 at 4:00 pm
  • Daniël van Eeden

    And for those who don’t use common_schema, Creating SQL which can be copied can be done with sed:

    dveeden@localhost [TEST] > pager sed -e ‘s/^|//’ -e ‘s/|$//’
    PAGER set to ‘sed -e ‘s/^|//’ -e ‘s/|$//”
    3 rows in set (0.00 sec)

    dveeden@localhost [TEST] > ALTER TABLE t1 ENGINE=InnoDB;
    Query OK, 0 rows affected (0.77 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    dveeden@localhost [TEST] > ALTER TABLE t2 ENGINE=InnoDB;
    Query OK, 0 rows affected (0.07 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    dveeden@localhost [TEST] > ALTER TABLE t3 ENGINE=InnoDB;
    Query OK, 0 rows affected (0.32 sec)
    Records: 0 Duplicates: 0 Warnings: 0

    January 21, 2013 at 8:27 am
  • Elaine

    Some slick ideas here. I have needed the /dev/null and the checksum ones; definitely welcome additions to my toolbox.

    January 22, 2013 at 12:56 pm
  • aftab

    pager md5sum doesn’t seem to work properly e.g.

    mysql> pager md5sum
    PAGER set to ‘md5sum’
    mysql> select pkCaseId from tblCase where pkCaseId=1;
    e8f6937bca3138aa2d09439a0450a146 –
    1 row in set (0.01 sec)

    mysql> select fkCaseId from tblCaseSegment where fkCaseId=1;
    906f654071b2b83572668b94b235c655 –
    1 row in set (0.00 sec)

    Both queries return same value i.e. 1 but why the checksum is different?

    February 15, 2013 at 9:41 am
    • Master Elgorath (@Elgorath)

      Row header for one query is pkCaseId and for the other query the row header is fkCaseId so the output checksum is different, try select fkCaseId as pkCaseId from tblCaseSegment where fkCaseId=1;

      September 16, 2019 at 6:15 am
  • Stephane Combaudon


    The name of the column is passed to md5sum along with the value, that’s why you get different checksums.

    For instance: