Os comandos deste post funcionarão no servidor MySQL e MariaDB.
É uma boa ideia realizar a manutenção do banco de dados de tempos em tempos. Uma coisa a fazer é otimizar as tabelas. Temos duas opções:
1. OTIMIZAR TABELA comando
Reorganiza o armazenamento físico de dados de tabela e dados de índice associados, para reduzir o espaço de armazenamento e melhorar a eficiência de E/S ao acessar a tabela. As alterações exatas feitas em cada tabela dependem do mecanismo de armazenamento usado por essa tabela.
Veja abaixo como usá-lo.
root@web [~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3670
Server version: 10.1.22-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use roundcube
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [roundcube]> OPTIMIZE TABLE cache;
+-----------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+-----------------+----------+----------+-------------------------------------------------------------------+
| roundcube.cache | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| roundcube.cache | optimize | status | OK |
+-----------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.04 sec)
MariaDB [roundcube]> quit
Bye
root@web [~]#
Se você deseja executar o comando para várias tabelas do mesmo banco de dados, use:
OPTIMIZE TABLE table1,table2,table3;
OPTIMIZE TABLE funciona com tabelas InnoDB, MyISAM e ARCHIVE.
2. mysqlcheck comando
O cliente mysqlcheck realiza manutenção de tabelas:verifica, repara, otimiza ou analisa tabelas.
Para verificar uma tabela use:mysqlcheck db_name tbl_name
Para verificar todas as tabelas de um banco de dados:mysqlcheck –databases db_name
Para verificar as tabelas de todos os bancos de dados no servidor:mysqlcheck –all-databases
Observe que as tabelas do banco de dados estão bloqueadas enquanto o mysqlcheck está em execução. Nenhum registro pode ser inserido ou excluído das tabelas.
root@web [~]# mysqlcheck roundcube
roundcube.cache OK
roundcube.cache_index OK
roundcube.cache_messages OK
roundcube.cache_shared OK
roundcube.cache_thread OK
roundcube.contactgroupmembers OK
roundcube.contactgroups OK
roundcube.contacts OK
roundcube.cp_schema_version OK
roundcube.dictionary OK
roundcube.identities OK
roundcube.searches OK
roundcube.session OK
roundcube.system OK
roundcube.users OK
root@web [~]#
Para otimizar um banco de dados, use:
root@web [~]# mysqlcheck -o roundcube
roundcube.cache
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.cache_index
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.cache_messages
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.cache_shared
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.cache_thread
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.contactgroupmembers
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.contactgroups
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.contacts
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.cp_schema_version Table is already up to date
roundcube.dictionary
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.identities
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.searches
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.session
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.system
note : Table does not support optimize, doing recreate + analyze instead
status : OK
roundcube.users
note : Table does not support optimize, doing recreate + analyze instead
status : OK
root@web [~]#
Para otimizar todo o banco de dados no servidor use:
root@web [~]# mysqlcheck -o -A
Recursos:
OTIMIZAR TABELA manual
manual do mysqlcheck