p147~p176.
Simple principle of optimization of data types
- Smaller is usually better.
- Simple enough.
- Try to avoid NULL.
Integer type
- TINYINT(8), SMALLINT(16), MEDIUMINT(24), INT(32), BIGINT(64).
- All types may be added UNSIGNED integer unsigned number, such as
TINYINT UNSIGNED
. - Integer type specifies legal range width, does not limit values as int (11) and the int (20) is the same.
Decimal
- The exact type, DECIMAL, general financial data for storing data of a greater amount of time, can be considered in place with BIGINT DECIMAL, the data is multiplied by a corresponding factor.
- Imprecise type, FLOAT, and DOUBLE.
String type
Each string can define your own character set and collation.
VARCHAR type
- Variable length strings may be stored, or require the use of an additional two-byte string length records.
- UPDATE may be longer than the line to become the original, can not put down within a page, resulting in a line can not be placed in a page, then the MyISAM line will split into different fragments of memory, InnoDB need to split pages, generate a new page to store this line.
- Situation: The maximum length of a string column is much greater than the average length of the column rarely updated, so fragmentation is not a problem; the use of such a complex like UTF-8 character set, each character using different lengths for storage.
- InnoDB will long VARCHAR stored as BLOB.
CHAR type
- Fixed-length type, MySQL Always according to the length of the string allocated enough space defined delete trailing spaces.
- Adapted to store fixed-length character string, such as MD5 value,
The same as VARCHAR (5) and VARCHAR (200) storage cost, but consumes more memory columns longer, especially for memory a temporary table or sort operations.
Type BLOB and TEXT types
- TINYBLOB, SMALLBLOB, BLOB, MEDIUMBLOB, LONGBLOB. BLOB = Similar SMALLBLOB. TEXT type.
- MySQL the BLOB and TEXT values as a separate object processing. When the value is too large, InnoDB using the "external" storage area for storage. At this time takes 1 to 4 bytes to store a pointer value in each row, and then The external storage actual value.
- Difference: BLOB storage type of binary data, there is no collation and character set, TEXT there.
- Special sorting: Sort only do the frontmost max_sort_length bytes of each column, rather than the entire string to be sorted, if only a small part of the front of characters, using ORDER BY SUSTRING (column, length)..
SET max_length_for_sort_data = 1024;
SHOW VARIABLES LIKE '%max_length_for_sort_data%';