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 implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Detailed steps for deploying Tomcat server based on IDEA

Table of contents Introduction Step 1 Step 2: Cre...

How to handle concurrent updates of MySQL data

Will UPDATE lock? Will the SQL statement be locke...

Detailed explanation of MySQL file storage

What is a file system We know that storage engine...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

Analyze the working principle of Tomcat

SpringBoot is like a giant python, slowly winding...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...

JavaScript to achieve a simple carousel effect

What is a carousel? Carousel: In a module or wind...

How to use CSS attribute value regular matching selector (tips)

There are three types of attribute value regular ...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...

Let's talk about Vue's mixin and inheritance in detail

Table of contents Preface Mixin Mixin Note (dupli...

js to achieve the effect of light switch

This article example shares the specific code of ...

html base url tag

Its function is to set a global style. Then your s...

Detailed use cases of MySql escape

MySQL escape Escape means the original semantics ...