Neat tricks for the MySQL command-line pager

PREVIOUS POST
NEXT POST

How many of you use the mysql command-line client?  And did you know about the pager command you can give it?  It’s pretty useful.  It tells mysql to pipe the output of your commands through the specified program before displaying it to you.

Here’s the most basic thing I can think of to do with it: use it as a pager.  (It’s scary how predictable I am sometimes, isn’t it?)

For big result sets, it’s a pretty handy way to be able to search and scroll through. No mouse required, of course.

But it doesn’t have to be this simple! You can specify anything you want as a pager. Hmm, you know what that means? It means you can write your own script and push the output through it. You can’t specify arguments to the script, but since you can write your own, that’s not really a limitation.(Edit: I’m wrong! You can. See Giuseppe’s comment below.) For example, here’s a super-simple script that will show the lock waits in the output of SHOW INNODB STATUS. Save this file as /tmp/lock_waits and make it executable.

Now in your mysql session, set /tmp/lock_waits as your pager and let’s see if there are any lock waits:

Pretty useful, isn’t it? But we can do even more. For example, the Maatkit tools are specifically designed to be useful at the command line in the traditional Unix pipe-and-filter manner. What sort of goodies can we think of here?

Now, that’s handy.

What are your favorite ideas?

PREVIOUS POST
NEXT POST

Comments

  1. says

    Cool tricks. My most frequent usage of “pager” is “vim -” and then manipulate the output at will.

    I have a handy script that gives me the size of indexes in a database from a SHOW TABLE STATUS. (Useful when you need to calculate how much memory to allocate for a buffer).

    $ cat index_size.sh
    perl -lane ‘$i+= $F[17];END{print $i}’

    mysql> pager ./index_size.sh
    PAGER set to ‘./index_size.sh’
    mysql> show table status;
    71680

    mysql> nopager

    Giuseppe

  2. says

    This is cool. I have known (and ignored) this earlier, my bad. But something with mk-visual-explain (one of my favorite tools) is just great.

    Next, I would like to see some regex based piping that I can specify in my.cnf, something like

    pager ^explain = mk-visual-explain
    pager ^show engine = less
    pager .* = stdout

    I dont think I’m asking too much :)


    Parvesh

  3. says

    This is one I use a bit, requires a Unix/Linux OS though. You could do similar on MySQL on Windows.

    pager grep -v “Sleep” |sort -n -t “|” -k 7 |tail

    show processlist will now suppress sleeping sessions and display the top 10 oldest sessions.

    Thanks for the tip on passing any script… very nice. I have interesting tests to try out… or just use mytop eh?

    Have Fun

    Paul

  4. Clint Byrum says

    Parvesh, you can have your cake and eat it too.

    superpager.bash:

    #!/bin/bash
    teefile=~/.mysqltee

    goback=100

    # find the last ^mysql>

    lastcmd=$(tail -n $goback $teefile | grep “^mysql> “|tail -1| awk -F”mysql> ” ‘{print $2}’|sed -e ‘s/\*/\\*/g’)

    echo lastcmd=$lastcmd

    if echo “$lastcmd” | egrep -qi “^\s*explain” ; then
    exec mk-visual-explain
    fi
    if echo “$lastcmd” | egrep -qi “^\s*show\s+engine” ; then
    fgrep -C3 -i “LOCK” | less
    exit
    fi
    less

    One has to do a \T ~/.mysqltee before the \P superpager.bash , but thats nice because everything you do gets logged..

    Works reasonably well for something I hacked together with bash in 10 minutes. Would be nice if the pager could be passed args like “last command” but this is a decent enough hack until they implement that in version 6.1 of mysql. ;)

  5. says

    I use pager for two things:

    1) Save the results of the last query
    2) Replace EXPLAIN with mk-visual-explain

    Unfortunately, MySQL 5.0.51a and earlier writes to the tee file AFTER the pager has completed (and to .mysql_history only after you exit mysql), so getting the last command is impossible via these methods (however, it works if you are willing to issue queries twice …). You *can* still override commands (EXPLAIN, for example) by having scripts that error if the input does not conform to a standard; an example follows:

    #!/usr/bin/perl

    use strict;
    use warnings FATAL => ‘all';
    use English qw ( -no_match_vars );

    my $out;

    while ( my $line = ) {

    $out .= $line;

    }

    # Log results to a file (always useful)
    # But only ever save one :)
    open (PAGER_LOG, ‘>/tmp/mysql_pager.log’);
    print PAGER_LOG “$out\n”;
    close (PAGER_LOG);

    # See if it an EXPLAIN
    eval { local $SIG{‘__WARN__’}; system(‘/usr/local/bin/mk-visual-explain /tmp/mysql_pager.log 2> /dev/null’); };

    if ($?) {

    print $out;

    }

  6. Mark says

    Good stuff.

    Is there a way to set vi-style command editing for the mysql command line client? That would be super useful.

  7. Amos says

    > pager column -t | less -S
    meaning you don’t have to look at wrapped rows, you can just scroll right and left

  8. Amos says

    Actually, I correct myself. 2 nice options for aligned nowrap browsing

    mysql> pager less -S

    and

    mysql> pager vim -R -c “set nowrap” -

Leave a Reply

Your email address will not be published. Required fields are marked *