Sample Code for generating UUID unique sequence in PHP

Source: Internet
Author: User

 

/**  * Generates an UUID  *  * @author     Anis uddin Ahmad   * @param      string  an optional prefix  * @return     string  the formatted uuid  */  function uuid($prefix = '')  {    $chars = md5(uniqid(mt_rand(), true));    $uuid  = substr($chars,0,8) . '-';    $uuid .= substr($chars,8,4) . '-';    $uuid .= substr($chars,12,4) . '-';    $uuid .= substr($chars,16,4) . '-';    $uuid .= substr($chars,20,12);    return $prefix . $uuid;  }   //Example of using the function -//Using without prefix.echo uuid(); //Returns like ‘1225c695-cfb8-4ebb-aaaa-80da344e8352′    //Using with prefixecho uuid(‘urn:uuid:’);//Returns like ‘urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344e8352′

**************************************** **************************************** *****

You can also directly use SQL statements to generate the following:
Insert UUID directly in the insert statement as the primary key usage (simple ):
Insert into price (name, UUID, price, bid) values ('feifei _ test', UUID (), 32, 3 );

**************************************** **************************************** ****

PHP generates UUID to indicate the unique code

What is UUID? What is guid? How does PHP generate UUID? (Using UUID + MYSQL to search related content on Google requires more results than UUID + PHP)

Here is a detailed description of UUID.

Http://mlxia.javaeye.com/blog/279059

The following sections are reproduced:

The only database that I am still familiar with is mysql. Probably mysql users, more than 9% will use autoincrement ID as the primary key. This is understandable, because the auto-increment ID of MySQL is very efficient and convenient to use. So what do the remaining 1% users use as the primary key? It may be your own keygenerator, or the uuid we will talk about below.

It is said that in the Oracle circle, if the user uses auto-incrementing ID as the primary key is to be despised, the most natural choice of the primary key is UUID. I am not familiar with Oracle, and do not promise whether these conclusions are correct.

What is UUID first? In short, UUID refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. In the uuid algorithm, information such as the nic mac address, IP address, host name, and process ID may be used to ensure its independence.

If your MySQL version is not old, type select UUID (); the output is UUID, as shown below:

Mysql> select UUID ();
+ -------------------------------------- +
| UUID () |
+ -------------------------------------- +
| 54b4c01f-dce0-102a-a4e0-462c07a00c5e |
+ -------------------------------------- +

Now you should have a more intuitive understanding of UUID. Let's take a look at the advantages and disadvantages of UUID.

Advantages:

It can ensure independence, and the program can be migrated between different databases without affecting the effect.
Ensure that the generated IDs are not only table independent, but also database independent. This is especially important when you want to split the database.

Disadvantages:

Compared to the int type, it takes more space to store a uuid.
After UUID is used, the URL is lengthy and unfriendly.

The following is my opinion on the shortcomings of the uuid mentioned above. I am not very concerned about this shortcoming, but the most valuable one is the hard disk. I can skip this shortcoming. As for the use of UUID, the URL seems unfriendly. I think this is the inertial Thinking Caused by your int complex. In fact, compared with the int type, UUID is the most natural primary key choice, note: I use the natural adjective here. I want to understand what you mean. In addition, in many cases, the URL itself does not need to be friendly. For example, for an e-commerce website, according to the INT-friendly URL, her order URL is probably in the following form:/order. PHP/ID/123, I want to note that this is very friendly, but some are very friendly, friendly, or even insecure. For example, I placed an order in the morning and found
URL is/order. PHP/ID/1000. The URL for the next order in the evening is/order. PHP/ID/2000, then I can estimate that the number of orders for this website on a day is about 1000, and I can even estimate the sales volume of this website, these data are often important commercial secrets. There is no such concern when using UUID.

Efficiency?

If none of the above-mentioned UUID's so-called shortcomings are true, then whether to use UUID as the primary key is the only problem is efficiency. It is said that there are dedicated UUID types in PostgreSQL and other databases. In such a database, using UUID as the primary key has no efficiency problems. Unfortunately, such fields are not found in MySQL, if you want to save the uuid in MySQL as the primary key, it is generally simulated using char (36). Because it is not a native UUID type, how can the efficiency of the primary key be tested? In addition, the efficiency of UUID primary keys is also closely related to the uuid algorithm implementation.

I originally wanted to insert 1000000 pieces of data to my computer and test it. Unfortunately, the hard drive lights keep on, so I am worried that it will crash, although the hard disk is not worth the money, but I have all the important data on it. Once it is broken, the loss will be huge. So, we have to give up the test.

I do not know the efficiency of using UUID (stored in char (36) as the primary key in MySQL. Sorry -_-!!!

How to generate UUID? The following method does not seem to generate UUID, because MD5 may actually have repeated values (refer to the http://www.phpx.com/happy/archiver/tid-56636.html), and the use of random can not avoid repeated. therefore, you should directly use the uuid function in MySQL to generate

De> function UUID ($ prefix = '')

{

$ Chars = MD5 (uniqid (mt_rand (), true ));

$ UUID = substr ($ chars, 0, 8 ).'-';

$ UUID. = substr ($ chars, 8, 4 ).'-';

$ UUID. = substr ($ chars, 12, 4 ).'-';

$ UUID. = substr ($ chars, 16,4 ).'-';

$ UUID. = substr ($ chars, 20, 12 );

Return $ prefix. $ UUID;

} De>

Insert UUID in MySQL using the uuid () function of MySQL

Insert into table (ID,...) values (UUID (),...)

Of course, you can also use select UUID () to obtain a uuid value before inserting it.

Question:

It may be less efficient than using an integer as the primary key,
Another problem is that the URL may be too long, such
Normally, category. php? Cid = 2 but now it may be category. php? UUID = a93f16c5-9634-102c-824f-3ea0651c5b77
Can it be changed to an integer as the primary key?

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.