Performance Comparison of datetime, timestamp, and bigint for Storing Time in MySQL
This article evaluates how MySQL handles time data stored as datetime, timestamp, and bigint by inserting half a million records and measuring query, grouping, and sorting speeds, concluding that bigint generally offers the best performance for time‑range operations.
When storing time values in a MySQL database, developers can choose between datetime , timestamp , or bigint . This article investigates which type provides the best performance for common operations such as range queries, grouping, and ordering.
Data Preparation
A table named users is created with three columns representing the same moment in different formats.
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time_date` datetime NOT NULL,
`time_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`time_long` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `time_long` (`time_long`),
KEY `time_timestamp` (`time_timestamp`),
KEY `time_date` (`time_date`)
) ENGINE=InnoDB AUTO_INCREMENT=500003 DEFAULT CHARSET=latin1The corresponding Java entity class uses Date , Timestamp , and long fields.
/**
* @author hetiantian
* @date 2018/10/21
*/
@Builder
@Data
public class Users {
/** Auto‑increment unique id */
private Long id;
/** date type */
private Date timeDate;
/** timestamp type */
private Timestamp timeTimestamp;
/** long type */
private long timeLong;
}The MyBatis mapper inserts records into the table.
/**
* @author hetiantian
* @date 2018/10/21
*/
@Mapper
public interface UsersMapper {
@Insert("insert into users(time_date, time_timestamp, time_long) value(#{timeDate}, #{timeTimestamp}, #{timeLong})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int saveUsers(Users users);
}A test class inserts 500,000 rows, each with the current system time.
public class UsersMapperTest extends BaseTest {
@Resource
private UsersMapper usersMapper;
@Test
public void test() {
for (int i = 0; i < 500000; i++) {
long time = System.currentTimeMillis();
usersMapper.saveUsers(Users.builder()
.timeDate(new Date(time))
.timeLong(time)
.timeTimestamp(new Timestamp(time))
.build());
}
}
}SQL Query Speed Test
datetime range query: select count(*) from users where time_date >= "2018-10-21 23:32:44" and time_date <= "2018-10-21 23:41:22" – 0.171 s
timestamp range query: select count(*) from users where time_timestamp >= "2018-10-21 23:32:44" and time_timestamp <= "2018-10-21 23:41:22" – 0.351 s
bigint range query: select count(*) from users where time_long >= 1540135964091 and time_long <= 1540136482372 – 0.130 s
Conclusion: In InnoDB, range queries perform best with bigint , followed by datetime , then timestamp .
SQL Group By Speed Test
Group by datetime : 0.176 s
Group by timestamp : 0.173 s
Conclusion: Grouping by timestamp is slightly faster than datetime , but the difference is minimal.
SQL Order By Speed Test
Order by datetime : 1.038 s
Order by timestamp : 0.933 s
Order by bigint : 0.775 s
Conclusion: Ordering by bigint is the fastest, followed by timestamp , then datetime .
Summary
If you need to perform operations such as range queries or sorting on a time column, bigint is recommended. If the time field is only stored without further manipulation, timestamp saves space (4 bytes) but is limited to dates before 2038.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.