Skip to content

Commit

Permalink
Merge pull request #685 from jmrenouard/master
Browse files Browse the repository at this point in the history
Adding checks for Primary keys, non innodb tables and non UTF-8 columns
  • Loading branch information
jmrenouard authored Jun 22, 2023
2 parents e0e2168 + 2910dc1 commit 46f0aea
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ golang/prog-*
test_db/**
result*
result_*
sql/*.sql
sql/*.sql
sql/*.csv
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

[!["Buy Us A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jmrenouard)

[![Build Status - Master](https://travis-ci.org/major/MySQLTuner-perl.svg?branch=master)](https://travis-ci.org/major/MySQLTuner-perl)
[![Project Status](http://opensource.box.com/badges/active.svg)](http://opensource.box.com/badges)
[![Project Status](http://opensource.box.com/badges/maintenance.svg)](http://opensource.box.com/badges)
[![Test Status](https://github.com/anuraghazra/github-readme-stats/workflows/Test/badge.svg)](https://github.com/anuraghazra/github-readme-stats/)
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/major/MySQLTuner-perl.svg)](http://isitmaintained.com/project/major/MySQLTuner-perl "Average time to resolve an issue")
[![Percentage of open issues](http://isitmaintained.com/badge/open/major/MySQLTuner-perl.svg)](http://isitmaintained.com/project/major/MySQLTuner-perl "Percentage of issues still open")
[![GPL License](https://badges.frapsoft.com/os/gpl/gpl.png?v=103)](https://opensource.org/licenses/GPL-3.0/)
Expand All @@ -28,8 +27,11 @@ MySQLTuner needs you
* Please join us on issue track at [GitHub tracker](https://github.com/major/MySQLTuner-perl/issues).
* Contribution guide is available following [MySQLTuner contributing guide](https://github.com/major/MySQLTuner-perl/blob/master/CONTRIBUTING.md)
* Star **MySQLTuner project** at [MySQLTuner Git Hub Project](https://github.com/major/MySQLTuner-perl)
* Paid support for LightPath here: [[email protected]]([email protected])
* Paid support for Releem available here: [Releem App](https://releem.com/)

![Anurag's GitHub stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&theme=radical)

## Stargazers over time

[![Stargazers over time](https://starcharts.herokuapp.com/major/MySQLTuner-perl.svg)](https://starcharts.herokuapp.com/major/MySQLTuner-perl)
Expand Down
152 changes: 108 additions & 44 deletions mysqltuner.pl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env perl
# mysqltuner.pl - Version 2.1.11
# mysqltuner.pl - Version 2.2.0
# High Performance MySQL Tuning Script
# Copyright (C) 2006-2023 Major Hayden - [email protected]
# Copyright (C) 2015-2023 Jean-Marie Renouard - [email protected]
Expand Down Expand Up @@ -57,7 +57,7 @@ package main;
#use Env;

# Set up a few variables for use in the script
my $tunerversion = "2.1.11";
my $tunerversion = "2.2.0";
my ( @adjvars, @generalrec );

# Set defaults
Expand Down Expand Up @@ -1120,6 +1120,7 @@ sub select_csv_file {
print $l if $opt{debug};
}
close $fh;
infoprint "CSV file $tfile created";
}

sub human_size {
Expand Down Expand Up @@ -2750,6 +2751,17 @@ sub check_storage_engines {

my %mycalc;

sub dump_into_file {
my $file=shift;
my $content=shift;
if ( -d "$opt{dumpdir}" ) {
$file="$opt{dumpdir}/$file";
open (FILE, ">$file") or die "Can't open $file: $!";
print FILE $content;
close FILE;
infoprint "Data saved to $file";
}
}
sub calculations {
if ( $mystat{'Questions'} < 1 ) {
badprint "Your server has not answered any queries: cannot continue...";
Expand Down Expand Up @@ -3790,13 +3802,7 @@ sub mysql_myisam {
$sql_mig="${sql_mig}-- InnoDB migration for $myisam_table\nALTER TABLE $myisam_table ENGINE=InnoDB;\n\n";
infoprint "* InnoDB migration request for $myisam_table Table: ALTER TABLE $myisam_table ENGINE=InnoDB;";
}
if ( -d "$opt{dumpdir}" ) {
my $file_mig="$opt{dumpdir}/migrate_myisam_to_innodb.sql";
open (FILE, ">$file_mig") or die "Can't open $file_mig: $!";
print FILE $sql_mig;
close FILE;
infoprint "Migration script saved to $file_mig";
}
dump_into_file("migrate_myisam_to_innodb.sql", $sql_mig );
}
infoprint("General MyIsam metrics:");
infoprint " +-- Total MyISAM Tables : $nb_myisam_tables";
Expand Down Expand Up @@ -5717,6 +5723,97 @@ sub get_wsrep_option {
return $memValue;
}
# REcommendations for Tables
sub mysql_table_structures {
subheaderprint "Table structures analysis";
my @primaryKeysNbTables = select_array(
"Select CONCAT(c.table_schema, ',' , c.table_name)
from information_schema.columns c
join information_schema.tables t using (TABLE_SCHEMA, TABLE_NAME)
where c.table_schema not in ('sys', 'mysql', 'information_schema', 'performance_schema')
and t.table_type = 'BASE TABLE'
group by c.table_schema,c.table_name
having sum(if(c.column_key in ('PRI', 'UNI'), 1, 0)) = 0"
);
my $tmpContent='Schema,Table';
if ( scalar(@primaryKeysNbTables) > 0 ) {
badprint "Following table(s) don't have primary key:";
foreach my $badtable (@primaryKeysNbTables) {
badprint "\t$badtable";
push @{ $result{'Tables without PK'} }, $badtable;
$tmpContent.="\n$badtable";
}
push @generalrec,
"Ensure that all table(s) get an explicit primary keys for performance, maintenance and also for replication";
}
else {
goodprint "All tables get a primary key";
}
dump_into_file( "tables_without_primary_keys.csv", $tmpContent );
my @nonInnoDBTables = select_array(
"select CONCAT(table_schema, ',', table_name, ',', ENGINE)
FROM information_schema.tables t
WHERE ENGINE <> 'InnoDB'
and t.table_type = 'BASE TABLE'
and table_schema not in
('sys', 'mysql', 'performance_schema', 'information_schema')"
);
$tmpContent='Schema,Table,Engine';
if ( scalar(@nonInnoDBTables) > 0 ) {
badprint "Following table(s) are not InnoDB table:";
push @generalrec,
"Ensure that all table(s) are InnoDB tables for performance and also for replication";
foreach my $badtable (@nonInnoDBTables) {
badprint "\t$badtable";
$tmpContent.="\n$badtable";
}
}
else {
goodprint "All tables are InnoDB tables";
}
dump_into_file( "tables_non_innodb.csv", $tmpContent );
my @nonutf8columns = select_array(
"SELECT CONCAT(table_schema, ',', table_name, ',', column_name, ',', CHARacter_set_name, ',', COLLATION_name, ',', data_type, ',', CHARACTER_MAXIMUM_LENGTH)
from information_schema.columns
WHERE table_schema not in ('sys', 'mysql', 'performance_schema', 'information_schema')
and (CHARacter_set_name NOT LIKE 'utf8%'
or COLLATION_name NOT LIKE 'utf8%');"
);
$tmpContent='Schema,Table,Column, Charset, Collation, Data Type, Max Length';
if ( scalar(@nonutf8columns) > 0 ) {
badprint "Following character columns(s) are not utf8 compliant:";
push @generalrec,
"Ensure that all text colums(s) are UTF-8 compliant for encoding support and performance";
foreach my $badtable (@nonutf8columns) {
badprint "\t$badtable";
$tmpContent.="\n$badtable";
}
}
else {
goodprint "All columns are UTF-8 compliant";
}
dump_into_file( "columns_non_utf8.csv", $tmpContent );
my @utf8columns = select_array(
"SELECT CONCAT(table_schema, ',', table_name, ',', column_name, ',', CHARacter_set_name, ',', COLLATION_name, ',', data_type, ',', CHARACTER_MAXIMUM_LENGTH)
from information_schema.columns
WHERE table_schema not in ('sys', 'mysql', 'performance_schema', 'information_schema')
and (CHARacter_set_name LIKE 'utf8%'
or COLLATION_name LIKE 'utf8%');"
);
$tmpContent='Schema,Table,Column, Charset, Collation, Data Type, Max Length';
foreach my $badtable (@utf8columns) {
badprint "\t$badtable";
$tmpContent.="\n$badtable";
}
dump_into_file( "columns_utf8.csv", $tmpContent );
}
# Recommendations for Galera
sub mariadb_galera {
subheaderprint "Galera Metrics";
Expand Down Expand Up @@ -5755,16 +5852,6 @@ sub mariadb_galera {
infoprint "GCache is using "
. hr_bytes_rnd( get_wsrep_option('gcache.mem_size') );
#my @primaryKeysNbTables=();
my @primaryKeysNbTables = select_array(
"Select CONCAT(c.table_schema,CONCAT('.', c.table_name))
from information_schema.columns c
join information_schema.tables t using (TABLE_SCHEMA, TABLE_NAME)
where c.table_schema not in ('mysql', 'information_schema', 'performance_schema')
and t.table_type != 'VIEW'
group by c.table_schema,c.table_name
having sum(if(c.column_key in ('PRI', 'UNI'), 1, 0)) = 0"
);
infoprint "CPU cores detected : " . (cpu_cores);
infoprint "wsrep_slave_threads: " . get_wsrep_option('wsrep_slave_threads');
Expand Down Expand Up @@ -5831,30 +5918,6 @@ sub mariadb_galera {
"Flow control fraction seems to be OK (wsrep_flow_control_paused <= 0.02)";
}
if ( scalar(@primaryKeysNbTables) > 0 ) {
badprint "Following table(s) don't have primary key:";
foreach my $badtable (@primaryKeysNbTables) {
badprint "\t$badtable";
push @{ $result{'Tables without PK'} }, $badtable;
}
}
else {
goodprint "All tables get a primary key";
}
my @nonInnoDBTables = select_array(
"select CONCAT(table_schema,CONCAT('.', table_name)) from information_schema.tables where ENGINE <> 'InnoDB' and table_schema not in ('mysql', 'performance_schema', 'information_schema')"
);
if ( scalar(@nonInnoDBTables) > 0 ) {
badprint "Following table(s) are not InnoDB table:";
push @generalrec,
"Ensure that all table(s) are InnoDB tables for Galera replication";
foreach my $badtable (@nonInnoDBTables) {
badprint "\t$badtable";
}
}
else {
goodprint "All tables are InnoDB tables";
}
if ( $myvar{'binlog_format'} ne 'ROW' ) {
badprint "Binlog format should be in ROW mode.";
push @adjvars, "binlog_format = ROW";
Expand Down Expand Up @@ -7121,6 +7184,7 @@ sub which {
check_metadata_perf; # Show parameter impacting performance during analysis
mysql_databases; # Show information about databases
mysql_tables; # Show information about table column
mysql_table_structures; # Show information about table structures
mysql_indexes; # Show information about indexes
mysql_views; # Show information about views
Expand Down Expand Up @@ -7161,7 +7225,7 @@ sub which {
=head1 NAME
MySQLTuner 2.1.11 - MySQL High Performance Tuning Script
MySQLTuner 2.2.0 - MySQL High Performance Tuning Script
=head1 IMPORTANT USAGE GUIDELINES
Expand Down

0 comments on commit 46f0aea

Please sign in to comment.