it-wiki:mysql:faq
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
it-wiki:mysql:faq [2023/03/24 07:10] – [Tips to reduce the size of MySQL Database] marko | it-wiki:mysql:faq [2023/03/24 07:18] (aktuell) – [Tips to reduce the size of MySQL Database] marko | ||
---|---|---|---|
Zeile 42: | Zeile 42: | ||
===== Tips to reduce the size of MySQL Database ===== | ===== Tips to reduce the size of MySQL Database ===== | ||
+ | ==== Backup, first but not least ==== | ||
+ | Best practices suggest to backup your database before take any dangerous action. MySQL and MariaDB include the mysqldump utility to simplify the process to create a backup of a database or system of databases. | ||
+ | <code bash> | ||
+ | # Backup Single DB | ||
+ | mysqldump -u [username] -p [databaseName] > [filename]-$(date +%F).sql | ||
+ | # Backup Entire DBMS | ||
+ | mysqldump --all-databases --single-transaction --quick --lock-tables=false > full-backup-$(date +%F).sql -u root -p | ||
+ | </ | ||
+ | |||
+ | If you are using MariaDB, you have the option of full or incremental backup via '' | ||
+ | <code bash> | ||
+ | $ mariabackup --backup --target-dir=/ | ||
+ | </ | ||
==== List MySQL Table and Index Size ==== | ==== List MySQL Table and Index Size ==== | ||
Use the following queries to monitor and evaluation table and index size. | Use the following queries to monitor and evaluation table and index size. | ||
Zeile 133: | Zeile 146: | ||
To enable statistics dynamically execute the following command : | To enable statistics dynamically execute the following command : | ||
+ | <code bash> | ||
+ | SET GLOBAL userstat=1; | ||
+ | </ | ||
+ | |||
+ | Now, every query to the database updates the statistic tables. The '' | ||
+ | <code bash> | ||
+ | SELECT * FROM INFORMATION_SCHEMA.TABLE_STATISTICS; | ||
+ | +--------------+------------------------+-----------+--------------+------------------------+ | ||
+ | | TABLE_SCHEMA | TABLE_NAME | ||
+ | +--------------+------------------------+-----------+--------------+------------------------+ | ||
+ | | hope | eth_table1 | ||
+ | | hope | eth_table2 | ||
+ | | hope | eth_table3 | ||
+ | +--------------+------------------------+-----------+--------------+------------------------+ | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | SELECT * FROM INDEX_STATISTICS; | ||
+ | +--------------+-------------------+------------+-----------+ | ||
+ | | TABLE_SCHEMA | TABLE_NAME | ||
+ | +--------------+-------------------+------------+-----------+ | ||
+ | | hope | eth_table1 | ||
+ | | hope | eth_table2 | ||
+ | +--------------+-------------------+------------+-----------+ | ||
+ | </ | ||
+ | |||
+ | With the help of the new tables, you can find all unused indexes in a single query. | ||
+ | <code bash> | ||
+ | SELECT DISTINCT s.TABLE_SCHEMA, | ||
+ | FROM information_schema.statistics `s` LEFT JOIN information_schema.index_statistics INDXS | ||
+ | ON (s.TABLE_SCHEMA = INDXS.TABLE_SCHEMA AND | ||
+ | | ||
+ | | ||
+ | WHERE INDXS.TABLE_SCHEMA IS NULL; | ||
+ | </ | ||
+ | |||
+ | Finally, to delete index run this command in '' | ||
+ | |||
+ | <code bash> | ||
+ | DROP INDEX index_name ON table_name; | ||
+ | </ | ||
+ | |||
+ | Do not forget to set '' | ||
+ | |||
+ | ==== Shrink and Optimize MySQL ==== | ||
+ | In general MySQL InnoDB does not release disk space after deleting data rows from the table. It keeps the space to reuse it later. | ||
+ | |||
+ | '' | ||
+ | |||
+ | Optimization is available only when '' | ||
+ | <code bash> | ||
+ | MariaDB [(none)]> | ||
+ | +-----------------------+-------+ | ||
+ | | Variable_name | ||
+ | +-----------------------+-------+ | ||
+ | | innodb_file_per_table | ON | | ||
+ | +-----------------------+-------+ | ||
+ | </ | ||
+ | |||
+ | You can use '' | ||
+ | <code bash> | ||
+ | OPTIMIZE table MyTable; | ||
+ | +-------------------+----------+----------+----------+ | ||
+ | | Table | Op | Msg_type | Msg_text | | ||
+ | +-------------------+----------+----------+----------+ | ||
+ | | testDB.MyTable | ||
+ | +-------------------+----------+----------+----------+ | ||
+ | </ | ||
+ | |||
+ | When innodb_file_per_table is OFF, then all data is going to be stored in ibdata files. If you drop some tables and delete some data, then there are two ways to reclaim that unused disk: | ||
+ | |||
+ | * Completely dump the database, then drop it and finally reload it | ||
+ | * Change the storage engine and revert it back to your previous configuration. This will definitively recreate the table and indexes from the start and unused disk space will be reclaimed. | ||
+ | |||
+ | <code bash> | ||
+ | ALTER TABLE my_table ENGINE = MyISAM; | ||
+ | ALTER TABLE my_table ENGINE = InnoDB; | ||
+ | </ | ||
+ | |||
+ | Whenever you run OPTIMIZE or ALTER TABLE , MySQL will create a new data file until the operation is finished. Do not forget to have enough available space for the operations to finish successfully! | ||
+ | |||
+ | If you want to optimize a 10 GB Table, ensure that there are more than 10GB of free disk space. |
it-wiki/mysql/faq.1679641831.txt.gz · Zuletzt geändert: 2023/03/24 07:10 von marko