Re: [Q] string to int hash function for small range

Поиск
Список
Период
Сортировка
От hubert depesz lubaczewski
Тема Re: [Q] string to int hash function for small range
Дата
Msg-id 20090308042635.GA22509@depesz.com
обсуждение исходный текст
Ответ на [Q] string to int hash function for small range  ("V S P" <toreason@fastmail.fm>)
Список pgsql-general
On Sat, Mar 07, 2009 at 09:33:33PM -0500, V S P wrote:
> I would like to have a function
> given a user id varchar(64) to get a hash number between 0 and 255.
>
> I first tried md5 but the number it returns is very big and cannot
> be converted to an INT
>
> there is likely a way to simply add ascii values of the userId together
> to get a small integer, but wanted to know if there are any other
> 'built-in' ways
>
> this is to get the database id based on user id.

this is bad idea, as there will be collisions. better use sequential
ids.

but if you *have to*:

select hashtext('username') % 128 + 127;

depesz;

--
Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

В списке pgsql-general по дате отправления:

Предыдущее
От: John R Pierce
Дата:
Сообщение: Re: [Q] string to int hash function for small range
Следующее
От: Martin Gainty
Дата:
Сообщение: Re: [Q] string to int hash function for small range