sql >> Database teknologi >  >> RDS >> Mysql

Sådan optimerer du MySQL/MariaDB-tabeller

Kommandoerne fra dette indlæg fungerer på MySQL- og MariaDB-serveren.

Det er en god idé at udføre databasevedligeholdelse fra tid til anden. En ting er at gøre er at optimere tabellerne. Vi har to muligheder:

1. OPTIMER TABEL kommando

Omorganiserer den fysiske lagring af tabeldata og tilknyttede indeksdata for at reducere lagerplads og forbedre I/O-effektiviteten ved adgang til tabellen. De nøjagtige ændringer, der foretages i hver tabel, afhænger af den lagermaskine, der bruges af den tabel.

Se nedenfor, hvordan du bruger det.

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 [~]#

Hvis du vil køre kommandoen for flere tabeller fra den samme database, skal du bruge:

OPTIMIZE TABLE table1,table2,table3;

OPTIMIZE TABLE fungerer med InnoDB-, MyISAM- og ARCHIVE-tabeller.

2. mysqlcheck kommando

Mysqlcheck-klienten udfører tabelvedligeholdelse:Den kontrollerer, reparerer, optimerer eller analyserer tabeller.

For at kontrollere en tabel, brug:mysqlcheck db_name tbl_name
For at kontrollere alle tabeller fra en database:mysqlcheck –databases db_name
For at kontrollere tabellerne fra alle databaserne på serveren:mysqlcheck –alle-databaser

Bemærk, at databasetabeller er låste mens mysqlcheck kører. Ingen poster kan indsættes eller slettes fra tabellerne.

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 [~]# 

For at optimere en database skal du bruge:

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 [~]#

For at optimere hele databasen på serveren skal du bruge:

root@web [~]# mysqlcheck -o -A

Ressourcer:
OPTIMER TABEL manual
mysqlcheck manual


  1. Opret en lagret procedure i SQL Server 2017

  2. deaktiver BEMÆRKNINGER i psql-output

  3. Er primærnøgler passé?

  4. Vælg Forespørgsel for at hente rækker i MySQL