Skip to content

Commit 8d17844

Browse files
committed
2 parents 5fce4fc + 29fb536 commit 8d17844

File tree

1 file changed

+87
-12
lines changed

1 file changed

+87
-12
lines changed

mysqltuner.pl

+87-12
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,35 @@ sub select_one {
988988
return $result;
989989
}
990990

991+
# MySQL Request one
992+
sub select_one_g {
993+
my $pattern = shift;
994+
995+
my $req = shift;
996+
debugprint "PERFORM: $req ";
997+
my @result = `$mysqlcmd $mysqllogin -re "\\w$req\\G" 2>>/dev/null`;
998+
if ( $? != 0 ) {
999+
badprint "failed to execute: $req";
1000+
badprint "FAIL Execute SQL / return code: $?";
1001+
debugprint "CMD : $mysqlcmd";
1002+
debugprint "OPTIONS: $mysqllogin";
1003+
debugprint `$mysqlcmd $mysqllogin -Bse "$req" 2>&1`;
1004+
1005+
#exit $?;
1006+
}
1007+
debugprint "select_array: return code : $?";
1008+
chomp(@result);
1009+
return (grep { /$pattern/ } @result)[0];
1010+
}
1011+
sub select_str_g {
1012+
my $pattern = shift;
1013+
1014+
my $req = shift;
1015+
my $str=select_one_g $pattern, $req;
1016+
my @val=split /:/, $str;
1017+
shift @val;
1018+
return trim(@val);
1019+
}
9911020
sub get_tuning_info {
9921021
my @infoconn = select_array "\\s";
9931022
my ( $tkey, $tval );
@@ -5577,13 +5606,13 @@ sub mysql_databases {
55775606
return;
55785607
}
55795608

5580-
my @dblist = select_array("SHOW DATABASES;");
5609+
my @dblist = select_array("SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );");
55815610
infoprint "There is " . scalar(@dblist) . " Database(s).";
55825611
my @totaldbinfo = split /\s/,
55835612
select_one(
5584-
"SELECT SUM(TABLE_ROWS), SUM(DATA_LENGTH), SUM(INDEX_LENGTH) , SUM(DATA_LENGTH+INDEX_LENGTH), COUNT(TABLE_NAME),COUNT(DISTINCT(TABLE_COLLATION)),COUNT(DISTINCT(ENGINE)) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql' );"
5613+
"SELECT SUM(TABLE_ROWS), SUM(DATA_LENGTH), SUM(INDEX_LENGTH) , SUM(DATA_LENGTH+INDEX_LENGTH), COUNT(TABLE_NAME),COUNT(DISTINCT(TABLE_COLLATION)),COUNT(DISTINCT(ENGINE)) FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );"
55855614
);
5586-
infoprint "All Databases:";
5615+
infoprint "All User Databases:";
55875616
infoprint " +-- TABLE : "
55885617
. ( $totaldbinfo[4] eq 'NULL' ? 0 : $totaldbinfo[4] ) . "";
55895618
infoprint " +-- ROWS : "
@@ -5621,15 +5650,6 @@ sub mysql_databases {
56215650
print "\n" unless ( $opt{'silent'} or $opt{'json'} );
56225651

56235652
foreach (@dblist) {
5624-
chomp($_);
5625-
if ( $_ eq "information_schema"
5626-
or $_ eq "performance_schema"
5627-
or $_ eq "mysql"
5628-
or $_ eq "" )
5629-
{
5630-
next;
5631-
}
5632-
56335653
my @dbinfo = split /\s/,
56345654
select_one(
56355655
"SELECT TABLE_SCHEMA, SUM(TABLE_ROWS), SUM(DATA_LENGTH), SUM(INDEX_LENGTH) , SUM(DATA_LENGTH+INDEX_LENGTH), COUNT(DISTINCT ENGINE),COUNT(TABLE_NAME),COUNT(DISTINCT(TABLE_COLLATION)),COUNT(DISTINCT(ENGINE)) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$_' GROUP BY TABLE_SCHEMA ORDER BY TABLE_SCHEMA"
@@ -5749,6 +5769,58 @@ sub mysql_databases {
57495769

57505770
}
57515771

5772+
5773+
# Recommendations for database columns
5774+
sub mysql_tables {
5775+
return if ( $opt{dbstat} == 0 );
5776+
5777+
subheaderprint "Table Column Metrics";
5778+
unless ( mysql_version_ge( 5, 5 ) ) {
5779+
infoprint
5780+
"Skip Database metrics from information schema missing in this version";
5781+
return;
5782+
}
5783+
my @dblist = select_array("SELECT DISTINCT TABLE_SCHEMA FROM information_schema.TABLES WHERE TABLE_SCHEMA NOT IN ( 'mysql', 'performance_schema', 'information_schema', 'sys' );");
5784+
foreach (@dblist) {
5785+
my $dbname=$_;
5786+
next unless defined $_;
5787+
infoprint "Database: " . $_ . "";
5788+
my @dbtable = select_array(
5789+
"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='$dbname' AND TABLE_TYPE='BASE TABLE' ORDER BY TABLE_NAME"
5790+
);
5791+
foreach(@dbtable) {
5792+
my $tbname=$_;
5793+
infoprint " +-- TABLE: $tbname";
5794+
my @tbcol=select_array(
5795+
"SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname'" );
5796+
foreach(@tbcol) {
5797+
my $ctype=select_one(
5798+
"SELECT COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname' AND COLUMN_NAME='$_' " );
5799+
my $isnull=select_one(
5800+
"SELECT IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='$dbname' AND TABLE_NAME='$tbname' AND COLUMN_NAME='$_' " );
5801+
infoprint " +-- Column $tbname.$_:" ;
5802+
my $current_type=uc($ctype). ($isnull eq 'NO'?" NOT NULL":"");
5803+
my $optimal_type=select_str_g("Optimal_fieldtype", "SELECT $_ FROM $dbname.$tbname PROCEDURE ANALYSE(100000)");
5804+
5805+
if ( $current_type ne $optimal_type )
5806+
{
5807+
infoprint " Current Fieldtype: $current_type";
5808+
infoprint " Optimal Fieldtype: $optimal_type";
5809+
badprint
5810+
"Consider changing type for column $_ in table $dbname.$tbname";
5811+
push( @generalrec,
5812+
"ALTER TABLE $dbname.$tbname MODIFY $_ $optimal_type;" );
5813+
5814+
}
5815+
else {
5816+
goodprint "$dbname.$tbname ($_) type: $current_type";
5817+
}
5818+
}
5819+
}
5820+
5821+
}
5822+
}
5823+
57525824
# Recommendations for Indexes metrics
57535825
sub mysql_indexes {
57545826
return if ( $opt{idxstat} == 0 );
@@ -5998,6 +6070,7 @@ sub which {
59986070
# BEGIN 'MAIN'
59996071
# ---------------------------------------------------------------------------
60006072
headerprint; # Header Print
6073+
60016074
validate_tuner_version; # Check last version
60026075
mysql_setup; # Gotta login first
60036076
os_setup; # Set up some OS variables
@@ -6010,6 +6083,8 @@ sub which {
60106083
log_file_recommandations; # check log file content
60116084
check_storage_engines; # Show enabled storage engines
60126085
mysql_databases; # Show informations about databases
6086+
mysql_tables; # Show informations about table column
6087+
60136088
mysql_indexes; # Show informations about indexes
60146089
security_recommendations; # Display some security recommendations
60156090
cve_recommendations; # Display related CVE

0 commit comments

Comments
 (0)