This article mainly shares with you the method of selecting the mysql field type. Decibel shares with you the integer and time types (choose the accurate type), hoping to help everyone.
Occupy as little storage space as possible
Integer type
Time type (choose the accurate Type)
Type |
Meaning |
time() |
Hours, minutes and seconds |
datetime() |
Year, month, day Hours, minutes and seconds |
##year() | Year |
date() | 年月日 |
##timestamp()
Timestamp( Number of seconds elapsed from 1970-1-1 to now) |
|
It is best to integrate data with a fixed length
char (number of characters)
Fixed length, fast running speed, occupying more storage Length: 255 character limit varchar( Number of bytes)
The length is not fixed, and the content is relatively small and requires position operation. This type should reserve 1-2 bytes to save the length of the current data Length: 65535 bytes limit Storage Chinese characters, such as character set utf8 (each Chinese character occupies 3 bytes), can store up to 65535/3-2 Bytes Information is best stored as integer
时间信息可以存储为整型的(时间戳)
Copy after login
set集合类型 多选:set(‘篮球’,’足球’,’棒球’,’乒乓球’);enum枚举类型 单选: enum(‘男’,’女’,’保密’);
推荐使用set和enum类型,内部会通过整型信息参数具体计算、运行。
Copy after login
ip地址也可以变为整型信息进行存储(mysql内部有算法,把ip变为数字):
mysql: inet_aton(ip) inet_ntoa(数字)php: ip2long(ip) long2ip(数字)
Copy after login
Summary:
Principle of field type selection: small space occupied, The data length is preferably fixed and the data content is preferably of integer type
The above is the detailed content of mysql field type selection method. For more information, please follow other related articles on the PHP Chinese website!