Jak uzyskać rozmiary tabel bazy danych MySQL?

Mogę uruchomić to zapytanie, aby uzyskać rozmiary wszystkich tabel w bazie danych MySQL:

show table status from myDatabaseName;
Proszę o pomoc w zrozumieniu wyników. Szukam stołów o największych rozmiarach.

Na którą kolumnę powinienem spojrzeć?

 660
Author: Peter Mortensen, 2012-03-08

15 answers

Możesz użyć tego zapytania, aby pokazać rozmiar tabeli (chociaż najpierw musisz zastąpić zmienne):

SELECT 
    table_name AS `Table`, 
    round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "$DB_NAME"
    AND table_name = "$TABLE_NAME";

Lub to zapytanie do listy wielkości każdej tabeli w każdej bazie danych, najpierw największej:

SELECT 
     table_schema as `Database`, 
     table_name AS `Table`, 
     round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
ORDER BY (data_length + index_length) DESC;
 1503
Author: ChapMic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-06 09:49:52
SELECT TABLE_NAME AS "Table Name", 
table_rows AS "Quant of Rows", ROUND( (
data_length + index_length
) /1024, 2 ) AS "Total Size Kb"
FROM information_schema.TABLES
WHERE information_schema.TABLES.table_schema = 'YOUR SCHEMA NAME/DATABASE NAME HERE'
LIMIT 0 , 30

Możesz pobrać nazwę schematu z "information_schema" -> SCHEMATA table - > "SCHEMA_NAME " column


Dodatkowe Możesz uzyskać Rozmiar baz danych mysql w następujący sposób.

SELECT table_schema "DB Name", 
Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema; 

Wynik

DB Name              |      DB Size in MB

mydatabase_wrdp             39.1
information_schema          0.0
Tutaj możesz uzyskać dodatkowe informacje.
 75
Author: Sumith Harshan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-01 22:13:15
SELECT 
    table_name AS "Table",  
    round(((data_length + index_length) / 1024 / 1024), 2) as size   
FROM information_schema.TABLES  
WHERE table_schema = "YOUR_DATABASE_NAME"  
ORDER BY size DESC; 

Sortuje rozmiary (Rozmiar DB w MB).

 27
Author: Gank,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-03 07:49:20

Jeśli chcesz, aby zapytanie korzystało z aktualnie wybranej bazy danych. po prostu skopiuj wklej to zapytanie. (Nie wymaga modyfikacji)

SELECT table_name ,
  round(((data_length + index_length) / 1024 / 1024), 2) as SIZE_MB
FROM information_schema.TABLES
WHERE table_schema = DATABASE() ORDER BY SIZE_MB DESC;
 17
Author: zainengineer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-04 07:20:20

Jest łatwy sposób na uzyskanie wielu informacji za pomocą Workbencha:

  • Kliknij prawym przyciskiem myszy nazwę schematu i kliknij "Inspektor schematu".

  • W wynikowym oknie Masz kilka zakładek. Pierwsza zakładka "Info" pokazuje przybliżone oszacowanie rozmiaru bazy danych w MB.

  • Druga zakładka, "Tables", pokazuje długość danych i inne szczegóły dla każdej tabeli.

 10
Author: Guppy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-27 15:29:16

Jeśli używasz phpmyadmin to po prostu przejdź do struktury tabeli

Np.

Space usage
Data    1.5 MiB
Index   0   B
Total   1.5 Mi
 6
Author: Almis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-21 14:05:27

Spróbuj wykonać następujące polecenie powłoki (zamień DB_NAME na nazwę bazy danych):

mysql -uroot <<<"SELECT table_name AS 'Tables', round(((data_length + index_length) / 1024 / 1024), 2) 'Size in MB' FROM information_schema.TABLES WHERE table_schema = \"DB_NAME\" ORDER BY (data_length + index_length) DESC;" | head

Dla Drupal / drush solution, sprawdź poniższy przykładowy skrypt, który wyświetli największe tabele w użyciu:

#!/bin/sh
DB_NAME=$(drush status --fields=db-name --field-labels=0 | tr -d '\r\n ')
drush sqlq "SELECT table_name AS 'Tables', round(((data_length + index_length) / 1024 / 1024), 2) 'Size in MB' FROM information_schema.TABLES WHERE table_schema = \"${DB_NAME}\" ORDER BY (data_length + index_length) DESC;" | head -n20
 6
Author: kenorb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-08 14:14:32

Załóżmy, że nazwa bazy danych jest "news_alert" wtedy to zapytanie pokaże rozmiar wszystkich tabel w bazie danych.

Rozmiar wszystkich tabel:

SELECT
  TABLE_NAME AS `Table`,
  ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
  TABLE_SCHEMA = "news_alert"
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;

Wyjście:

    +---------+-----------+
    | Table   | Size (MB) |
    +---------+-----------+
    | news    |      0.08 |
    | keyword |      0.02 |
    +---------+-----------+
    2 rows in set (0.00 sec)

Dla konkretnej tabeli:

SELECT
  TABLE_NAME AS `Table`,
  ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
    TABLE_SCHEMA = "news_alert"
  AND
    TABLE_NAME = "news"
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;

Wyjście:

+-------+-----------+
| Table | Size (MB) |
+-------+-----------+
| news  |      0.08 |
+-------+-----------+
1 row in set (0.00 sec)
 6
Author: Nurul Akter Towhid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-10 17:54:35

Oto inny sposób na rozwiązanie tego problemu z użyciem wiersza poleceń bash.

for i in mysql -NB -e 'show databases'; do echo $i; mysql-e "SELECT table_name AS 'Tables', round (((data_length+index_length)/1024/1024),2) 'Rozmiar W MB' z information_schema.TABLES WHERE table_schema =\ "$i\ "ORDER BY (data_length + index_length) DESC"; done

 4
Author: user1380599,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-10 11:06:34

/ Align = "left" /

Podaj tylko nazwę bazy danych, a następnie posortuj wszystkie tabele w porządku malejącym - od największej do najmniejszej tabeli wewnątrz wybranej bazy danych. Potrzebuje tylko 1 zmiennej do zastąpienia = nazwa bazy danych.

SELECT 
table_name AS `Table`, 
round(((data_length + index_length) / 1024 / 1024), 2) AS `size`
FROM information_schema.TABLES 
WHERE table_schema = "YOUR_DATABASE_NAME_HERE"
ORDER BY size DESC;
 3
Author: dev101,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-31 23:16:40

Inny sposób pokazania liczby wierszy i zajmowanych przez nią przestrzeni i porządkowania przez nią.

SELECT
     table_schema as `Database`,
     table_name AS `Table`,
     table_rows AS "Quant of Rows",
     round(((data_length + index_length) / 1024 / 1024/ 1024), 2) `Size in GB`
FROM information_schema.TABLES
WHERE table_schema = 'yourDatabaseName'
ORDER BY (data_length + index_length) DESC;  

Jedynym ciągiem znaków, który musisz zastąpić w tym zapytaniu, jest "yourDatabaseName".

 1
Author: Nav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-13 06:29:11

Jeśli masz ssh dostęp, możesz po prostu spróbować du -hc /var/lib/mysql (lub innego datadir, Jak ustawiony w my.cnf).

 1
Author: exic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-17 15:54:16

Oblicz Całkowity rozmiar bazy danych na końcu:

(SELECT 
  table_name AS `Table`, 
  round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
  FROM information_schema.TABLES 
  WHERE table_schema = "$DB_NAME"
)
UNION ALL
(SELECT 
  'TOTAL:',
  SUM(round(((data_length + index_length) / 1024 / 1024), 2) )
  FROM information_schema.TABLES 
  WHERE table_schema = "$DB_NAME"
)
 1
Author: MINGSONG HU,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-18 07:10:29
SELECT table_schema, # "DB Name", 
Round(Sum(data_length + index_length) / 1024 / 1024, 1), # "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema; 
 0
Author: William Chen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 03:08:15
SELECT TABLE_NAME AS table_name, 
table_rows AS QuantofRows, 
ROUND((data_length + index_length) /1024, 2 ) AS total_size_kb 
FROM information_schema.TABLES
WHERE information_schema.TABLES.table_schema = 'db'
ORDER BY (data_length + index_length) DESC; 

Wszystkie 2 powyżej są testowane na mysql

 0
Author: William Chen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 03:11:24