table of Contents
int type storage time-time conversion
Convert the result to an IP address
Selection of data type
1. Use the smallest data type that can save your data. (Time type data: you can use the varchar type, you can use the int type, you can also use the timestamp type)
2. Use a simple data type, int is simpler than varchar type in mysql processing. (Int type storage time is the best choice)
3. Use not null to define fields as much as possible. (Due to the characteristics of innodb, the value of null requires additional storage in the field, and it will also increase the cost of IO and storage.)
4. Try to use the text type as little as possible. It is best to consider the table when it is necessary.
int type storage time-time conversion
Use int to store date and time, and use FROM_UNIXTIME (), UNIX_TIMESTAMP () two functions to convert.
- unix_timestamp () function is to convert the date format data to int type
- FROM_UNIXTIME (timestr) function is to convert int type to time format
Create Table
create table test(
id int auto_increment not null,
timestr int ,
primary key(id)
);
Import Data
insert into test (timestr) values (unix_timestamp('2018-05-29 16:00:00'));
Fetch data
IP address storage
In our external application, we must record the ip address. In most cases, varchar (15) is used for storage, which requires 15 bytes for storage, but bigint only requires 8 bytes for storage. Use INET_ATON (), INET_NTOA () two functions to convert.
Create Table
create table sessions(
id int auto_increment not null,
ipaddress bigint,
primary key (id)
);
Import Data
insert into sessions (ipaddress)values (inet_aton('192.168.0.1'));
Convert the result to an IP address
select inet_ntoa(ipaddress) from sessions;