Optimize the shell script bitsCN.com of the Mysql data table because the data volume in the company database is large, optimize the data table in the company's mysql database on a regular basis (the optimize description is as follows ), there are more than 300 data tables in the database. manual operations are obviously not realistic. the efficiency of script execution is still very good. The script is as follows:
The OPTIMIZE description in the mysql manual:
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name]…
If you have deleted a majority of tables, or if you have made many changes to a table containing variable-length rows (tables containing VARCHAR, BLOB, or TEXT columns), you should use
Optimize table. The deleted records are kept in the link list. Subsequent INSERT operations will reuse the old record location. You can use optimize table again
Use unused space and organize data file fragments.
Usage: sh optimize. sh word
[Root @ shellec shell] #
#! /Bin/sh
Time_log =/opt/optimize_time
Sum = $ #
If ["$ sum"-eq 0]
Then
Echo "Error: no parameter chosed"
Exit 1
Fi
For I in $ *; do
Echo "optimize database $ I starting ..."
Tables = $ (/usr/bin/mysql $ I-udevuser-pdevuser-e "show tables" | grep-v "Tables">/opt/$ I)
Tablelist = $ (cat/opt/$ I)
Echo "optimize database $ I starting ......"> $ time_log
Echo "$ I start at $ (date + [% Y/% m/% d/% H: % M: % S])" >>$ time_log
For list in $ tablelist
Do
Echo $ list
/Usr/bin/mysql $ I-utaobao-padmin-e "optimize table $ list"
Done
Echo "$ I end at $ (date + [% Y/% m/% d/% H: % M: % S])" >>$ time_log
Echo> $ time_log
Done
BitsCN.com