Mysql
 SQL >> Database >  >> RDS >> Mysql

Controllare la dimensione di un database in MySQL

In MySQL puoi interrogare la tabella information_schema.tables per ottenere informazioni sulle tabelle di un database. Questa tabella include dettagli come lunghezza dei dati, lunghezza degli indici, collation, data di creazione e altro. Puoi usarla per calcolare la dimensione di un singolo database o di tutti i database presenti sul server.

Puoi ottenere informazioni simili anche tramite l'interfaccia grafica MySQL Workbench.

Esempio SQL

SELECT table_schema 'Database Name', SUM(data_length + index_length) 'Size in Bytes', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MiB' FROM information_schema.tables GROUP BY table_schema;

Risultato:

+--------------------+---------------+-------------+
| Database Name      | Size in Bytes | Size in MiB |
+--------------------+---------------+-------------+
| information_schema | 0             | 0.00        |
| Music              | 98304         | 0.09        |
| mysql              | 2506752       | 2.39        |
| performance_schema | 0             | 0.00        |
| sakila             | 6766592       | 6.45        |
| Solutions          | 16384         | 0.02        |
| sys                | 16384         | 0.02        |
| world              | 802816        | 0.77        |
+--------------------+---------------+-------------+

Qui la dimensione è mostrata in byte e in mebibyte. Puoi scegliere l'unità che preferisci.

Puoi anche limitare l'analisi a un singolo database:

SELECT table_schema 'Database Name', SUM(data_length + index_length) 'Size in Bytes', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MiB' FROM information_schema.tables WHERE table_schema = 'sakila';

Risultato:

+---------------+---------------+-------------+
| Database Name | Size in Bytes | Size in MiB |
+---------------+---------------+-------------+
| sakila        | 6766592       | 6.45        |
+---------------+---------------+-------------+

La funzione FORMAT_BYTES()

Puoi usare sys.FORMAT_BYTES() per evitare di convertire manualmente la dimensione. Questa funzione restituisce un valore leggibile con unità appropriate, come bytes, KiB, MiB, GiB, TiB o PiB.

SELECT table_schema 'Database Name', SUM(data_length + index_length) 'Size in Bytes', sys.FORMAT_BYTES(SUM(data_length + index_length)) 'Size (Formatted)' FROM information_schema.tables GROUP BY table_schema;

Risultato:

+--------------------+---------------+------------------+
| Database Name      | Size in Bytes | Size (Formatted) |
+--------------------+---------------+------------------+
| information_schema | 0             | 0 bytes          |
| Music              | 98304         | 96.00 KiB        |
| mysql              | 2506752       | 2.39 MiB         |
| performance_schema | 0             | 0 bytes          |
| sakila             | 6766592       | 6.45 MiB         |
| Solutions          | 16384         | 16.00 KiB        |
| sys                | 16384         | 16.00 KiB        |
| world              | 802816        | 784.00 KiB       |
+--------------------+---------------+------------------+

MySQL Workbench

Puoi controllare la dimensione del database anche da MySQL Workbench:

  1. Apri il database nel pannello Schemas
  2. Passa sopra il database desiderato
  3. Fai clic sull'icona informativa accanto al nome del database

Le informazioni includono dimensione approssimativa, numero di tabelle, collation e altri dettagli, solitamente nella scheda Info.