A fixed number (such as 8 digits) is randomly generated and saved to the database as the uid registered by the user. Each uid is unique but can be 12345678,12345677. One way is to randomly generate eight digits and then query the database for this data. If yes, it is generated again in the query. If no, it is written... generate a fixed number (such as eight digits) at random,
Store the uid registered as a user to the database,
Each uid is unique,
But it can be 12345678,12345677.
One way is to randomly generate 8 digits,
Then, query whether the database has this data,
If yes, it is generated again in the query,
Write data to the database if none exist,
This method is time-consuming.
Is there a better way,
Such as combining other random values and timestamps.
The solution idea or method can be implemented in php
Reply content:
Generate a fixed number (such as eight digits) at random,
Store the uid registered as a user to the database,
Each uid is unique,
But it can be 12345678,12345677.
One way is to randomly generate 8 digits,
Then, query whether the database has this data,
If yes, it is generated again in the query,
Write data to the database if none exist,
This method is time-consuming.
Is there a better way,
Such as combining other random values and timestamps.
The solution idea or method can be implemented in php
Generally, the database stores the timestamp, that is, the number of milliseconds of the current system time, divided by 1000, and obtains the number of seconds of the current time as the uid, because if you want to store the time in milliseconds, it takes up too long a database, and it can be used in seconds to ensure that the unique data is not repeated. If you want to generate uid not only numbers but also letters, you can convert the number of seconds to hexadecimal notation.
The uid registered by the user can be automatically increased in a certain range.
Assuming that the qps registered by the user is not that high, you can use the update + private complex data method to simulate a lock in mongodb. The lock protects the uid data of the last registered user, and each time the lock is obtained, add one, register, apply the uid + 1 protected by the lock, and release the lock.
Unique, unordered: md5 (uniqid ());
Sequence unique: auto-Increment
The timestamp has almost no chance of repetition.
// Generate a random decimal number from 0 to 1. public function randomFloat ($ min = 0, $ max = 1) {return $ min + mt_rand ()/mt_getrandmax () * ($ max-$ min );}
Why? Do not use primary key auto-increment?
Uid is better to use primary key auto-increment, and then use phpmysql_insert_id()
And so on. In this way, query operations are relatively small.
Create an auto-increment column. The starting value starts from 10000000.
I'm glad to answer this question for LZ.
First, what is constantly increasing and will not be repeated?
Time.
As LZ guessed, using time cut to convert to a number can generate numbers that are always increasing and not repeated.
In addition, you can view the registration order and registration time based on the time.
This is just a reference.
$ Now = date ("Y-m-d H: I: s"); // record the current time in the format of year, month, day, hour, minute, and second. $ Unix_stamp = strtotime ($ now); echo $ unix_stamp;/* The strtotime function is expected to contain a string in the American English date format, and try to parse it into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT) Here LZ will get a 10-digit number. */
LZ can use $ unix_stamp as a random uid.
(For example, if multiple users register simultaneously within one second), you can add the microtime () function after $ unix_stamp to convert it to a number.
To make this number more random to avoid duplicate uid issues.