Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Software Developer
MySQL Optimizer team
January, 2020
Martin Hansson
How to avoid the most common pitfalls of date and tim...
The following is intended to outline our general product direction. It is intended for information purposes only, and
may ...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted3
Software Engineer
Op...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted4
5
4
3
2
1
Stick with...
The Main Players
TIMESTAMP
Format
Limits
Time
Zone
32-bit seconds since Epoch (time_t)
1970-01-01 00:00:01 UTC to
2038-01-...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted6
The Daylight Saving ...
CET
00:00
00:30
01:00
01:30
03:00
03:30
04:00
04:30
23:00
23:30
00:00
00:30
01:00
01:30
02:00
02:30
UTC
Adjusting to Summe...
CET
01:00
01:30
02:00
02:30
02:00
02:30
03:00
03:30
23:00
23:30
00:00
00:30
01:00
01:30
02:00
02:30
UTC
Adjusting to Winte...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted9
mysql> SELECT from_u...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted10
mysql> SET time_zon...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted11
Ordering
mysql> SET...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted12
Ordering
mysql> SET...
MySQL Architecture
Indexes
Tables
Comparison Operators
Filesort
SQL layer
Storage
Engine
Layer
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted14
• Strictly ±hh:mm
•...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted15
Client/Server proto...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted16
mysql> INSERT INTO ...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted17
mysql> INSERT INTO ...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted18
5
4
3
2
1
Stick wit...
SQL Standard
DATE
TIME WITHOUT TIME ZONE
TIME WITH TIME ZONE
TIMESTAMP WITHOUT TIME ZONE
TIMESTAMP WITH TIME ZONE
DATE
TIM...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted20
5
4
3
2
1
Stick wit...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted21
5
4
3
2
1
Stick wit...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted22
0000-00-00 00:00:00...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted23
• Automatic initial...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted24
What is it?
Automat...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted25
Old since MySQL 5.6...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted26
• Automatic initial...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted27
What is it?
Three-v...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted28
What is it?
Three-v...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted29
What is it?
Three-v...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted30
• Automatic initial...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted31
• Automatic initial...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted32
• Automatic initial...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted33
5
4
3
2
1
Stick wit...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted34
5
4
3
2
1
Stick wit...
Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted35
Truncated
Removed i...
ThankYou
Questions?
Martin Hansson
Prochain SlideShare
Chargement dans…5
×

Everything you always wanted to know about datetime types but didn’t have time to ask

140 vues

Publié le

How to avoid the most common pitfalls of date and time types in MySQL

Publié dans : Données & analyses
  • Soyez le premier à commenter

Everything you always wanted to know about datetime types but didn’t have time to ask

  1. 1. Software Developer MySQL Optimizer team January, 2020 Martin Hansson How to avoid the most common pitfalls of date and time types in MySQL Everything you always wanted to know about datetime types but didn’t have time to ask
  2. 2. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Safe harbor statement Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted2
  3. 3. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted3 Software Engineer OptimizerTeam Oracle Martin Hansson
  4. 4. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted4 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as time zone on servers When in doubt, choose DATETIME over TIMESTAMP Take-home points
  5. 5. The Main Players TIMESTAMP Format Limits Time Zone 32-bit seconds since Epoch (time_t) 1970-01-01 00:00:01 UTC to 2038-01-19 04:14:07 UTC Session time zone in server, UTC in SE DATETIME Bit-encoded year, month, day, etc, 5 bytes. 1000-01-01 00:00:00 to 9999-12-31 23:59:59 None
  6. 6. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted6 The Daylight Saving Problem
  7. 7. CET 00:00 00:30 01:00 01:30 03:00 03:30 04:00 04:30 23:00 23:30 00:00 00:30 01:00 01:30 02:00 02:30 UTC Adjusting to SummerTime
  8. 8. CET 01:00 01:30 02:00 02:30 02:00 02:30 03:00 03:30 23:00 23:30 00:00 00:30 01:00 01:30 02:00 02:30 UTC Adjusting to WinterTime
  9. 9. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted9 mysql> SELECT from_unixtime(1572136200) AS tG *************************** 1. row *************************** t: 2019-10-27 02:30:00 1 row in set (0.02 sec) MySQL Chooses the Later time mysql> SELECT unix_timestamp('2019-10-27 02:30:00') AS tG *************************** 1. row *************************** t: 1572139800 1 row in set (0.01 sec) mysql> SELECT from_unixtime(1572139800) AS tG *************************** 1. row *************************** t: 2019-10-27 02:30:00 1 row in set (0.02 sec) = 0:30 UTC = 1:30 UTC = 1:30 UTC
  10. 10. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted10 mysql> SET time_zone= 'UTC'; mysql> CREATE TABLE t( a TIMESTAMP ); mysql> INSERT INTO t VALUES -> ( '2019-10-27 00:30' ), -> ( '2019-10-27 00:59' ), -> ( '2019-10-27 01:00' ); Ordering
  11. 11. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted11 Ordering mysql> SET time_zone = 'CET'; mysql> SELECT * FROM t ORDER BY a; +---------------------+ | a | +---------------------+ | 2019-10-27 02:30:00 | | 2019-10-27 02:59:00 | | 2019-10-27 02:00:00 | +---------------------+
  12. 12. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted12 Ordering mysql> SET time_zone = 'CET'; mysql> SELECT * FROM t ORDER BY a; +---------------------+ | a | +---------------------+ | 2019-10-27 02:30:00 | | 2019-10-27 02:59:00 | | 2019-10-27 02:00:00 | +---------------------+
  13. 13. MySQL Architecture Indexes Tables Comparison Operators Filesort SQL layer Storage Engine Layer
  14. 14. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted14 • Strictly ±hh:mm • -14:00 to +14:00 • Only with ISO8601 Extended Format: • YYYY-MM-DD␣hh:mm:ss±hh:mm • YYYY-MM-DDThh:mm:ss±hh:mm • Only valid dates. Store the Time You Want New since 8.0.19 2020-01-27 10:25:39+01:00
  15. 15. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted15 Client/Server protocol Store the Time You Want New since 8.0.19 Length Year Month Day Hour Minute Second Fractional Second Time Zone 1 2 1 1 1 1 1 4 2
  16. 16. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted16 mysql> INSERT INTO times VALUES ( '2019-10-27 00:30:00+00:00' ); Query OK, 1 row affected (0.02 sec) mysql> SELECT * FROM times; +---------------------+ | time_stamp | +---------------------+ | 2019-10-27 02:30:00 | +---------------------+ 1 row in set (0.00 sec) Store the Time You Want New since 8.0.19
  17. 17. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted17 mysql> INSERT INTO times VALUES ( '2019-10-27 00:30:00+00:00' ); Query OK, 1 row affected (0.02 sec) mysql> SELECT /*+ SET_VAR(time_zone = UTC) */ * FROM times; +---------------------+ | time_stamp | +---------------------+ | 2019-10-27 00:30:00 | +---------------------+ 1 row in set (0.00 sec) Read What’s Stored New since 8.0.17
  18. 18. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted18 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as server time zone When in doubt, use DATETIME over TIMESTAMP Take-home points
  19. 19. SQL Standard DATE TIME WITHOUT TIME ZONE TIME WITH TIME ZONE TIMESTAMP WITHOUT TIME ZONE TIMESTAMP WITH TIME ZONE DATE TIME DATETIME TIMESTAMP MySQL
  20. 20. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted20 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as server time zone When in doubt, use DATETIME over TIMESTAMP Take-home points
  21. 21. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted21 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as server time zone When in doubt, use DATETIME over TIMESTAMP Take-home points
  22. 22. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted22 0000-00-00 00:00:00 The Zero Date
  23. 23. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted23 • Automatic initialization • Easier • Less space • Efficiency • Legacy Why use it? Zero Dates
  24. 24. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted24 What is it? Automatic Initialization of Dates CREATE TABLE member ( … signup_time TIMESTAMP DEFAULT NOW() ); CREATE TABLE member ( … last_updated TIMESTAMP ON UPDATE NOW() );
  25. 25. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted25 Old since MySQL 5.6.5 Automatic Initialization of Dates CREATE TABLE member ( id INT PRIMARY KEY, joined DATETIME DEFAULT NOW(), really_joined DATETIME DEFAULT NOW(), last_updated DATETIME ON UPDATE NOW() );
  26. 26. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted26 • Automatic initialization • Easier • Less space • Efficiency • Legacy Why use it? Zero Dates
  27. 27. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted27 What is it? Three-valued Logic isYour Friend SELECT * FROM member WHERE signup_time IS UNKNOWN; SELECT * FROM member WHERE last_update IS UNKNOWN;
  28. 28. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted28 What is it? Three-valued Logic isYour Friend SELECT signup_time FROM member WHERE signup_time < ‘2018-10-01’; +---------------------+ | signup_time | +---------------------+ | 2018-09-10 11:07:51 | | … | +---------------------+
  29. 29. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted29 What is it? Three-valued Logic isYour Friend SELECT signup_time FROM member WHERE signup_time < ‘2018-10-01’; +---------------------+ | signup_time | +---------------------+ | 0000-00-00 00:00:00 | | … | +---------------------+
  30. 30. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted30 • Automatic initialization • Easier • Less space • Efficiency • Legacy Why use it? Zero Dates
  31. 31. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted31 • Automatic initialization • Easier • Less space • Efficiency • Legacy Why use it? Zero Dates
  32. 32. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted32 • Automatic initialization • Easier • Less space • Efficiency • Legacy Why use it? Zero Dates
  33. 33. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted33 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as server time zone When in doubt, use DATETIME over TIMESTAMP Take-home points
  34. 34. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted34 5 4 3 2 1 Stick with ISO formats Don’t use invalid dates Prefer NULL over zero dates Use UTC as server time zone When in doubt, use DATETIME over TIMESTAMP Take-home points
  35. 35. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted35 Truncated Removed in ISO8601:2004 Basic Extended 2020-01-30T10:00:00, 2020-01-30T10:00 The ISO8601 Standard 20200130100000 20200130T100000 200130100000 RFC3339 2020-01-30 10:00:00, 2020-01-30 10:00
  36. 36. ThankYou Questions? Martin Hansson

×