pager 命令

https://www.percona.com/blog/2013/01/21/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):

 
mysql> pager less;
PAGER set to 'less'
mysql> show engine innodb status\G *************************** . row ***************************
Type: InnoDB
Name:
Status:
=====================================
-- :: 2ab7b4f90940 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: srv_active, srv_shutdown, srv_idle
srv_master_thread log flush and writes:
----------
SEMAPHORES
----------
----------
MUTEX INFO
----------
Locked mutex: addr 0x1829940 thread file /usr/src/mysql-5.6./storage/innobase/handler/ha_innodb.cc line
Locked mutex: addr 0x18298c0 thread file /usr/src/mysql-5.6./storage/innobase/srv/srv0srv.cc line
Total number of mutexes
-------------
:

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:

 
 
 
 恢复默认
 
1.
mysql> pager
Default pager wasn't set, using stdout.

2.\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 > /root/.txt
mysql> pager cat > /dev/null
PAGER set to 'cat > /dev/null'
mysql> show engine innodb status\G
row in set (0.03 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 * from  COLLATIONS       ;
+--------------------------+--------------------+-----+------------+-------------+---------+
| COLLATION_NAME | CHARACTER_SET_NAME | ID | IS_DEFAULT | IS_COMPILED | SORTLEN |
+--------------------------+--------------------+-----+------------+-------------+---------+
| big5_chinese_ci | big5 | | Yes | Yes | |
| big5_bin | big5 | | | Yes | |
| dec8_swedish_ci | dec8 | | Yes | Yes | |
| dec8_bin | dec8 | | | Yes | |
| cp850_general_ci | cp850 | | Yes | Yes | |
| cp850_bin | cp850 | | | Yes | |
| hp8_english_ci | hp8 | | Yes | Yes | |
| hp8_bin | hp8 | | | Yes | |
| koi8r_general_ci | koi8r | | Yes | Yes | |
| koi8r_bin | koi8r | | | Yes | |
| latin1_german1_ci | latin1 | | | Yes | |
| latin1_swedish_ci | latin1 | | Yes | Yes | |
| latin1_danish_ci | latin1 | | | Yes | |
| latin1_german2_ci | latin1 | | | Yes | |
| latin1_bin | latin1 | | | Yes | |
| latin1_general_ci | latin1 | | | Yes | |
| latin1_general_cs | latin1 | | | Yes | |
| latin1_spanish_ci | latin1 | | | Yes | |
| latin2_czech_cs | latin2 | | | Yes | |
| latin2_general_ci | latin2 | | Yes | Yes | |
| latin2_hungarian_ci | latin2 | | | Yes | |
| latin2_croatian_ci | latin2 | | | Yes | |
| latin2_bin | latin2 | | | Yes | |
| swe7_swedish_ci | swe7 | | Yes | Yes | |
| swe7_bin | swe7 | | | Yes | |
| ascii_general_ci | ascii | | Yes | Yes | |
| ascii_bin | ascii | | | Yes | |
| ujis_japanese_ci | ujis | | Yes | Yes | |
| ujis_bin | ujis | | | Yes | |
| sjis_japanese_ci | sjis | | Yes | Yes | |
| sjis_bin | sjis | | | Yes | |
| hebrew_general_ci | hebrew | | Yes | Yes | |
| hebrew_bin | hebrew | | | Yes | |
| tis620_thai_ci | tis620 | | Yes | Yes | |
| tis620_bin | tis620 | | | Yes | |
| euckr_korean_ci | euckr | | Yes | Yes | |
| euckr_bin | euckr | | | Yes | |
| koi8u_general_ci | koi8u | | Yes | Yes | |
| koi8u_bin | koi8u | | | Yes | |
| gb2312_chinese_ci | gb2312 | | Yes | Yes | |
| gb2312_bin | gb2312 | | | Yes | |
| greek_general_ci | greek | | Yes | Yes | |
| greek_bin | greek | | | Yes | |
| cp1250_general_ci | cp1250 | | Yes | Yes | |
| cp1250_czech_cs | cp1250 | | | Yes | |
| cp1250_croatian_ci | cp1250 | | | Yes | |
| cp1250_bin | cp1250 | | | Yes | |
| cp1250_polish_ci | cp1250 | | | Yes | |
| gbk_chinese_ci | gbk | | Yes | Yes | |
| gbk_bin | gbk | | | Yes | |
| latin5_turkish_ci | latin5 | | Yes | Yes | |
| latin5_bin | latin5 | | | Yes | |
| armscii8_general_ci | armscii8 | | Yes | Yes | |
| armscii8_bin | armscii8 | | | Yes | |
| utf8_general_ci | utf8 | | Yes | Yes | |
| utf8_bin | utf8 | | | Yes | |
| utf8_unicode_ci | utf8 | | | Yes | |
| utf8_icelandic_ci | utf8 | | | Yes | |
| utf8_latvian_ci | utf8 | | | Yes | |
| utf8_romanian_ci | utf8 | | | Yes | |
| utf8_slovenian_ci | utf8 | | | Yes | |
| utf8_polish_ci | utf8 | | | Yes | |
| utf8_estonian_ci | utf8 | | | Yes | |
| utf8_spanish_ci | utf8 | | | Yes | |
| utf8_swedish_ci | utf8 | | | Yes | |
| utf8_turkish_ci | utf8 | | | Yes | |
| utf8_czech_ci | utf8 | | | Yes | |
| utf8_danish_ci | utf8 | | | Yes | |
| utf8_lithuanian_ci | utf8 | | | Yes | |
| utf8_slovak_ci | utf8 | | | Yes | |
| utf8_spanish2_ci | utf8 | | | Yes | |
| utf8_roman_ci | utf8 | | | Yes | |
| utf8_persian_ci | utf8 | | | Yes | |
| utf8_esperanto_ci | utf8 | | | Yes | |
| utf8_hungarian_ci | utf8 | | | Yes | |
| utf8_sinhala_ci | utf8 | | | Yes | |
| utf8_german2_ci | utf8 | | | Yes | |
| utf8_croatian_ci | utf8 | | | Yes | |
| utf8_unicode_520_ci | utf8 | | | Yes | |
| utf8_vietnamese_ci | utf8 | | | Yes | |
| utf8_general_mysql500_ci | utf8 | | | Yes | |
| ucs2_general_ci | ucs2 | | Yes | Yes | |
| ucs2_bin | ucs2 | | | Yes | |
| ucs2_unicode_ci | ucs2 | | | Yes | |
| ucs2_icelandic_ci | ucs2 | | | Yes | |
| ucs2_latvian_ci | ucs2 | | | Yes | |
| ucs2_romanian_ci | ucs2 | | | Yes | |
| ucs2_slovenian_ci | ucs2 | | | Yes | |
| ucs2_polish_ci | ucs2 | | | Yes | |
| ucs2_estonian_ci | ucs2 | | | Yes | |
| ucs2_spanish_ci | ucs2 | | | Yes | |
| ucs2_swedish_ci | ucs2 | | | Yes | |
| ucs2_turkish_ci | ucs2 | | | Yes | |
| ucs2_czech_ci | ucs2 | | | Yes | |
| ucs2_danish_ci | ucs2 | | | Yes | |
| ucs2_lithuanian_ci | ucs2 | | | Yes | |
| ucs2_slovak_ci | ucs2 | | | Yes | |
| ucs2_spanish2_ci | ucs2 | | | Yes | |
| ucs2_roman_ci | ucs2 | | | Yes | |
| ucs2_persian_ci | ucs2 | | | Yes | |
| ucs2_esperanto_ci | ucs2 | | | Yes | |
| ucs2_hungarian_ci | ucs2 | | | Yes | |
| ucs2_sinhala_ci | ucs2 | | | Yes | |
| ucs2_german2_ci | ucs2 | | | Yes | |
| ucs2_croatian_ci | ucs2 | | | Yes | |
| ucs2_unicode_520_ci | ucs2 | | | Yes | |
| ucs2_vietnamese_ci | ucs2 | | | Yes | |
| ucs2_general_mysql500_ci | ucs2 | | | Yes | |
| cp866_general_ci | cp866 | | Yes | Yes | |
| cp866_bin | cp866 | | | Yes | |
| keybcs2_general_ci | keybcs2 | | Yes | Yes | |
| keybcs2_bin | keybcs2 | | | Yes | |
| macce_general_ci | macce | | Yes | Yes | |
| macce_bin | macce | | | Yes | |
| macroman_general_ci | macroman | | Yes | Yes | |
| macroman_bin | macroman | | | Yes | |
| cp852_general_ci | cp852 | | Yes | Yes | |
| cp852_bin | cp852 | | | Yes | |
| latin7_estonian_cs | latin7 | | | Yes | |
| latin7_general_ci | latin7 | | Yes | Yes | |
| latin7_general_cs | latin7 | | | Yes | |
| latin7_bin | latin7 | | | Yes | |
| utf8mb4_general_ci | utf8mb4 | | Yes | Yes | |
| utf8mb4_bin | utf8mb4 | | | Yes | |
| utf8mb4_unicode_ci | utf8mb4 | | | Yes | |
| utf8mb4_icelandic_ci | utf8mb4 | | | Yes | |
| utf8mb4_latvian_ci | utf8mb4 | | | Yes | |
| utf8mb4_romanian_ci | utf8mb4 | | | Yes | |
| utf8mb4_slovenian_ci | utf8mb4 | | | Yes | |
| utf8mb4_polish_ci | utf8mb4 | | | Yes | |
| utf8mb4_estonian_ci | utf8mb4 | | | Yes | |
| utf8mb4_spanish_ci | utf8mb4 | | | Yes | |
| utf8mb4_swedish_ci | utf8mb4 | | | Yes | |
| utf8mb4_turkish_ci | utf8mb4 | | | Yes | |
| utf8mb4_czech_ci | utf8mb4 | | | Yes | |
| utf8mb4_danish_ci | utf8mb4 | | | Yes | |
| utf8mb4_lithuanian_ci | utf8mb4 | | | Yes | |
| utf8mb4_slovak_ci | utf8mb4 | | | Yes | |
| utf8mb4_spanish2_ci | utf8mb4 | | | Yes | |
| utf8mb4_roman_ci | utf8mb4 | | | Yes | |
| utf8mb4_persian_ci | utf8mb4 | | | Yes | |
| utf8mb4_esperanto_ci | utf8mb4 | | | Yes | |
| utf8mb4_hungarian_ci | utf8mb4 | | | Yes | |
| utf8mb4_sinhala_ci | utf8mb4 | | | Yes | |
| utf8mb4_german2_ci | utf8mb4 | | | Yes | |
| utf8mb4_croatian_ci | utf8mb4 | | | Yes | |
| utf8mb4_unicode_520_ci | utf8mb4 | | | Yes | |
| utf8mb4_vietnamese_ci | utf8mb4 | | | Yes | |
| cp1251_bulgarian_ci | cp1251 | | | Yes | |
| cp1251_ukrainian_ci | cp1251 | | | Yes | |
| cp1251_bin | cp1251 | | | Yes | |
| cp1251_general_ci | cp1251 | | Yes | Yes | |
| cp1251_general_cs | cp1251 | | | Yes | |
| utf16_general_ci | utf16 | | Yes | Yes | |
| utf16_bin | utf16 | | | Yes | |
| utf16_unicode_ci | utf16 | | | Yes | |
| utf16_icelandic_ci | utf16 | | | Yes | |
| utf16_latvian_ci | utf16 | | | Yes | |
| utf16_romanian_ci | utf16 | | | Yes | |
| utf16_slovenian_ci | utf16 | | | Yes | |
| utf16_polish_ci | utf16 | | | Yes | |
| utf16_estonian_ci | utf16 | | | Yes | |
| utf16_spanish_ci | utf16 | | | Yes | |
| utf16_swedish_ci | utf16 | | | Yes | |
| utf16_turkish_ci | utf16 | | | Yes | |
| utf16_czech_ci | utf16 | | | Yes | |
| utf16_danish_ci | utf16 | | | Yes | |
| utf16_lithuanian_ci | utf16 | | | Yes | |
| utf16_slovak_ci | utf16 | | | Yes | |
| utf16_spanish2_ci | utf16 | | | Yes | |
| utf16_roman_ci | utf16 | | | Yes | |
| utf16_persian_ci | utf16 | | | Yes | |
| utf16_esperanto_ci | utf16 | | | Yes | |
| utf16_hungarian_ci | utf16 | | | Yes | |
| utf16_sinhala_ci | utf16 | | | Yes | |
| utf16_german2_ci | utf16 | | | Yes | |
| utf16_croatian_ci | utf16 | | | Yes | |
| utf16_unicode_520_ci | utf16 | | | Yes | |
| utf16_vietnamese_ci | utf16 | | | Yes | |
| utf16le_general_ci | utf16le | | Yes | Yes | |
| utf16le_bin | utf16le | | | Yes | |
| cp1256_general_ci | cp1256 | | Yes | Yes | |
| cp1256_bin | cp1256 | | | Yes | |
| cp1257_lithuanian_ci | cp1257 | | | Yes | |
| cp1257_bin | cp1257 | | | Yes | |
| cp1257_general_ci | cp1257 | | Yes | Yes | |
| utf32_general_ci | utf32 | | Yes | Yes | |
| utf32_bin | utf32 | | | Yes | |
| utf32_unicode_ci | utf32 | | | Yes | |
| utf32_icelandic_ci | utf32 | | | Yes | |
| utf32_latvian_ci | utf32 | | | Yes | |
| utf32_romanian_ci | utf32 | | | Yes | |
| utf32_slovenian_ci | utf32 | | | Yes | |
| utf32_polish_ci | utf32 | | | Yes | |
| utf32_estonian_ci | utf32 | | | Yes | |
| utf32_spanish_ci | utf32 | | | Yes | |
| utf32_swedish_ci | utf32 | | | Yes | |
| utf32_turkish_ci | utf32 | | | Yes | |
| utf32_czech_ci | utf32 | | | Yes | |
| utf32_danish_ci | utf32 | | | Yes | |
| utf32_lithuanian_ci | utf32 | | | Yes | |
| utf32_slovak_ci | utf32 | | | Yes | |
| utf32_spanish2_ci | utf32 | | | Yes | |
| utf32_roman_ci | utf32 | | | Yes | |
| utf32_persian_ci | utf32 | | | Yes | |
| utf32_esperanto_ci | utf32 | | | Yes | |
| utf32_hungarian_ci | utf32 | | | Yes | |
| utf32_sinhala_ci | utf32 | | | Yes | |
| utf32_german2_ci | utf32 | | | Yes | |
| utf32_croatian_ci | utf32 | | | Yes | |
| utf32_unicode_520_ci | utf32 | | | Yes | |
| utf32_vietnamese_ci | utf32 | | | Yes | |
| binary | binary | | Yes | Yes | |
| geostd8_general_ci | geostd8 | | Yes | Yes | |
| geostd8_bin | geostd8 | | | Yes | |
| cp932_japanese_ci | cp932 | | Yes | Yes | |
| cp932_bin | cp932 | | | Yes | |
| eucjpms_japanese_ci | eucjpms | | Yes | Yes | |
| eucjpms_bin | eucjpms | | | Yes | |
+--------------------------+--------------------+-----+------------+-------------+---------+
rows in set (0.02 sec)

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

  
mysql> pager md5sum;
PAGER set to 'md5sum'
mysql> select * from COLLATIONS;
6650d5d87f2abe18b6ead3588b133087 -
rows in set (0.02 sec)
mysql> pager md5sum
PAGER set to 'md5sum' # Original query
mysql> SELECT ...
32a1894d773c9b85172969c659175d2d -
row in set (0.40 sec) # Rewritten query - wrong
mysql> SELECT ...
fdb94521558684afedc8148ca724f578 -
row in set (0.16 sec)

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

# Rewritten query - correct
mysql> SELECT ...
32a1894d773c9b85172969c659175d2d -
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> show processlist;
+----+------+-----------+--------------------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+--------------------+---------+------+-------+------------------+
| | root | localhost | information_schema | Query | | init | show processlist |
| | root | localhost | test | Sleep | | | NULL |
+----+------+-----------+--------------------+---------+------+-------+------------------+
rows in set (0.00 sec) mysql> pager grep Sleep |wc -l
PAGER set to 'grep Sleep |wc -l' mysql> show processlist; rows in set (0.00 sec)

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 $}' | sort | uniq -c | sort -r'
mysql> show processlist; Sleep
Query
Command
rows in set (0.00 sec)

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(*) |
+----------+
| 1 |
+----------+
1 row in set (0.17 sec)

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 |
+---------+-------+
| Query | |
| Sleep | |
+---------+-------+
rows in set (0.01 sec)

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
 
上一篇:P154、面试题28:字符串的排列


下一篇:去掉MySQL字段中的空格