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