MySQL Tables
Check
SHOW DATABASES;
SHOW TABLES;
DESCRIBE myTable;
SHOW CREATE TABLE myTable;
SHOW INDEXES FROM myTable;
mysqlshow
mysqlshow myDatabase
mysqlshow myDatabase myTable
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES WHERE NAME LIKE 'myDatabase/myTable' \G
SHOW TABLE STATUS LIKE 'mytable'\G
Unused Space
This is visible as Data_free in the output of SHOW TABLE STATUS. You can show data for all tables using the query below...
SELECT table_name,
FORMAT(data_length/POWER(1024,2),0) AS table_noidx_MB,
FORMAT(SUM(data_length+index_length)/POWER(1024,2),0) AS table_size_MB,
FORMAT(data_free/POWER(1024,2),0) AS table_free_MB,
FORMAT(SUM(data_length+index_length)/POWER(1024,3),0) AS table_size_GB,
FORMAT(data_free/POWER(1024,3),0) AS table_free_GB
FROM information_schema.tables
WHERE table_schema='alfresco_uat'
GROUP BY table_name
ORDER BY data_free DESC;
To reclaim space you could Optimize the table (see later on this page).
Create
CREATE TABLE myTable (myVarcharCol VARCHAR(20), myCharCol CHAR(1), myDateCol DATE);
A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed...
CREATE TEMPORARY TABLE myTable (myVarcharCol VARCHAR(20), myCharCol CHAR(1), myDateCol DATE);
An example including indexes and constraints...
CREATE TABLE myTable ( myInt INT AUTO_INCREMENT,
myVC1 VARCHAR(30) NOT NULL,
myVC2 VARCHAR(30) NOT NULL,
first VARCHAR(30),
last VARCHAR(30),
PRIMARY KEY(myInt),
UNIQUE(myVC1),
INDEX myVC2i(myVC2) INVISIBLE,
INDEX name(first, last) COMMENT 'first and/or last' );
Configuration
Default: DYNAMIC
SELECT @@innodb_default_row_format;
You can override the default row format during a CREATE or ALTER TABLE operation if required.
DYNAMIC - The DYNAMIC row format offers the same storage characteristics as the COMPACT row format but adds enhanced storage capabilities for long variable-length columns and supports large index key prefixes (1). Valid for Barracuda File Format only.
COMPACT - The COMPACT row format reduces row storage space by about 20% compared to the REDUNDANT row format, at the cost of increasing CPU use for some operations. If your workload is a typical one that is limited by cache hit rates and disk speed, COMPACT format is likely to be faster. If the workload is limited by CPU speed, compact format might be slower (1). Valid for Antelope and Barracuda File Formats.
REDUNDANT - The REDUNDANT format provides compatibility with older versions of MySQL (1). Valid for Antelope and Barracuda File Formats.
The COMPRESSED row format, which is not supported for use in the system tablespace, cannot be defined as the default. The COMPRESSED row format offers the same storage characteristics and capabilities as the DYNAMIC row format but adds support for table and index data compression (1). Valid for Barracuda File Format only.
To see the row format of a specific table...
SELECT *
FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES
WHERE NAME LIKE 'mydb/mytable' \G
To ovverride the default row format at table level...
ALTER TABLE mytable ROW_FORMAT=COMPRESSED;
INNODB_FILE_PER_TABLE
Default: 1 (ON)
The default has been 1 since MySQL 5.6.6SELECT @@innodb_file_per_table;
This configuration option controls whether each table has its own .frm and .ibd files in your database directory.
1 - ON
0 - OFF
When innodb_file_per_table is OFF then all table data is stored in the ibdata1 file (i.e. individual .frm files will exist but .ibd files will not)
INNODB_CHECKSUM_ALGORITHM
SELECT @@innodb_checksum_algorithm;
Specifies how to generate and verify the checksum stored in the disk blocks of InnoDB tablespaces. crc32 is the default value as of MySQL 5.7.7. (2)
LOWER_CASE_TABLE_NAMES
SELECT @@lower_case_table_names;
This configuration option controls whether each table name is case-insensitive.
lower_case_table_names can only be configured when initializing the server.
Changing the lower_case_table_names setting after the server is initialized is prohibited. (3)
Load Data
To load data from a CSV file...
LOAD DATA INFILE '/tmp/myfile.csv' INTO TABLE myTable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
For Windows use UNIX style forward slashes "/" or double up backslashes "\\"Optimize
Running Optimize regularly is not recommended.
Optimize only when there is likely to be a benefit. For example, if you have performed a bulk DELETE of many records (which you do not expect to be replaced).
To reclaim space in a table...
OPTIMIZE TABLE mytable;
You can provide a comma separated list of tables to optimize multiple tables in the same operation.An alternative method...
sudo mysqlcheck -o mydatabase mytable -u myuser -p mypasswd
Percona
If you have Percona tools installed you can use this to optimize tables without locking the table...
pt-online-schema-change --alter "ENGINE=InnoDB" D=mydatabase, t=mytable --execute
NOTE pt-online-schema-schange avoids locking by creating triggers on the original table that insert, update or delete on the temporary copy of the table whilst the optimize is in progress.
Example
ls -l /mysql/data/mydatabase/mytable1.ibd
optimize table mytable1;
ls -l /mysql/data/mydatabase/mytable1.ibd
Troubleshooting
Check for Errors
This applies to InnoDB. For MyISAM options refer to the documentation.https://dev.mysql.com/doc/refman/5.7/en/check-table.htmlNote that running this command does carry a degree of risk... from blocked threads to server exit.https://dev.mysql.com/doc/refman/5.7/en/check-table.html#check-table-innodb
At its simplest...
CHECK TABLE myTable;
Do not scan the rows for incorrect links.
CHECK TABLE myTable QUICK;
Rebuilding or Repairing
https://dev.mysql.com/doc/refman/5.7/en/rebuilding-tables.htmlTo rebuild by dumping and reloading...
mysqldump myDatabase myTable > MyDumpFile.sql
mysql myDatabase < MyDumpFile.sql
To rebuild an InnoDB Table using ALTER TABLE...
ALTER TABLE myTable ENGINE = InnoDB;
The REPAIR TABLE command can be used for non InnoDB table types (MyISAM, ARCHIVE, CSV)
ERROR 2013 (HY000): Lost connection to MySQL server during query
If this happens during a DDL operation (e.g. ALTER TABLE) you could be left with some temporary tables identifiable (assuming you are using innodb_file_per_table) by a leading hash (#). To get rid of them you may need to do this (2)...
DROP TABLE`#mysql50##temporarytablename`;
Bibliography & References
Indexhttps://opensource.com/article/17/5/speed-your-mysql-queries-300-timeshttps://stackoverflow.com/questions/40251700/unknown-table-table-name-in-information-schema
ROW_FORMAThttps://mariadb.com/kb/en/innodb-row-formats-overview/(1) https://dev.mysql.com/doc/refman/5.7/en/innodb-row-format.html
innodb_file_per_tablehttps://mariadb.com/resources/blog/get-rid-of-orphaned-innodb-temporary-tables-the-right-way/https://serverfault.com/questions/487159/what-is-the-ibdata1-file-in-my-var-lib-mysql-directoryhttps://dba.stackexchange.com/questions/226210/why-was-default-of-innodb-file-per-table-changed
innodb_checksum_algorithm(2) https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_checksum_algorithmhttps://jira.mariadb.org/browse/MDEV-25105
lower_case_table_nameshttps://www.simplified.guide/mysql-mariadb/case-insensitive-table-linux(3) https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.htmlhttps://stackoverflow.com/questions/51803216/lower-case-table-names-settings-in-mysql-8-0-12
Optimizehttps://planet.mysql.com/entry/?id=23373https://phoenixnap.com/kb/mysql-optimize-tablehttps://www.thegeekstuff.com/2016/04/mysql-optimize-table/https://www.percona.com/blog/how-to-reclaim-space-in-innodb-when-innodb_file_per_table-is-on/https://dba.stackexchange.com/questions/35520/what-is-the-difference-between-optimize-table-and-analyze-table-table-in-mysqlhttps://dba.stackexchange.com/questions/83380/how-much-space-is-needed-to-reclaim-space-from-a-table-in-innodbhttps://dba.stackexchange.com/questions/76867/appropriate-use-of-mysql-optimize-tablehttps://dba.stackexchange.com/questions/110736/mysql-indexes-maintenancehttps://stackoverflow.com/questions/19300893/why-is-mysqls-data-free-larger-than-data-and-indexes-combinedhttps://stackoverflow.com/questions/321461/when-should-database-indexes-be-rebuilthttps://stackoverflow.com/questions/30635603/what-does-table-does-not-support-optimize-doing-recreate-analyze-instead-mehttps://stackoverflow.com/questions/2961899/analyze-table-optimize-table-how-oftenhttps://bugs.mysql.com/bug.php?id=108101
Best Practicehttps://dev.mysql.com/doc/refman/8.0/en/innodb-best-practices.html
Troubleshootinghttps://mariadb.com/resources/blog/get-rid-of-orphaned-innodb-temporary-tables-the-right-way/https://serverfault.com/questions/364391/huge-sql-xxxx-xxxx-ibd-files-in-mysql-data-folder
5.7https://dev.mysql.com/doc/refman/5.7/en/check-table.htmlhttps://dev.mysql.com/doc/refman/5.7/en/rebuilding-tables.html
5.6https://dev.mysql.com/doc/refman/5.6/en/creating-tables.htmlhttps://dev.mysql.com/doc/refman/5.6/en/show-tables.htmlhttps://dev.mysql.com/doc/refman/5.6/en/create-table.htmlhttps://dev.mysql.com/doc/refman/5.6/en/create-temporary-table.htmlhttps://dev.mysql.com/doc/refman/5.6/en/alter-table.htmlhttps://dev.mysql.com/doc/refman/5.6/en/load-data.htmlhttps://dev.mysql.com/doc/refman/5.6/en/insert.htmlhttps://dev.mysql.com/doc/refman/5.6/en/replace.htmlhttps://dev.mysql.com/doc/refman/5.6/en/update.htmlhttps://dev.mysql.com/doc/refman/5.6/en/select.htmlhttps://dev.mysql.com/doc/refman/5.6/en/delete.html