Article Directory
1. The principle of mysql order by
Internal sorting
Refers to the sorting process in which the records to be sorted are stored in the computer's random access memory, that is, all sorting is performed in the server memory.
Thinking: Is the sorting process of mysql all carried out in memory?
External sort
Refers to the sorting process in which the number of records to be sorted is so large that the memory cannot hold all the records at one time, and access to the external storage is still required during the sorting process.
Unstable sort
For mysql
The sorting action may be done in memory, or it may need to use external sorting, depending on the memory and parameters required for sorting
sort_buffer_size. sort_buffer_size is the size of the memory (sort_buffer) opened up by MySQL for sorting. If the amount of data to be sorted is less than sort_buffer_size, the sorting is done in memory. However, if the amount of sorting data is too large to be stored inside, you have to use disk temporary files to assist sorting.
mysql sort_buffer sort uses quick sort, which is an unstable sort. When the time is the same, the data position of each sort cannot be guaranteed to be the same.
Second, the problems that occur in order of time
Before the
data is updated, the id 623 is
sorted to the end after the data is updated. Although there is no logical problem with the returned results according to the time sorting rules, for the user, the order of messages changes after the message list is updated.
Here we need to further in-depth mysql source code to understand the details of the entire sorting process, why the sorting order will change after the update operation
After analyzing the problem, let’s take a look at several ideas to solve the problem.
Add sort field
Sorting according to time and id, this sorting will ensure the stability of the results, but for mysql, sorting is a relatively resource-consuming operation, the operation that can be done by a sorting field should not increase the sorting field as much as possible.
Exact publishTime
The datetime field in mysql8.0 defaults to 0 digits after the decimal, then the intelligence is accurate to the second, and adding 6 digits after the decimal is accurate to the microsecond.
Use now(6) to generate microsecond time data when writing data tables in batches.
If it is the time field assigned to the entity in java, you need to get the time in microseconds.
MYSQL get microsecond time
now() can only return the time accurate to the second
Get accurate to the millisecond can use now (3)
accurate to microseconds now (6)
mysql8.0 only supports microseconds
SELECT
NOW( ) AS A,
NOW( 3 ) AS b,
NOW( 6 ) AS c;
Get microsecond time in java
Get system time in milliseconds:
public Date() {
this(System.currentTimeMillis());
}
Generally, the system time can be effectively distinguished if it is accurate to milliseconds, but in the case of high concurrency systems, it needs to be further accurate to microseconds.
1 millisecond = 1000 microseconds = 1000 nanoseconds
Get microseconds:
Date.from(Instant.now().truncatedTo(ChronoUnit.MICROS))
Change ChronoUnit.MICROS can choose the time accuracy of the return
The classes in java.time are parsed as nanoseconds, which is more refined than the old date and time classes and milliseconds used by Joda-Time.
Clock implementation
In jdk11
Instant
Objects can provide time accurate to nanoseconds and are thread-safe.
able to pass
System.out.println(ZonedDateTime.now(ZoneId.of("America/Montreal")));
System.out.println(Clock.system(ZoneId.of("Asia/Shanghai")).instant());
System.out.println(Date.from(Clock.system(ZoneId.of("Asia/Shanghai")).instant()));
Get precise time.
The time obtained by mysql and java is based on the server time. If you encounter time inconsistencies, first ensure that the unix server time is accurate.