Refactor dbstat part of mysqltuner

This commit is contained in:
Jean-Marie Renouard 2022-02-04 22:29:04 +01:00
parent 3ce4c603b4
commit 55cdf92f8b

View file

@ -6139,7 +6139,29 @@ sub mysql_databases {
); );
infoprint "All User Databases:"; infoprint "All User Databases:";
infoprint " +-- TABLE : " infoprint " +-- TABLE : "
. ( $totaldbinfo[4] eq 'NULL' ? 0 : $totaldbinfo[4] ) . ""; . select_one( "SELECT count(*) from information_schema.TABLES WHERE TABLE_TYPE ='BASE TABLE' AND TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' )" )
. "";
infoprint " +-- VIEW : "
. select_one( "SELECT count(*) from information_schema.TABLES WHERE TABLE_TYPE ='VIEW' AND TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' )" )
. "";
infoprint " +-- INDEX : "
. select_one( "SELECT count(distinct(concat(TABLE_NAME, TABLE_SCHEMA, INDEX_NAME))) from information_schema.STATISTICS WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' )" )
. "";
infoprint " +-- CHARS : "
. ( $totaldbinfo[5] eq 'NULL' ? 0 : $totaldbinfo[5] ) . " ("
. (
join ", ",
select_array(
"select distinct(CHARACTER_SET_NAME) from information_schema.columns WHERE CHARACTER_SET_NAME IS NOT NULL AND TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );")
) . ")";
infoprint " +-- COLLA : "
. ( $totaldbinfo[5] eq 'NULL' ? 0 : $totaldbinfo[5] ) . " ("
. (
join ", ",
select_array(
"SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES WHERE TABLE_COLLATION IS NOT NULL AND TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );")
) . ")";
infoprint " +-- ROWS : " infoprint " +-- ROWS : "
. ( $totaldbinfo[0] eq 'NULL' ? 0 : $totaldbinfo[0] ) . ""; . ( $totaldbinfo[0] eq 'NULL' ? 0 : $totaldbinfo[0] ) . "";
infoprint " +-- DATA : " infoprint " +-- DATA : "
@ -6149,18 +6171,11 @@ sub mysql_databases {
. hr_bytes( $totaldbinfo[2] ) . "(" . hr_bytes( $totaldbinfo[2] ) . "("
. percentage( $totaldbinfo[2], $totaldbinfo[3] ) . "%)"; . percentage( $totaldbinfo[2], $totaldbinfo[3] ) . "%)";
infoprint " +-- SIZE : " . hr_bytes( $totaldbinfo[3] ) . ""; infoprint " +-- SIZE : " . hr_bytes( $totaldbinfo[3] ) . "";
infoprint " +-- COLLA : " infoprint " +-- ENGINE: "
. ( $totaldbinfo[5] eq 'NULL' ? 0 : $totaldbinfo[5] ) . " ("
. (
join ", ",
select_array(
"SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES;")
) . ")";
infoprint " +-- ENGIN : "
. ( $totaldbinfo[6] eq 'NULL' ? 0 : $totaldbinfo[6] ) . " (" . ( $totaldbinfo[6] eq 'NULL' ? 0 : $totaldbinfo[6] ) . " ("
. ( . (
join ", ", join ", ",
select_array("SELECT DISTINCT(ENGINE) FROM information_schema.TABLES;") select_array("SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE ENGINE IS NOT NULL AND TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );")
) . ")"; ) . ")";
$result{'Databases'}{'All databases'}{'Rows'} = $result{'Databases'}{'All databases'}{'Rows'} =
@ -6181,15 +6196,27 @@ sub mysql_databases {
); );
next unless defined $dbinfo[0]; next unless defined $dbinfo[0];
infoprint "Database: " . $dbinfo[0] . ""; infoprint "Database: " . $dbinfo[0] . "";
infoprint " +-- TABLE: " infoprint " +-- TABLE : "
. ( !defined( $dbinfo[6] ) or $dbinfo[6] eq 'NULL' ? 0 : $dbinfo[6] ) . select_one( "SELECT count(*) from information_schema.TABLES WHERE TABLE_TYPE ='BASE TABLE' AND TABLE_SCHEMA='$_'" )
. ""; . "";
infoprint " +-- COLL : " infoprint " +-- VIEW : "
. select_one( "SELECT count(*) from information_schema.TABLES WHERE TABLE_TYPE ='VIEW' AND TABLE_SCHEMA='$_'" )
. "";
infoprint " +-- INDEX : "
. select_one( "SELECT count(distinct(concat(TABLE_NAME, TABLE_SCHEMA, INDEX_NAME))) from information_schema.STATISTICS WHERE TABLE_SCHEMA='$_'" )
. "";
infoprint " +-- CHARS : "
. ( $totaldbinfo[5] eq 'NULL' ? 0 : $totaldbinfo[5] ) . " ("
. (
join ", ",
select_array(
"select distinct(CHARACTER_SET_NAME) from information_schema.columns WHERE CHARACTER_SET_NAME IS NOT NULL AND TABLE_SCHEMA='$_';")
) . ")";infoprint " +-- COLLA : "
. ( $dbinfo[7] eq 'NULL' ? 0 : $dbinfo[7] ) . " (" . ( $dbinfo[7] eq 'NULL' ? 0 : $dbinfo[7] ) . " ("
. ( . (
join ", ", join ", ",
select_array( select_array(
"SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_';" "SELECT DISTINCT(TABLE_COLLATION) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_' AND TABLE_COLLATION IS NOT NULL;"
) )
) . ")"; ) . ")";
infoprint " +-- ROWS : " infoprint " +-- ROWS : "
@ -6198,22 +6225,22 @@ sub mysql_databases {
infoprint " +-- DATA : " infoprint " +-- DATA : "
. hr_bytes( $dbinfo[2] ) . "(" . hr_bytes( $dbinfo[2] ) . "("
. percentage( $dbinfo[2], $dbinfo[4] ) . "%)"; . percentage( $dbinfo[2], $dbinfo[4] ) . "%)";
infoprint " +-- INDEX: " infoprint " +-- INDEX : "
. hr_bytes( $dbinfo[3] ) . "(" . hr_bytes( $dbinfo[3] ) . "("
. percentage( $dbinfo[3], $dbinfo[4] ) . "%)"; . percentage( $dbinfo[3], $dbinfo[4] ) . "%)";
infoprint " +-- TOTAL: " . hr_bytes( $dbinfo[4] ) . ""; infoprint " +-- TOTAL : " . hr_bytes( $dbinfo[4] ) . "";
infoprint " +-- ENGIN : " infoprint " +-- ENGINE: "
. ( $dbinfo[8] eq 'NULL' ? 0 : $dbinfo[8] ) . " (" . ( $dbinfo[8] eq 'NULL' ? 0 : $dbinfo[8] ) . " ("
. ( . (
join ", ", join ", ",
select_array( select_array(
"SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_'" "SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_' AND ENGINE IS NOT NULL"
) )
) . ")"; ) . ")";
foreach my $eng ( foreach my $eng (
select_array( select_array(
"SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_'" "SELECT DISTINCT(ENGINE) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_' AND ENGINE IS NOT NULL"
) )
) )
{ {