1つのShellスクリプトは、Mysqlの各テーブルの行数を正確に統計します.

4537 ワード

前言
開発者や運営者にとって、Mysqlデータベースの各テーブルの数は、不要なデータを整理したり、どのテーブルが占有されているかを理解したりするのに役立つに違いありません.また、テーブルの行数を複数回集計することで、Mysqlテーブルの増分状況も発見され、テーブルの将来の量を予測することができます.くだらないことは言わないで、みんなに直接簡単なShellのスクリプトを書いてもらいます.
データベース名のループ取得
Shellコードに直接アクセスし、show databasesはすべてのライブラリ名を取得します.その結果、私たちが望んでいないのは、Databaseです.このgrep-vが落ちて、すべてのデータベースを簡単に取得することができます.
[root@shijiangeit ~]# mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| shijiange          |
| test               |
| wordpress          |
+--------------------+

[root@shijiangeit ~]# mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database
information_schema
mysql
performance_schema
shijiange
test
wordpress

すべてのテーブルのループ取得
ライブラリ情報があれば、すべてのテーブルを取得するのは簡単で、Shellコードに直接アクセスします.show tablesはすべてのテーブル名を取得し、そのうちTables_inいいえ、grep-vが落ちます.
[root@shijiangeit ~]# for onedb in $(mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database);do
>   echo $onedb
>   mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "show tables" 2>/dev/null
> done
information_schema
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| ENGINES                               |
| EVENTS                                |
| FILES                                 |
| GLOBAL_STATUS                         |
| GLOBAL_VARIABLES                      |
| KEY_COLUMN_USAGE                      |

循環統計各テーブルの行数
ライブラリ名にテーブル名を付け、1つのselect count(1)統計テーブルの行数、循環統計を取り出し、Shellコードに直接接続します.
[root@shijiangeit ~]# for onedb in $(mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database);do
>   for onetab in $(mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do
>     onetablength=$(mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count')
>     echo -e "$onedb.$onetab\t$onetablength"
>   done
> done
information_schema.CHARACTER_SETS   40
information_schema.COLLATIONS   219
information_schema.COLLATION_CHARACTER_SET_APPLICABILITY    219
information_schema.COLUMNS  1789
information_schema.COLUMN_PRIVILEGES    0
shijiange.logincount    4
shijiange.member    0
shijiange.user  2097153
test.detect_servers 0
wordpress.wp_commentmeta    0
wordpress.wp_comments   0
wordpress.wp_links  0
wordpress.wp_options    156

変数化、スクリプトの直接使用
どのMysqlを統計する必要があるのか、前の3つの変数を変更すると、すぐにすべてのテーブルのサイズを統計することができます.
mysqlhost=127.0.0.1
mysqluser=xxx
mysqlpassword=xxx

for onedb in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword -e "show databases;" 2>/dev/null |grep -v Database);do
  for onetab in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do
    onetablength=$(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count')
    echo -e "$onedb.$onetab\t$onetablength"
  done
done

どの時計の行数が一番多いですか?
前のスクリプトに|sort-nrk 2|lessを付けて完成して、超実用的な小さいスクリプトはこのように完成しました
[root@shijiangeit ~]# for onedb in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword -e "show databases;" 2>/dev/null |grep -v Database);do
>   for onetab in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do
>     onetablength=$(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count')
>     echo -e "$onedb.$onetab\t$onetablength"
>   done
> done | sort -nrk 2
shijiange.user  2097153
information_schema.INNODB_BUFFER_PAGE   8191
performance_schema.events_waits_summary_by_thread_by_event_name 5320
information_schema.INNODB_BUFFER_PAGE_LRU   3453

Mysql基礎学習の推奨
Mysql 8基礎実戦入門学習サイトhttps://edu.51cto.com/sd/6b871