目录
概述
整型 字符串 存储日期时间
Unixtime 存储日期时间
结论
运行环境:
CentOS:7.6
Clickhouse:20.6.1.4066
中文 | 和秒的计量 | 英文缩写 | 英文全称 |
毫秒 | 0.001s | ms | millisecond |
微妙 | 0.000 001s | us | microsecond |
纳秒 | 0.000 000 001s | ns | nanosecond |
Clickhouse最多只支持到纳秒,多数情况下只需要计算到微妙级别的即可。
需求:日期时间格式存储为整数,方便计算加速,但是在前端显示和计算过程中会出现类型转换,而clickhouse对数据类型要求比较严格。日期时间可以被存储为datetime类型,String类型,Int类型。
Clickhouse> select now() as dt,toYYYYMMDDhhmmss(dt) dt_int,toString(dt) dt_str,parseDateTimeBestEffort(toString(dt_int)) datetime;
SELECT
now() AS dt,
toYYYYMMDDhhmmss(dt) AS dt_int,
toString(dt) AS dt_str,
parseDateTimeBestEffort(toString(dt_int)) AS datetime
┌──────────────────dt─┬─────────dt_int─┬─dt_str──────────────┬────────────datetime─┐
│ 2020-07-28 10:01:42 │ 20200728100142 │ 2020-07-28 10:01:42 │ 2020-07-28 10:01:42 │
└─────────────────────┴────────────────┴─────────────────────┴─────────────────────┘
1 rows in set. Elapsed: 0.005 sec.
可以看到各种数据类型之间的转换。
也可以将这整形的日期转为string 通过截取在拼接为日期,但是比较麻烦,不如clickhouse官方的函数简洁明了。
方法二:
select toYYYYMMDDhhmmss(now()) as starttime,
toDateTime(concat(
substring(toString(starttime), 1, 4), '-',
substring(toString(starttime), 5, 2), '-',
substring(toString(starttime), 7, 2), ' ',
substring(toString(starttime), 9, 2), ':',
substring(toString(starttime), 11, 2), ':',
substring(toString(starttime), 13, 2))) as datetime;
┌──────starttime─┬────────────datetime─┐
│ 20200728102629 │ 2020-07-28 10:26:29 │
└────────────────┴─────────────────────┘
方法三:
SELECT 20201028101818 as expire_date, toDateTime(expire_date),
concat(toString(floor(expire_date/10000000000)), '-',
toString(floor((expire_date%10000000000)/100000000)), '-',
toString(floor((expire_date%100000000)/1000000)), ' ',
toString(floor((expire_date%1000000)/10000)), ':',
toString(floor((expire_date%10000)/100)), ':',
toString(floor((expire_date%10000)/100))) as str, toDateTime(str);
20201028101818 │ 2026-12-10 21:18:50 │ 2020-10-28 10:18:18 │ 2020-10-28 10:18:18
此方法有严重缺陷 ,不推荐时间。不够通用。当遇到月份和日期是一位数的时候会报错。
clickhouse 支持毫秒 微妙 和纳秒.
Clickhouse> select now() dt,now64(3) ms,now64(6) us,now64(9) ns;
SELECT
now() AS dt,
now64(3) AS ms,
now64(6) AS us,
now64(9) AS ns
┌──────────────────dt─┬──────────────────────ms─┬─────────────────────────us─┬────────────────────────────ns─┐
│ 2020-07-28 10:33:19 │ 2020-07-28 10:33:19.070 │ 2020-07-28 10:33:19.070586 │ 2020-07-28 10:33:19.070596096 │
└─────────────────────┴─────────────────────────┴────────────────────────────┴───────────────────────────────┘
1 rows in set. Elapsed: 0.006 sec.
Clickhouse对毫秒 微妙和纳秒的函数支持:
SELECT *
FROM system.functions
WHERE lower(name) LIKE 'fromunix%'
UNION ALL
SELECT *
FROM system.functions
WHERE lower(name) LIKE 'tounix%'
FORMAT PrettyCompactMonoBlock
┌─name─────────────────────┬─is_aggregate─┬─case_insensitive─┬─alias_to─┐
│ fromUnixTimestamp64Nano │ 0 │ 0 │ │
│ fromUnixTimestamp64Micro │ 0 │ 0 │ │
│ fromUnixTimestamp64Milli │ 0 │ 0 │ │
│ toUnixTimestamp64Nano │ 0 │ 0 │ │
│ toUnixTimestamp64Micro │ 0 │ 0 │ │
│ toUnixTimestamp64Milli │ 0 │ 0 │ │
│ toUnixTimestamp │ 0 │ 0 │ │
└──────────────────────────┴──────────────┴──────────────────┴──────────┘
7 rows in set. Elapsed: 0.005 sec. Processed 1.57 thousand rows, 52.38 KB (332.43 thousand rows/s., 11.08 MB/s.)
对于正常的日期也可以转为unixtime来处理:
Clickhouse> select now() as dt,toYYYYMMDDhhmmss(dt) dt_int,toString(dt) dt_str,parseDateTimeBestEffort(toString(dt_int)) datetime;
SELECT
now() AS dt,
toYYYYMMDDhhmmss(dt) AS dt_int,
toString(dt) AS dt_str,
parseDateTimeBestEffort(toString(dt_int)) AS datetime
┌──────────────────dt─┬─────────dt_int─┬─dt_str──────────────┬────────────datetime─┐
│ 2020-07-28 10:32:52 │ 20200728103252 │ 2020-07-28 10:32:52 │ 2020-07-28 10:32:52 │
└─────────────────────┴────────────────┴─────────────────────┴─────────────────────┘
1 rows in set. Elapsed: 0.005 sec.
Clickhouse> select now64() dt,toUnixTimestamp(dt) ut,toDateTime(ut) datetime;
SELECT
now64() AS dt,
toUnixTimestamp(dt) AS ut,
toDateTime(ut) AS datetime
┌──────────────────────dt─┬─────────ut─┬────────────datetime─┐
│ 2020-07-28 10:34:25.161 │ 1595903665 │ 2020-07-28 10:34:25 │
└─────────────────────────┴────────────┴─────────────────────┘
1 rows in set. Elapsed: 0.005 sec.
对于微妙的unixtime的支持:
Clickhouse> select now64(3) dt,toUnixTimestamp64Milli(dt) ut,fromUnixTimestamp64Milli(ut) datetime;
SELECT
now64(3) AS dt,
toUnixTimestamp64Milli(dt) AS ut,
fromUnixTimestamp64Milli(ut) AS datetime
┌──────────────────────dt─┬────────────ut─┬────────────────datetime─┐
│ 2020-07-28 10:34:47.731 │ 1595903687731 │ 2020-07-28 10:34:47.731 │
└─────────────────────────┴───────────────┴─────────────────────────┘
1 rows in set. Elapsed: 0.002 sec.
对于毫秒的支持:
lickhouse> select now64(6) dt,toUnixTimestamp64Micro(dt) ut,fromUnixTimestamp64Micro(ut) datetime;
SELECT
now64(6) AS dt,
toUnixTimestamp64Micro(dt) AS ut,
fromUnixTimestamp64Micro(ut) AS datetime
┌─────────────────────────dt─┬───────────────ut─┬───────────────────datetime─┐
│ 2020-07-28 10:35:23.209645 │ 1595903723209645 │ 2020-07-28 10:35:23.209645 │
└────────────────────────────┴──────────────────┴────────────────────────────┘
1 rows in set. Elapsed: 0.005 sec.
对于纳秒的支持:
Clickhouse> select now64(9) dt,toUnixTimestamp64Nano(dt) ut,fromUnixTimestamp64Nano(ut) datetime;
SELECT
now64(9) AS dt,
toUnixTimestamp64Nano(dt) AS ut,
fromUnixTimestamp64Nano(ut) AS datetime
┌────────────────────────────dt─┬──────────────────ut─┬──────────────────────datetime─┐
│ 2020-07-28 10:35:49.248605839 │ 1595903749248605839 │ 2020-07-28 10:35:49.248605839 │
└───────────────────────────────┴─────────────────────┴───────────────────────────────┘
1 rows in set. Elapsed: 0.005 sec.
注意:
unixtiime需要使用对应的解析函数,否则解析出来的日期是不对的:
Clickhouse> select now64(9) dt,toUnixTimestamp64Nano(dt) ut,fromUnixTimestamp64Nano(ut) datetime_ns,fromUnixTimestamp64Micro(ut) us, fromUnixTimestamp64Milli(ut) ms;
SELECT
now64(9) AS dt,
toUnixTimestamp64Nano(dt) AS ut,
fromUnixTimestamp64Nano(ut) AS datetime_ns,
fromUnixTimestamp64Micro(ut) AS us,
fromUnixTimestamp64Milli(ut) AS ms
┌────────────────────────────dt─┬──────────────────ut─┬───────────────────datetime_ns─┬─────────────────────────us─┬──────────────────────ms─┐
│ 2020-07-28 10:37:10.983299230 │ 1595903830983299230 │ 2020-07-28 10:37:10.983299230 │ 2106-02-07 51:43:03.299230 │ 2106-01-27 05:34:59.230 │
└───────────────────────────────┴─────────────────────┴───────────────────────────────┴────────────────────────────┴─────────────────────────┘
1 rows in set. Elapsed: 0.006 sec.
1.日期时间转为整形:toYYYYMMDDhhmmss()
将字符串型的日期转为时间类型:parseDateTimeBestEffort()
2.日期时间转为unixtime:toUnixTimestamp()
unixtime转为日期时间:toDateTime()
参考:
https://clickhouse.tech/docs/en/sql-reference/data-types/datetime/
https://clickhouse.tech/docs/en/sql-reference/data-types/datetime64/
https://clickhouse.tech/docs/en/sql-reference/functions/date-time-functions/