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 [2014/12/02 12:04] – marko | it-wiki:mysql:faq [2023/03/24 07:18] (aktuell) – [Tips to reduce the size of MySQL Database] marko | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ==== zeigen aller Views ==== | + | ====== MySQL - Kleine Hilfe ====== |
+ | ===== Abfrage über mehrere Tabellen mit inner join ===== | ||
+ | <code bash> | ||
+ | select familienname, | ||
+ | </ | ||
+ | ===== zeigen aller Views ===== | ||
<code bash> | <code bash> | ||
SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE ' | SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE ' | ||
</ | </ | ||
- | ==== zeige die CREATE VIEW Syntax ==== | + | ===== zeige die CREATE VIEW Syntax |
<code bash> | <code bash> | ||
SHOW CREATE VIEW my_view_name; | SHOW CREATE VIEW my_view_name; | ||
</ | </ | ||
- | ==== fügt eine neue Spalte in eine existierenden Tabelle ein ==== | + | ===== fügt eine neue Spalte in eine existierenden Tabelle ein ===== |
<code bash> | <code bash> | ||
alter table t_hosts add mac_address varchar(17); | alter table t_hosts add mac_address varchar(17); | ||
</ | </ | ||
- | ==== ändert den Spaltennamen einer Tabelle ==== | + | ===== ändert den Spaltennamen einer Tabelle |
<code bash> | <code bash> | ||
alter table t_hosts change mac_adress mac_address varchar(17); | alter table t_hosts change mac_adress mac_address varchar(17); | ||
</ | </ | ||
- | ==== zeigt Spalteneigenschaften einer Tabelle an ==== | + | ===== zeigt Spalteneigenschaften einer Tabelle an ===== |
<code bash> | <code bash> | ||
explain t_hosts; | explain t_hosts; | ||
</ | </ | ||
- | ==== ändert einen Tabelleneintrag ==== | + | ===== ändert einen Tabelleneintrag |
<code bash> | <code bash> | ||
update t_hosts set hostname=' | update t_hosts set hostname=' | ||
</ | </ | ||
- | ==== zeigt die Rechte des Users an ==== | + | ===== zeigt die Rechte des Users an ===== |
<code bash> | <code bash> | ||
show grants for ' | show grants for ' | ||
</ | </ | ||
+ | ===== löscht alle Privilegien des Users ===== | ||
+ | <code bash> | ||
+ | revoke all privileges on `tuxnetlocal`.* from ' | ||
+ | </ | ||
+ | ===== User Rechte vergeben ===== | ||
+ | <code bash> | ||
+ | GRANT all privileges on `KMyMoney`.* TO ' | ||
+ | </ | ||
+ | |||
+ | ===== 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 ==== | ||
+ | Use the following queries to monitor and evaluation table and index size. | ||
+ | |||
+ | Query below returns the size of each Database in MB. | ||
+ | <code bash> | ||
+ | MariaDB [(none)]> | ||
+ | FROM information_schema.tables | ||
+ | GROUP BY table_schema; | ||
+ | +--------------------+---------------+ | ||
+ | | DB Name | DB Size in MB | | ||
+ | +--------------------+---------------+ | ||
+ | | hope | ||
+ | | information_schema | 0.2 | | ||
+ | | mysql | 1.9 | | ||
+ | | performance_schema | 0.0 | | ||
+ | +--------------------+---------------+ | ||
+ | </ | ||
+ | |||
+ | Query below returns the size of each Table in MB. | ||
+ | <code bash> | ||
+ | SELECT table_schema as `Database`, | ||
+ | FROM information_schema.TABLES | ||
+ | ORDER BY (data_length + index_length) DESC | ||
+ | LIMIT 5; -- adjust it according to your needs | ||
+ | +----------+------------------------+------------+ | ||
+ | | Database | Table | Size in MB | | ||
+ | +----------+------------------------+------------+ | ||
+ | | hope | eth_products | ||
+ | | hope | eth_customers | ||
+ | | hope | eth_emails | ||
+ | | hope | eth_id | ||
+ | | mysql | help_topic | ||
+ | +----------+------------------------+------------+ | ||
+ | </ | ||
+ | |||
+ | Query below returns index size ordered from the ones using the most. | ||
+ | <code bash> | ||
+ | SELECT table_schema as database_name, | ||
+ | FROM information_schema.tables | ||
+ | WHERE table_type = 'BASE TABLE' | ||
+ | and table_schema not in (' | ||
+ | and table_schema = 'your database name' | ||
+ | ORDER BY index_size desc; | ||
+ | +---------------+------------------------+------------+ | ||
+ | | database_name | table_name | ||
+ | +---------------+------------------------+------------+ | ||
+ | | hope | eth_products | ||
+ | | hope | eth_customers | ||
+ | | hope | eth_emails | ||
+ | | hope | eth_id | ||
+ | | hope | eth_temp | ||
+ | +---------------+------------------------+------------+ | ||
+ | </ | ||
+ | |||
+ | ==== Delete Unwanted Data ==== | ||
+ | The easier and the hardest way to reduce MySQL size is by deleting all the unwanted data. DB admins usually fill in DB tables or columns with unnecessary data. A DB schema reevaluation is essential to identify such cases. | ||
+ | |||
+ | The following query helps determine the last time a table is updated. | ||
+ | <code bash> | ||
+ | SELECT table_schema, | ||
+ | WHERE table_schema not in (' | ||
+ | and engine is not null and ((update_time < (now() - interval 1 day)) or update_time is NULL) | ||
+ | LIMIT 5; | ||
+ | +--------------+------------------------+---------------------+---------------------+ | ||
+ | | table_schema | table_name | ||
+ | +--------------+------------------------+---------------------+---------------------+ | ||
+ | | MariaDB | ||
+ | | MariaDB | ||
+ | | MariaDB | ||
+ | | MariaDB | ||
+ | | MariaDB | ||
+ | +--------------+------------------------+---------------------+---------------------+ | ||
+ | </ | ||
+ | |||
+ | When all unused data are unidentified the following commands will help you delete them : | ||
+ | |||
+ | <color # | ||
+ | <code bash> | ||
+ | DELETE FROM table1 / TRUNCATE table1 | ||
+ | DELETE FROM table1 WHERE condition | ||
+ | DROP TABLE table --Deletes table | ||
+ | DROP DATABASE | ||
+ | ALTER TABLE table_name DROP column_name; | ||
+ | </ | ||
+ | |||
+ | ==== Find and Remove Unused Indexes ==== | ||
+ | A general rule of thumb is that the more indexes you have on a table, the slower '' | ||
+ | |||
+ | By default, statistics are not collected. This is to ensure that statistics collection does not cause any extra load on the server unless desired. | ||
+ | |||
+ | 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.1417521897.txt.gz · Zuletzt geändert: 2014/12/02 12:04 von marko