国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

Mysql存儲日期類型用int、timestamp還是datetime?

2024-07-24 12:36:35
字體:
來源:轉載
供稿:網友

通常存儲時間用datetime類型,現在很多系統也用int存儲時間,它們有什么區別?個人更喜歡使用int這樣對于日期計算時比較好,下面我們一起來看到底那種會好些.

int

(1).4個字節存儲,INT的長度是4個字節,存儲空間上比datatime少,int索引存儲空間也相對較小,排序和查詢效率相對較高一點點

(2)可讀性極差,無法直觀的看到數據,可能讓你很惱火

TIMESTAMP

(1)4個字節儲存

(2)值以UTC格式保存

(3)時區轉化 ,存儲時對當前的時區進行轉換,檢索時再轉換回當前的時區。

(4)TIMESTAMP值不能早于1970或晚于2037

datetime

(1)8個字節儲存

(2)與時區無關

(3)以'YYYY-MM-DD HH:MM:SS'格式檢索和顯示DATETIME值。支持的范圍為'1000-01-01 00:00:00'到'9999-12-31 23:59:59'

mysql也是這兩年才流行,性能越來越來,具體怎么存儲看個人習慣和項目需求吧.

分享兩篇關于int vs timestamp vs datetime性能測試的文章.

  1. Myisam:MySQL DATETIME vs TIMESTAMP vs INT 測試儀 
  2. CREATE TABLE `test_datetime` ( 
  3. `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  4. `datetime` FIELDTYPE NOT NULL
  5. PRIMARY KEY (`id`) 
  6. ) ENGINE=MyISAM; 

機型配置

  1. kip-locking 
  2. key_buffer = 128M 
  3. max_allowed_packet = 1M 
  4. table_cache = 512 
  5. sort_buffer_size = 2M 
  6. read_buffer_size = 2M 
  7. read_rnd_buffer_size = 8M 
  8. myisam_sort_buffer_size = 8M 
  9. thread_cache_size = 8 
  10. query_cache_type = 0 
  11. query_cache_size = 0 
  12. thread_concurrency = 4 

測試

  1. DATETIME   14111 14010        14369     130000000 
  2. TIMESTAMP  13888        13887        14122     90000000 
  3. INT        13270        12970        13496     90000000 

執行mysql

  1. mysql> select * from test_datetime into outfile ‘/tmp/test_datetime.sql’; 
  2. Query OK, 10000000 rows affected (6.19 sec) 
  3.  
  4. mysql> select * from test_timestamp into outfile ‘/tmp/test_timestamp.sql’; 
  5. Query OK, 10000000 rows affected (8.75 sec) 
  6.  
  7. mysql> select * from test_int into outfile ‘/tmp/test_int.sql’; 
  8. Query OK, 10000000 rows affected (4.29 sec) 
  9.  
  10. alter table test_datetime rename test_int; 
  11. alter table test_int add column datetimeint INT NOT NULL; 
  12. update test_int set datetimeint = UNIX_TIMESTAMP(datetime); 
  13. alter table test_int drop column datetime; 
  14. alter table test_int change column datetimeint datetime int not null
  15. select * from test_int into outfile ‘/tmp/test_int2.sql’; 
  16. drop table test_int; 
  17. 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. 
  18. mysql> load data infile ‘/export/home/ntavares/test_datetime.sql’ into table test_datetime; 
  19. Query OK, 10000000 rows affected (41.52 sec) 
  20. Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0 
  21.  
  22. mysql> load data infile ‘/export/home/ntavares/test_datetime.sql’ into table test_timestamp; 
  23. Query OK, 10000000 rows affected, 44 warnings (48.32 sec) 
  24. Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 44 
  25.  
  26. mysql> load data infile ‘/export/home/ntavares/test_int2.sql’ into table test_int; 
  27. Query OK, 10000000 rows affected (37.73 sec) 
  28. Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0 
  29. 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. 
  30. Let’s check the performance of full table scan: 
  31. 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′; 
  32. +———–+ 
  33. | count(id) | 
  34. +———–+ 
  35. |    211991 | 
  36. +———–+ 
  37. 1 row in set (3.93 sec) 
  38.  
  39. 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′; 
  40. +———–+ 
  41. | count(id) | 
  42. +———–+ 
  43. |   211991 | 
  44. +———–+ 
  45. 1 row in set (9.87 sec) 
  46.  
  47. 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′); 
  48. +———–+ 
  49. | count(id) | 
  50. +———–+ 
  51. |    211991 | 
  52. +———–+ 
  53. 1 row in set (15.12 sec) 
  54. 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: 
  55. mysql> select UNIX_TIMESTAMP(’1970-01-01 01:30:00′) AS lower, UNIX_TIMESTAMP(’1970-01-01 01:35:00′) AS bigger; 
  56. +——-+——–+ 
  57. | lower | bigger | 
  58. +——-+——–+ 
  59. |  1800 |   2100 | 
  60. +——-+——–+ 
  61. 1 row in set (0.00 sec) 
  62.  
  63. mysql> SELECT SQL_NO_CACHE count(id) FROM test_int WHERE datetime > 1800 AND datetime < 2100; 
  64. +———–+//開源軟件:Vevb.com 
  65. | count(id) | 
  66. +———–+ 
  67. |    211991 | 
  68. +———–+ 
  69. 1 row in set (1.94 sec)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长阳| 丽水市| 岢岚县| 大荔县| 博爱县| 垫江县| 肇州县| 隆尧县| 石景山区| 长乐市| 鄂伦春自治旗| 台南市| 巨野县| 陈巴尔虎旗| 乳源| 三江| 汤阴县| 新津县| 江门市| 金溪县| 古交市| 盖州市| 昌江| 新安县| 吴堡县| 宁津县| 金塔县| 桂东县| 凤山县| 志丹县| 晋城| 英德市| 山东| 葵青区| 宁武县| 阜新| 绥棱县| 栾川县| 泸州市| 三穗县| 科技|