MySQL存儲時間類型選擇的問題講解
MySQL中存儲時間通常會用datetime類型,但現(xiàn)在很多系統(tǒng)也用int存儲unix時間戳,它們有什么區(qū)別?本人總結(jié)如下:
int
(1)4個字節(jié)存儲,INT的長度是4個字節(jié),存儲空間上比datatime少,int索引存儲空間也相對較小,排序和查詢效率相對較高一點點
(2)可讀性極差,無法直觀的看到數(shù)據(jù)
TIMESTAMP
(1)4個字節(jié)儲存
(2)值以UTC格式保存
(3)時區(qū)轉(zhuǎn)化 ,存儲時對當(dāng)前的時區(qū)進(jìn)行轉(zhuǎn)換,檢索時再轉(zhuǎn)換回當(dāng)前的時區(qū)。
(4)TIMESTAMP值不能早于1970或晚于2037
datetime
(1)8個字節(jié)儲存
(2)與時區(qū)無關(guān)
(3)以'YYYY-MM-DD HH:MM:SS'格式檢索和顯示DATETIME值。支持的范圍為'1000-01-01 00:00:00'到'9999-12-31 23:59:59'
隨著Mysql性能越來越來高,個人覺得關(guān)于時間的存儲方式,具體怎么存儲看個人習(xí)慣和項目需求吧
分享兩篇關(guān)于int vs timestamp vs datetime性能測試的文章
Myisam:MySQL DATETIME vs TIMESTAMP vs INT 測試儀
CREATE TABLE `test_datetime` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `datetime` FIELDTYPE NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM;
機(jī)型配置
- kip-locking
- key_buffer = 128M
- max_allowed_packet = 1M
- table_cache = 512
- sort_buffer_size = 2M
- read_buffer_size = 2M
- read_rnd_buffer_size = 8M
- myisam_sort_buffer_size = 8M
- thread_cache_size = 8
- query_cache_type = 0
- query_cache_size = 0
- thread_concurrency = 4
測試
DATETIME 14111 14010 14369 130000000
TIMESTAMP 13888 13887 14122 90000000
INT 13270 12970 13496 90000000
執(zhí)行mysql
mysql> select * from test_datetime into outfile ‘/tmp/test_datetime.sql'; Query OK, 10000000 rows affected (6.19 sec) mysql> select * from test_timestamp into outfile ‘/tmp/test_timestamp.sql'; Query OK, 10000000 rows affected (8.75 sec) mysql> select * from test_int into outfile ‘/tmp/test_int.sql'; Query OK, 10000000 rows affected (4.29 sec) alter table test_datetime rename test_int; alter table test_int add column datetimeint INT NOT NULL; update test_int set datetimeint = UNIX_TIMESTAMP(datetime); alter table test_int drop column datetime; alter table test_int change column datetimeint datetime int not null; select * from test_int into outfile ‘/tmp/test_int2.sql'; drop table test_int;
So now I have exactly the same timestamps from the DATETIME test, and it will be possible to reuse the originals for TIMESTAMP tests as well.
mysql> load data infile ‘/export/home/ntavares/test_datetime.sql' into table test_datetime;
Query OK, 10000000 rows affected (41.52 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0mysql> load data infile ‘/export/home/ntavares/test_datetime.sql' into table test_timestamp;
Query OK, 10000000 rows affected, 44 warnings (48.32 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 44mysql> load data infile ‘/export/home/ntavares/test_int2.sql' into table test_int;
Query OK, 10000000 rows affected (37.73 sec)
Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0
As expected, since INT is simply stored as is while the others have to be recalculated. Notice how TIMESTAMP still performs worse, even though uses half of DATETIME storage size.
Let's check the performance of full table scan:
mysql> SELECT SQL_NO_CACHE count(id) FROM test_datetime WHERE datetime > ‘1970-01-01 01:30:00′ AND datetime < ‘1970-01-01 01:35:00′; +———–+ | count(id) | +———–+ | 211991 | +———–+ 1 row in set (3.93 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_timestamp WHERE datetime > ‘1970-01-01 01:30:00′ AND datetime < ‘1970-01-01 01:35:00′; +———–+ | count(id) | +———–+ | 211991 | +———–+ 1 row in set (9.87 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > UNIX_TIMESTAMP('1970-01-01 01:30:00′) AND datetime < UNIX_TIMESTAMP('1970-01-01 01:35:00′); +———–+ | count(id) | +———–+ | 211991 | +———–+ 1 row in set (15.12 sec)
Then again, TIMESTAMP performs worse and the recalculations seemed to impact, so the next good thing to test seemed to be without those recalculations: find the equivalents of those UNIX_TIMESTAMP() values, and use them instead:
mysql> select UNIX_TIMESTAMP('1970-01-01 01:30:00′) AS lower, UNIX_TIMESTAMP('1970-01-01 01:35:00′) AS bigger; +——-+——–+ | lower | bigger | +——-+——–+ | 1800 | 2100 | +——-+——–+ 1 row in set (0.00 sec) mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > 1800 AND datetime < 2100; +———–+ | count(id) | +———–+ | 211991 | +———–+ 1 row in set (1.94 sec)
Innodb:MySQL DATETIME vs TIMESTAMP vs INT performance and benchmarking with InnoDB
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
php+mysql prepare 與普通查詢的性能對比實例講解
prepare可以解決大訪問量的網(wǎng)站給數(shù)據(jù)庫服務(wù)器所帶來的負(fù)載和開銷,本文章通過實例向大家介紹預(yù)查詢prepare與普通查詢的性能對比,需要的朋友可以參考一下2016-11-11MySQL實戰(zhàn)文章(非常全的基礎(chǔ)入門類教程)
半個月時間把MySQL重新鞏固了一遍,梳理了一篇幾萬字超硬核文章,想學(xué)習(xí)mysql的朋友可以看看2023-05-05Mysql系統(tǒng)變量與狀態(tài)變量詳細(xì)介紹
這篇文章主要介紹了Mysql系統(tǒng)變量與狀態(tài)變量詳細(xì)介紹,能夠在程序運行過程中影響Mysql程序行為的變量稱之為系統(tǒng)變量,想了解更多相關(guān)內(nèi)容的小伙伴可以參考下面文章內(nèi)容2022-09-09MySQL按小時查詢數(shù)據(jù),沒有的補(bǔ)0
這篇文章主要介紹了MySQL按小時查詢數(shù)據(jù),沒有的補(bǔ)0,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12MySQL對小數(shù)進(jìn)行四舍五入的操作實現(xiàn)
數(shù)學(xué)函數(shù)是MySQL中常用的一類函數(shù),其主要用于處理數(shù)字,包括整型和浮點數(shù)等等,本文主要介紹了MySQL對小數(shù)進(jìn)行四舍五入的操作實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-08-08解決Java程序使用MySQL時返回參數(shù)為亂碼的示例教程
這篇文章主要介紹了解決Java程序使用MySQL返回參數(shù)為亂碼的問題的教程,通過示例來講解utf-8編碼在MySQL中的相關(guān)配置,示例需要的朋友可以參考下2015-12-12