How PHP generates a unique numeric ID, note that it is numeric, do not string the
Reply content:
How PHP generates a unique numeric ID, note that it is numeric, do not string the
Landlord, you are a big problem.
Twitter,weibo and so on have been specially made a number device to solve this problem.
The thing about Twitter, called Snowflake, has already been pointed out upstairs. This thing altogether 64bit, the former 41bit is with the subtle timestamp, 10bit is the machine guard says the server ID, the last 12bit is the SEQ sequence accumulation counter.
Weibo's approach is similar to Twitter, where IDs are divided into n segments, each capturing a certain data source, and finally generating a highly unique ID.
Flickr is a gadget called Ticketserver that is implemented using pure MySQL.
CREATE TABLE Ticket (
ID bigint unsigned NOT NULL auto_increment primary key,
Stag char (1) NOT null default ' ',
Unique key stag
( stag
)
) Engine=myisam;
Insert a record before using replace to get the ID.
Replace into ticket (stag) VALUES (' a ');
Select last_insert_id (); ====>id
Then, the UUID is a good choice, but the sequence of UUID generation is too long, and MySQL itself does not have native support (I pretended to use MySQL), but the landlord can try to use the UUID as binary (16) to save the effect will be better. But the landlord said it is a pure number, it is useless.
There is another one that I hear in my flock. is to generate a large lump of usable unique ID in advance, it is possible to take it directly and then del off, which is also feasible, because the person who said the scheme is a great God. Why the early generation, I think the most important one is to prevent high concurrency cases, two people get the same ID; Pre-generated words can be queued for a single build, ensuring uniqueness. Of course, I don't know what their generation strategy is. Landlord can refer to the practice of Twitter.
In addition, MongoDB comes with the Objectid is also a highly unique sequence, the landlord can use MongoDB generated directly to use, but also to ensure high concurrency, two or more people get the same ID, although the probability is very low. But the landlord said it is a pure figure, it is not a vain.
If the landlord to play a stand-alone, it will not be too tangled with the unique ID problem, the primary key from the increase can be a unique ID. Too long-term problems can now be considered, but not too rigidly. If the landlord is distributed, it is necessary to have this thing.
base_convert(uniqid(), 16, 10);
OK
redis incr
Recommend a unique digital ID generator that I'm using myself
https://github.com/sschiau/Particle.php
Particle
language:php
64bits int time Based ID Generator
PHP implementation of Twitter snowflake ID Generator
Uncoordinated
For the availability within and across data centers, machines generating IDs should not having to coordinate with each othe R.
Solution
System Clock Dependency
You should the use of NTP to keep your system clock accurate.
How to use it
Generate particle ID
Change-Const EPOCH in particle class to today epoch Time w/miliseconds (digits)
$machineID = 1; // Machine ID (aka Server ID no) Particle::generateParticle($machineID);
Time from particle ID (w/milisecond precision)
$particleID = '4611692470816737853'; Particle::timeFromParticle($particleID);
Landlord should explain the unique ID use and usage scenarios, database self-increment ID, as well as PHP is weak type, numeric and string numeric types can be universal, conversion to each other.
No way, unless you record a generated number, or even a random function, the timestamp generation number will appear duplicated.
Twitter Global Unique ID generation Service: Snowflake (Https://github.com/twitter/snowflake)
Reference:
Http://www.oschina.net/p/snowflake
Http://www.cppblog.com/tx7do/archive/2014/06/10/207248.html
Uniqid is really good, why don ' t you use it? Just need convert to number, first answer is cool.
Sorry I can ' t type Chinese, because it ' s disabled on chrome, I think it's a bug of segmentfault.com
Should first explain the scene, single or how long the unique, single request unique or multiple requests unique
Timestamp plus a combination of random numbers, but on the safe side, it's best to validate after the build or the ID pool you've logged.
@magicgod I think can @SegmentFault to solve your question
Not quite understand the meaning of the main question, what is the use of the scene? In the database, the direct setting of the ID from the increase it is OK? If it's an array, it's straight $i++.
You can use time stamps
You can generate a UUID or GUID that guarantees that there is no repetition in the same time and space, and this open source class has a lot, and can be written by itself, according to the current timestamp + random factor. The random factor can include the MAC address of the current PC and the number of random strings and lines of the current code, etc., PHP built-in functions:
Com_create_guid can complete your function. If you are not satisfied, there are many open source implementations on GitHub.
Supplement: 1 should be fixed length 2 encounter letters turn into numbers or randomly cause numbers to be represented
There is a way to use the current timestamp, a pure number
Put me on a pseudo code, I used the ID generator
$id = substr(strtotime(date("Y-m-d", time())), 0, 2) . substr(strrev(microtime()), 0, 2) . substr(mt_rand(), 0, 5) . substr(rand(), 0, 2);$sql = "SELECT id FROM table WHERE id=" . $id . " LIMIT 1";$data mysql_query($sql);
The idea is probably time stamp plus random number piece, concrete realization of course landlord according to demand and then make changes
Although it is possible to derive a unique value based on calculations, it is not guaranteed that a unique key should be added to the library and that unique values are ensured through queries
Generate a digital pool beforehand, and then take it out of the pool when you need it.
If that number is released, you can just put it back in the pool.
In that case, the random thing will be solved.