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?)
mysql> pager less mysql> show innodb status\G
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.
#!/bin/sh grep -A 1 'TRX HAS BEEN WAITING'
Now in your mysql session, set /tmp/lock_waits as your pager and let’s see if there are any lock waits:
mysql> pager /tmp/lock_waits PAGER set to '/tmp/lock_waits' mysql> show innodb status\G ------- TRX HAS BEEN WAITING 50 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 0 page no 52 n bits 72 index `GEN_CLUST_INDEX` of table `test/t` trx id 0 14615 lock_mode X waiting 1 row in set, 1 warning (0.00 sec)
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?
mysql> pager mk-visual-explain PAGER set to 'mk-visual-explain' mysql> explain select * from sakila.film inner join sakila.film_actor using(film_id) inner join sakila.actor using(actor_id); JOIN +- Bookmark lookup | +- Table | | table actor | | possible_keys PRIMARY | +- Unique index lookup | key actor->PRIMARY | possible_keys PRIMARY | key_len 2 | ref sakila.film_actor.actor_id | rows 1 +- JOIN +- Bookmark lookup | +- Table | | table film_actor | | possible_keys PRIMARY,idx_fk_film_id | +- Index lookup | key film_actor->idx_fk_film_id | possible_keys PRIMARY,idx_fk_film_id | key_len 2 | ref sakila.film.film_id | rows 2 +- Table scan rows 1022 +- Table table film possible_keys PRIMARY 3 rows in set (0.00 sec)
Now, that’s handy.
What are your favorite ideas?