A
A
Alex Plotnikov2018-05-26 21:46:58
Hashing
Alex Plotnikov, 2018-05-26 21:46:58

How unique is the id generated from the md5 hash of the string?

Theoretical question: you need to generate a unique id, based on the title of the news. Without hesitation, they washed down such a bike, but wondered, with millions of articles with unique titles, will the id generated in a similar way remain unique?

$string = md5('Some unique string');
echo $string.'<br>';
$string= preg_replace('~[^0-9]+~','',$string); 
echo (int) $string.'<br>'; 

//result
//eb939f7a375f5520b090e49396e8393e
//9223372036854775807

so it's impossible to get a unique id, what options are there? it is necessary in order to check if there is a similar title before adding an article to the database, quickly search by id, long by like % title% ...

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Danil Sapegin, 2018-05-26
@TrueDevs

IMHO, do as you wrote
There is a separate varchar 32 field for the hash in the database, there is MD5 from the title and you put an index in the database on this field
The search will be very fast

A
Alexander Aksentiev, 2018-05-26
@Sanasol

There is hardly a chance to catch a collision by accident in millions. ( https://stackoverflow.com/a/288519)
If you don't strip anything from the hash, of course, why are you removing letters?
ID != only numbers
In general, there is no sense in the undertaking, then take uniqid() or mt_rand().

D
Denis, 2018-05-27
@sidni

Maybe there are special libraries for getting a unique identifier like
https://github.com/ramsey/uuid

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question