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).

NOTE: data_free isn't a great measure. Certainly it excludes any free space in indexes on the table (i.e. if you optimize the table you may find that the size on disk shrinks by more than the amount of data_free... unless data_free is larger than data_length+index_length, which most likely indicates that, at some point, a large delete has happened).

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

INNODB_DEFAULT_ROW_FORMAT

Default: DYNAMIC

SELECT @@innodb_default_row_format;

You can override the default row format during a CREATE or ALTER TABLE operation if required. 

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.6

SELECT @@innodb_file_per_table;

This configuration option controls whether each table has its own .frm and .ibd files in your database directory.


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

See: ANALYZE TABLE

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

NOTE that this will temporarily create a copy of the table so be sure you have enough space.
NOTE: The table will be locked for writes for the duration of the optimize operation.
Info: OPTIMIZE is an alias for ALTER TABLE...FORCE on Innodb tables

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 that this will temporarily create a copy of the table so be sure you have enough space.
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

+--------------------------------+----------------+---------------+---------------+---------------+---------------+| table_name                     | table_noidx_MB | table_size_MB | table_free_MB | table_size_GB | table_free_GB |+--------------------------------+----------------+---------------+---------------+---------------+---------------+| mytable1                       | 11             | 74            | 107           | 0             | 0             |+--------------------------------+----------------+---------------+---------------+---------------+---------------+
Based on this output we would expect the table size on disk to be ~181MB

ls -l /mysql/data/mydatabase/mytable1.ibd

-rw-rw---- 1 mysql mysql 197132288 Jun  1 20:02 /mysql/data/mydatabase/mytable1.ibd
In reality table size on disk is 197MB

optimize table mytable1;

+-----------------------+----------+----------+-------------------------------------------------------------------+| Table                 | Op       | Msg_type | Msg_text                                                          |+-----------------------+----------+----------+-------------------------------------------------------------------+| mydatabase.mytable1   | optimize | note     | Table does not support optimize, doing recreate + analyze instead || mydatabase.mytable1   | optimize | status   | OK                                                                |+-----------------------+----------+----------+-------------------------------------------------------------------+2 rows in set (1.13 sec)
This isn't suggesting anything is wrong, it's just highlighting that "optimize" on innodb works differently to "optimize" on MyISAM. See Bug #108101.
+--------------------------------+----------------+---------------+---------------+---------------+---------------+| table_name                     | table_noidx_MB | table_size_MB | table_free_MB | table_size_GB | table_free_GB |+--------------------------------+----------------+---------------+---------------+---------------+---------------+| mytable1                       | 4              | 14            |            | 0             | 0             |+--------------------------------+----------------+---------------+---------------+---------------+---------------+
Based on this output we would expect the new table size on disk to be ~14MB

ls -l /mysql/data/mydatabase/mytable1.ibd

-rw-rw---- 1 mysql mysql 14680064 Jun  1 20:18 /mysql/data/mydatabase/mytable1.ibd
Size on disk matches estimate... 14MB

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.html
Note 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.html

To 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

https://www.tutorialspoint.com/mysql/mysql-create-tables.htmhttps://www.mysqltutorial.org/import-csv-file-mysql-table/https://dev.mysql.com/doc/refman/8.0/en/windows-restrictions.htmlhttps://www.forknerds.com/reduce-the-size-of-mysql/
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