Analyze the selection problem of storing time and date types in MySQL

Analyze the selection problem of storing time and date types in MySQL

In general applications, we use timestamp, datetime, int types to store time formats:

int (corresponding to Integer or int in javaBean)

1. Occupies 4 bytes

2. After indexing, query speed is fast

3. Condition range search can use between

4. Cannot use the time functions provided by MySQL

Conclusion: Suitable for data tables that require a large number of time range queries

datetime (Date type is used in javaBean)

1. Occupies 8 bytes

2. Allows empty values, can customize values, and the system will not automatically modify its value.

3. Actual format storage (Just stores what you have stored and retrieves the same thing which you have stored.)

4. It has nothing to deal with the TIMEZONE and Conversion.

5. You cannot set a default value, so if null values ​​are not allowed, you must manually specify the value of the datetime field to successfully insert data.

6. You can use the now() variable to automatically insert the current time of the system when specifying the value of the datetime field.

Conclusion: The datetime type is suitable for recording the original creation time of data, because no matter how you change the values ​​of other fields in the record, the value of the datetime field will not change unless you change it manually.

timestamp (Date or Timestamp type in javaBean)

1. Occupies 4 bytes

2. Empty values ​​are allowed, but custom values ​​are not allowed, so empty values ​​have no meaning.

3. TIMESTAMP values ​​cannot be earlier than 1970 or later than 2037. This means that a date such as '1968-01-01', while valid for a DATETIME or DATE value, is not valid for a TIMESTAMP value and will be converted to 0 if assigned to such an object.

4. The value is saved in UTC format (it stores the number of milliseconds)

5. Time zone conversion: convert the current time zone when storing, and convert back to the current time zone when retrieving.

6. The default value is CURRENT_TIMESTAMP(), which is actually the current system time.

7. The database will automatically modify its value, so you do not need to specify the name of the timestamp field and the value of the timestamp field when inserting records. You only need to add a timestamp field when designing the table. After insertion, the value of the field will automatically become the current system time.

8. Whenever you modify a record in the table at any time in the future, the timestamp value of the corresponding record will be automatically updated to the current system time.

Conclusion: The timestamp type is suitable for recording the last modification time of data, because as long as you change the value of other fields in the record, the value of the timestamp field will be automatically updated.

Summarize

The above is all about analyzing the selection problem of storing time and date types in MySQL. Interested friends can refer to: MySQL in statement subquery efficiency optimization skills example, MYSQL subquery and nested query optimization example analysis, MySQL optimization using connection (join) instead of subquery, etc. If you have any questions, you can leave a message at any time and the editor will reply to you in time. I hope this helps you all.

You may also be interested in:
  • Explanation of the problem of selecting MySQL storage time type
  • How to choose the right MySQL datetime type to store your time
  • Solve the problem of inconsistent MySQL storage time
  • MySQL statement to get all dates or months in a specified time period (without setting stored procedures or adding tables)
  • Best Practices Guide for Storing Dates in MySQL

<<:  js implements table drag options

>>:  Detailed explanation of how to limit the update/delete range using the mysql parameter sql_safe_updates

Recommend

How to customize at and cron scheduled tasks in Linux

There are two types of scheduled tasks in Linux s...

How to solve the phantom read problem in MySQL

Table of contents Preface 1. What is phantom read...

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...

Discussion on image path issues in css (same package/different package)

In CSS files, sometimes you need to use background...

CSS--overflow:hidden in project examples

Here are some examples of how I use this property ...

Install nodejs and yarn and configure Taobao source process record

Table of contents 1. Download nodejs 2. Double-cl...

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is o...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

Tips and precautions for using MySQL index

1. The role of index In general application syste...

How to get the size of a Linux system directory using the du command

Anyone who has used the Linux system should know ...