MySQL OPTIMIZE all tables?

前端 未结 14 1940
迷失自我
迷失自我 2020-12-04 04:57

MySQL has an OPTIMIZE TABLE command which can be used to reclaim unused space in a MySQL install. Is there a way (built-in command or common stored procedure) to run this o

相关标签:
14条回答
  • 2020-12-04 05:22

    You can use mysqlcheck to do this at the command line.

    One database:

    mysqlcheck -o <db_schema_name>
    

    All databases:

    mysqlcheck -o --all-databases
    
    0 讨论(0)
  • 2020-12-04 05:22

    I made this 'simple' script:

    set @tables_like = null;
    set @optimize = null;
    set @show_tables = concat("show tables where", ifnull(concat(" `Tables_in_", database(), "` like '", @tables_like, "' and"), ''), " (@optimize:=concat_ws(',',@optimize,`Tables_in_", database() ,"`))");
    
    Prepare `bd` from @show_tables;
    EXECUTE `bd`;
    DEALLOCATE PREPARE `bd`;
    
    set @optimize := concat('optimize table ', @optimize);
    PREPARE `sql` FROM @optimize;
    EXECUTE `sql`;
    DEALLOCATE PREPARE `sql`;
    
    set @show_tables = null, @optimize = null, @tables_like = null;
    

    To run it, simply paste it in any SQL IDE connected to your database.

    Notice: this code WON'T work on phpmyadmin.

    How it works

    It runs a show tables statement and stores it in a prepared statement. Then it runs a optimize table in the selected set.

    You can control which tables to optimize by setting a different value in the var @tables_like (e.g.: set @tables_like = '%test%';).

    0 讨论(0)
  • 2020-12-04 05:24

    You can optimize/check and repair all the tables of database, using mysql client.

    First, you should get all the tables list, separated with ',':

    mysql -u[USERNAME] -p[PASSWORD] -Bse 'show tables' [DB_NAME]|xargs|perl -pe 's/ /,/g'
    

    Now, when you have all the tables list for optimization:

    mysql -u[USERNAME] -p[PASSWORD] -Bse 'optimize tables [tables list]' [DB_NAME]
    
    0 讨论(0)
  • 2020-12-04 05:30

    my 2cents: start with table with highest fragmentation

    for table in `mysql -sss -e "select concat(table_schema,".",table_name) from information_schema.tables where table_schema not in ('mysql','information_schema','performance_schema') order by data_free desc;"
    do
    mysql -e "OPTIMIZE TABLE $table;"
    done
    
    0 讨论(0)
  • 2020-12-04 05:31

    From phpMyAdmin and other sources you can use:

    SET SESSION group_concat_max_len = 99999999;
    SELECT GROUP_CONCAT(concat('OPTIMIZE TABLE `', table_name, '`;') SEPARATOR '') AS O
    FROM INFORMATION_SCHEMA.TABLES WHERE 
    TABLE_TYPE = 'BASE TABLE'
    AND table_name!='dual'
    AND TABLE_SCHEMA = '<your databasename>'
    

    Then you can copy & paste the result to a new query or execute it from your own source. If you don't see the whole statement:

    0 讨论(0)
  • 2020-12-04 05:32

    The MySQL Administrator (part of the MySQL GUI Tools) can do that for you on a database level.

    Just select your schema and press the Maintenance button in the bottom right corner.

    Since the GUI Tools have reached End-of-life status they are hard to find on the mysql page. Found them via Google: http://dev.mysql.com/downloads/gui-tools/5.0.html

    I don't know if the new MySQL Workbench can do that, too.

    And you can use the mysqlcheck command line tool which should be able to do that, too.

    0 讨论(0)
提交回复
热议问题