Mysql linux shell commands to get tables list and basic statistics

Mysql is so common that its good idea to have some command line scripts or tricks. Sometimes you need to execute some SQL queries on set of tables or all of them or get stats or fix tables etc.

Its good to be able quickly get list of tables and basic stats.

You can make bash script of it or just type in the command line:

# Put your database name here
export SET dbname='drupal';

# List all tables in the database
echo "show tables;" | mysql -u root $dbname |grep -v '^Tables_in_' > tables.txt

# Show tables status (tables' engine, rows count, table size, index size, collation, comment)
echo "show table status;" | mysql -u root $dbname > table_status.txt

# Get each tables indexes details
for x in `cat tables.txt `; do echo "show index from $x;" | mysql -u root $dbname >> indexes.txt; done

# Get databse schema without data
mysqldump -d -u root $dbname > schema.sql

This way you will get tables schema, indexes definiton and stats (how many rows are there, what is the key selectivity etc).

Comments

Post new comment

Image CAPTCHA