Comprehensive Guide to Millisecond Timestamps in SQL Databases

Nov 25, 2025 · Programming · 9 views · 7.8

Keywords: MySQL | PostgreSQL | Timestamp | Millisecond Precision | UNIX_TIMESTAMP

Abstract: This article provides an in-depth exploration of various methods to obtain millisecond-precision timestamps in mainstream databases like MySQL and PostgreSQL. By analyzing the usage techniques of core functions such as UNIX_TIMESTAMP, CURTIME, and date_part, it details the conversion process from basic second-level timestamps to precise millisecond-level timestamps. The article also covers time precision control, cross-platform compatibility considerations, and best practices in real-world applications, offering developers a complete solution for timestamp processing.

Fundamental Concepts of Timestamps

In database systems, timestamps are crucial for recording time information. Traditional timestamps are typically accurate to the second, but in modern application scenarios, millisecond-precision timestamps are becoming increasingly important. Millisecond-level timestamps provide finer time records, suitable for high-concurrency systems, performance monitoring, log recording, and other scenarios requiring precise time ordering.

Implementing Millisecond Timestamps in MySQL

MySQL offers various functions for obtaining timestamps, with UNIX_TIMESTAMP() being the most commonly used basic function. This function returns the number of seconds since January 1, 1970, but by default does not include millisecond information. To achieve millisecond precision, we need to extend it by combining other functions.

In MySQL 5.6 and above, the following method can be used to obtain millisecond timestamps:

SELECT ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000)

The working principle of this query is: first obtain the current time with 4 decimal places (microseconds) via CURTIME(4), then convert it to a second-level timestamp using UNIX_TIMESTAMP(), and finally multiply by 1000 and round to get the millisecond timestamp. The parameter 4 indicates retaining 4 decimal places, ensuring sufficient precision.

Alternative Approaches in PostgreSQL

PostgreSQL uses a different function system for handling timestamps. The basic method to obtain Unix timestamps is:

SELECT round(date_part('epoch', now()))

Here, date_part('epoch', now()) extracts the number of seconds since January 1, 1970, and the round() function performs rounding. To achieve millisecond precision, it can be modified to:

SELECT round(extract(epoch from now()) * 1000)

Time Precision and Storage Considerations

In practical applications, timestamp precision not only affects query results but also relates to data storage. MySQL's default timestamp type does not include fractional parts, meaning that directly using the TIMESTAMP type cannot store millisecond precision.

To support millisecond timestamp storage, precision must be specified in the table definition:

CREATE TABLE example_table ( id INT PRIMARY KEY, event_time TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) )

Here, TIMESTAMP(3) indicates timestamp precision with 3 decimal places (milliseconds), and CURRENT_TIMESTAMP(3) ensures that the default value also includes millisecond information.

Advanced Techniques and Considerations

For scenarios requiring higher precision, MySQL supports timestamps with up to 6 decimal places (microseconds):

SELECT CURRENT_TIMESTAMP(6)

However, it is important to note that the actual available precision may be affected by the operating system. On Windows systems, typically only 3 decimal places (milliseconds) precision can be achieved, while on Linux systems, up to 6 decimal places (microseconds) precision is possible.

Another practical technique is creating custom functions to simplify the acquisition of millisecond timestamps:

CREATE FUNCTION unix_ms() RETURNS BIGINT DETERMINISTIC BEGIN RETURN FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000); END

After creation, the current millisecond timestamp can be obtained via a simple SELECT unix_ms() call.

Cross-Database Compatibility Considerations

Although timestamp functions differ across database systems, the core principles are similar. When developing cross-database applications, it is recommended to:

Performance Optimization Recommendations

Millisecond timestamp operations can impact performance, especially in high-concurrency scenarios:

By implementing appropriate timestamp strategies, system performance can be maintained while ensuring precision.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.