Choosing the Right MySQL Date/Time Type: DATETIME vs TIMESTAMP vs Unix Timestamp
This article explains why storing dates as strings is inefficient, compares MySQL DATETIME, TIMESTAMP, and numeric Unix timestamps—including storage size, range, timezone handling, and performance—and adds PostgreSQL equivalents and practical recommendations for selecting the appropriate type.
Background
Storing time information is a fundamental requirement in software development, affecting business logic, data accuracy, and system stability. Selecting the proper MySQL date‑time type is therefore critical.
Common Pitfall: Storing Dates as Strings
Using VARCHAR or other string types for dates leads to two major problems:
Space inefficiency: Strings consume more storage than native date‑time types.
Query and calculation inefficiency: String comparisons are lexicographic, slower, and cannot leverage built‑in date functions; indexing on strings is less effective.
Choosing Between DATETIME and TIMESTAMP
DATETIME stores the literal date‑time value without any timezone information. The value you insert is stored exactly as provided.
TIMESTAMP converts the input value to UTC on storage and converts it back to the session’s timezone on retrieval, making it suitable for multi‑timezone applications.
Example table and queries demonstrate the behavior:
CREATE TABLE `time_zone_test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`date_time` datetime DEFAULT NULL,
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO time_zone_test(date_time,time_stamp) VALUES(NOW(),NOW()); SELECT date_time, time_stamp FROM time_zone_test;Result (session timezone UTC+0):
+---------------------+---------------------+
| date_time | time_stamp |
+---------------------+---------------------+
| 2020-01-11 09:53:32 | 2020-01-11 09:53:32 |
+---------------------+---------------------+After changing the session timezone to UTC+8:
SET time_zone = '+8:00'; # TIMESTAMP value automatically converts to UTC+8
+---------------------+---------------------+
| date_time | time_stamp |
+---------------------+---------------------+
| 2020-01-11 09:53:32 | 2020-01-11 17:53:32 |
+---------------------+---------------------+Numeric Unix Timestamp
Many developers store the Unix epoch seconds (or milliseconds) in an INT/BIGINT column. This offers the same advantages as TIMESTAMP regarding timezone‑independent storage and fast numeric comparisons, but sacrifices human readability.
-- Convert a datetime string to a Unix timestamp (seconds)
SELECT UNIX_TIMESTAMP('2020-01-11 09:53:32');
-- Convert a Unix timestamp back to a datetime string
SELECT FROM_UNIXTIME(1578707612);PostgreSQL Equivalents
PostgreSQL does not have a DATETIME type. Its TIMESTAMP WITHOUT TIME ZONE corresponds to MySQL’s DATETIME (stores literal values), while TIMESTAMPTZ (or TIMESTAMP WITH TIME ZONE) behaves like MySQL’s TIMESTAMP, handling UTC conversion automatically.
Recommendation Summary
Use TIMESTAMP when your application needs automatic timezone handling; be aware of the 2038‑year limit.
Choose DATETIME if you do not need timezone conversion and require dates beyond 2038.
Opt for a numeric Unix timestamp for maximum performance and cross‑system compatibility, accepting reduced readability.
Illustrations
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
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.
