Thursday, July 27, 2017

Database Storage allocation/Choosing Right Column Types mysql

Here integer type along with minimum maximum value length and storage in byte.

TypeStorageMinimum ValueMaximum Value
(Bytes)(Signed/Unsigned)(Signed/Unsigned)
TINYINT1-128127
0255
SMALLINT2-3276832767
065535
MEDIUMINT3-83886088388607
016777215
INT4-21474836482147483647
04294967295
BIGINT8-92233720368547758089223372036854775807
018446744073709551615

In contrast to CHAR (0 to 65,535),  VARCHAR (length 0 to 65,535) values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

ValueCHAR(4)Storage RequiredVARCHAR(4)Storage Required
'''    '4 bytes''1 byte
'ab''ab  '4 bytes'ab'3 bytes
'abcd''abcd'4 bytes'abcd'5 bytes
'abcdefgh''abcd'4 bytes'abcd'5 bytes

varchar = 1 byte which is fix + number of characters( 0 to 255) byte
varch = 1+4 = 5 byte (abcd is 4 character+1 = 5 byte)
varchar = 2 byte which is fix + number of characters(256 to 65535) byte

For more text type :

TEXT
String length + 2 bytes
A string with a maximum length of 65,535 characters
MEDIUMTEXT
String length + 3 bytes
A string with a maximum length of 16,777,215 characters
LONGTEXT
String length + 4 bytes
A string with a maximum length of 4,294,967,295 characters
For other field/column type
FLOAT[Length, Decimals]
4 bytes
A small number with a floating decimal point
DOUBLE[Length, Decimals]
8 bytes
A large number with a floating decimal point
DECIMAL[Length, Decimals]
Length + 1 or 2 bytes
DOUBLE stored as a string, allowing for a fixed decimal point
DATE
3 bytes
In the format of YYYY-MM-DD
DATETIME
8 bytes
In the format of YYYY-MM-DD HH:MM:SS
TIMESTAMP
4 bytes
In the format of YYYYMMDDHHMMSS; acceptable range starts in 1970 and ends in the year 2038
TIME
3 bytes
In the format of HH:MM:SS

No comments:

Post a Comment

Total Pageviews