Substitute for MySQL's UUID Version 1 function?

后端 未结 2 1543
清酒与你
清酒与你 2021-01-02 01:21

Context

Web application, PHP 5, MySQL 5.0.91

The Problem

I recently switched from using an auto-incremented integer

2条回答
  •  甜味超标
    2021-01-02 02:00

    Your concern, that "most of the UUID is useless and is wasting space" is inherent to the size of the data type. You will never be able to have as many entries in your database, as the theoretical limit of 16 bytes allows.

    In fact, V1 UUID is more fit than V4 if you use the UUID just as a table ID - because it uses MAC-address and time stamp to prevent clashes. In V4 there is no such mechanism, although practically you don't need to worry too much about clashes either :) You should use V4 UUID instead of V1 if you need your UUID to be unpredictable.

    Also, note that composing for example 4x4 byte random values may not be the same as creating a 16 byte random value. As always with crypto and randomness: I would disadvise from implementing your own UUID::V4 routine.

    If installed on your machine, you can make use of the php-uuid package.

    An example code (which can be used in your application as is) can be found here: http://rommelsantor.com/clog/2012/02/23/generate-uuid-in-php/

    Use it like this:

    $uuid = uuid_create(1);
    

    Users that are are able to install packages on their webserver, can install the required package, like: (here for ubuntu)

    apt-get install php5-dev uuid-dev
    pecl install uuid
    

提交回复
热议问题