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
 

pager 命令的更多相关文章

  1. mysql中pager命令妙用

    pager命令的妙用在mysql,可以大大提高工作效率. 一 当处理大量数据时,不想显示查询的结果,而只需知道查询话费的时间. mysql> select *   from t3; +----- ...

  2. 介绍一些有趣的MySQL pager命令

    一.分页结果集 在Linux系统中中,我们经常也会使用一些分页查看命令,例如less.more等.同样,MySQL客户端也提供了类似的命令,用来帮助我们对查询结果集进行分页.比如,SHOW ENGIN ...

  3. mysql pager用法&命令行命令

    下面讲的命令,有部分只能在linux上才有.像pager命令windows上就没有了. 分屏:在Linux上,而且不是xwindow时,使用mysql命令行时,输出太多的东西,看不到就很悲剧了.在sh ...

  4. mysql的一些特殊命令

    mysql命令行工具的编辑技巧 从mysql performace blog 中学到的: 1. pager 例子 mysql> pager more PAGER set to 'more' my ...

  5. Mysql 客户端查询结果如何保存到本地而不是服务端?

    应用场景:知道某台DB服务器的IP和账户,登录上去查询了10W条记录,需要把这些记录拉到本地做分析 方法1,远程连接到DB服务器执行OUTFILE命令,文件存储在DB机器上,只有mysql账户的情况下 ...

  6. MySQL 实用技巧

    概述: MySQL有许多实用的技巧,利用这些技巧能提高工作的效率,减少一些不必要的麻烦.以下是几个我在MySQL日常维护从常用的技巧. 一.prompt 命令 功能:设置mysql客户端提示符 说明: ...

  7. 【Linux】使用update-alternatives命令进行版本的切换

    引言 在Redhat中也有此功能,用于切换不同版本. 在Debian系统中,我们可能会同时安装有很多功能类似的程序和可选配置,可能会出现同一软件的多个版本并存的场景.比如像是一些编程语言工具,一些系统 ...

  8. 十个节省时间的MySQL命令

    十个节省时间的MySQL命令 2011-02-23 16:07 黄永兵 译 IT168 字号:T | T 编者在工作中积累起来了一些MySQL命令行客户端技巧,这些技巧或多或少会帮助您节省大量的时间. ...

  9. [Android ] linux命令英文缩写的含义(方便记忆)

    du -sh */ reference to : http://blog.chinaunix.net/uid-27164517-id-3299073.html linux常用命令的英文单词缩写 命令缩 ...

随机推荐

  1. codeforces 682C Alyona and the Tree DFS

    这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...

  2. codeforces 679A Bear and Prime 100 交互

    第一次交互题,记录一下吧 #include <cstdio> #include <iostream> #include <ctime> #include <v ...

  3. xcode 怎么样在发布release版本的时候 不输出log

    我们平时在开发应用的时候,经常会用到 NSLog 来调试我们的程序,而随着项目越来越大,这些用于调试的日志输出就会变得很难管理. 发布正式版的时候一定要屏蔽掉所有后台输出,因为这些输出还是比较消耗系统 ...

  4. CreateThread函数&amp;&amp;CString::GetBuffer函数

    对这个两个常见的windows下的函数学习了一下: //最简单的创建多线程实例 #include <stdio.h> #include <windows.h> //子线程函数 ...

  5. Tkinter教程之Canvas篇(3)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811922 ''Tkinter教程之Canvas篇(3)''''''16.移动item'''# ...

  6. HDU-4734 F(x) 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 注意到F(x)的值比较小,所以可以先预处理所有F(x)的组合个数.f[i][j]表示 i 位数时 ...

  7. socket.io的抽象实现:engine.io

    engine.io是一个socket.io的抽象实现,作为socket.io的服务器和浏览器之间交换的数据的传输层.它不会取代Socket.IO,它只是抽象出固有的复杂性,支持多种浏览器,设备和网络的 ...

  8. linux - 怎么自动填写有交互的shell脚本 - SegmentFault

    linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...

  9. JVM内存结构

    前言 在Java语言开发过程中,out of memory错误是很常见的一种错误.对于JVM的内存结构有更深入的了解,更更好的帮我们排查此类问题,有效的避免此类问题发生.在JAVA 8中内存结构有进行 ...

  10. [iOS基础控件 - 6.12.4] NavigationController vs TabBarController

    A.属性 1. Item NavigationController: navigationItem (不需要创建) title rightBarButtonItems/ rightBarButtonI ...