Analysis of MySQL built-in functions uuid and uuid_short

Source: Internet
Author: User


Some people asked about the MySQL uuid function. A brief introduction. Usage

The value is different for each execution. Generation rule 1st 2 3 is time-related. Www.2cto.com
Time_low, time_mid, and time_high_and_version are converted into hexadecimal values, which correspond to the 1st 2 3 segments respectively. This time is the 100ns value from 1582-10-15 00:00:00. 00 to the current time. (In fact, the system can only get the precise us, and then multiply by 10 ). Therefore, if you execute it in a short time, it is possible that only the first value is changed. In fact, both values may change. Section 4th is the random number when you run select uuid () for the first time after starting MySQL. Each restart will change. The 5th segment is converted from the mac value, and the same machine generally has the same multi-instance. If the mac value cannot be obtained, it is a random value. Therefore, this value may be considered different for each execution. In addition, there is only a small probability of repetition between different instances. Uuid_short
Different from the fixed-length string returned by uuid, the returned value of uuid_short is an unsigned long type. The value of the first execution after MySQL is started is initialized through server_id <56 + server_start_time <24. Server_start_time is measured in seconds. Then, 1 is added for each execution. Because the global mutex lock is applied every time you add 1, the multi-thread security can be used as sequence, but the initial value is a little large. Sequence MySQL does not have sequence like Oracle. If it is not very precise, you can consider the uuid_short mentioned above. There are some shortcomings: www.2cto.com 1. The initial value is too large, cannot reset 2. There is a problem that the value first executed after each restart is not the value + 13 before the restart, and if the restart is completed within 1 s, there may be non-monotonic increments (although this possibility is minimal ). To meet the above requirements, you can consider using udf. Author Ding Lin. tb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.