PHPackages                             fab2s/souuid - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fab2s/souuid

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fab2s/souuid
============

Simple Ordered Uuid Generator in PHP

1.0.2(3y ago)13573.2k↓18.8%11MITPHPPHP ^7.1|^8.0

Since Feb 9Pushed 3y ago2 watchersCompare

[ Source](https://github.com/fab2s/SoUuid)[ Packagist](https://packagist.org/packages/fab2s/souuid)[ RSS](/packages/fab2s-souuid/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (5)Versions (9)Used By (1)

SoUuid : Simple Ordered UUID
============================

[](#souuid--simple-ordered-uuid)

[![Build Status](https://github.com/fab2s/SoUuid/actions/workflows/ci.yml/badge.svg)](https://github.com/fab2s/SoUuid/actions/workflows/ci.yml/badge.svg) [![QA](https://github.com/fab2s/SoUuid/actions/workflows/qa.yml/badge.svg)](https://github.com/fab2s/SoUuid/actions/workflows/qa.yml) [![Total Downloads](https://camo.githubusercontent.com/0ec63abfd09215244de83b26d87baa558b7c18edb3b37e44668e33fdbdc93d58/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f736f757569642f646f776e6c6f616473)](https://packagist.org/packages/fab2s/souuid) [![Monthly Downloads](https://camo.githubusercontent.com/66d0ea6da3852e63b96834978b27f682d7c8fbf9cdbd7b791b0e9bb229ef3f40/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f736f757569642f642f6d6f6e74686c79)](https://packagist.org/packages/fab2s/souuid) [![Latest Stable Version](https://camo.githubusercontent.com/855810ea95ba22ce3124937cfca66d05debdfa6cd7e10cc004f461c1be367bef/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f736f757569642f762f737461626c65)](https://packagist.org/packages/fab2s/souuid) [![Maintainability](https://camo.githubusercontent.com/67da852ef51f8310d2a37207368fb57e067962ec4d69c38e1cde222ff740a08a/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f31346235386639356434366430643264343761372f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/fab2s/SoUuid/maintainability) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/254ae5d7c2122e4da86cbb66c6c657623cdd12fe83d93e48bda4ce68e20f5509/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66616232732f536f557569642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/fab2s/SoUuid/?branch=master) [![PRs Welcome](https://camo.githubusercontent.com/7d9ed3c8f22eceb1711573169b1390cc0b1194467340dc815205060c162b5309/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c6174)](http://makeapullrequest.com) [![License](https://camo.githubusercontent.com/d5ad739f3b1338aa277a5d6d7a96e4a812c5788ce15370c1ff3ce00176ff3b55/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f6e6f64616c666c6f772f6c6963656e7365)](https://packagist.org/packages/fab2s/souuid)

`SoUuid` is a working proposal to generate ordered UUIDs in a simple and efficient way using PHP.

While UUIDs already have [well defined standards](https://tools.ietf.org/html/rfc4122), they suffer from quite bad performance when used as a primary key in DBMS. The reason are well know and goes down to :

- UUID are 36 characters long, which can grow the index size significantly, especially with InnoDB and alike where every secondary index would also contain the primary key.
- Insert comes at a terrible cost since UUID PKs are pretty random thus highly scattered across the index.

You can find more information and benchmarks in [Percona Database Performance Blog](https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/) or [on MariaDb's KB](https://mariadb.com/kb/en/library/guiduuid-performance/). Both includes solutions to handle the matter at the database level. While they focus on Mysql, the problem is similar with other DBMS : lack of UUID order and UUID size are costly, with ordering being critical for massive inserts on a PK.

The order problem has been addressed in various ways in PHP, most being implemented as some extra feature of an RFC compliant implementation. It seemed to me that something simpler and more adequate with PHP could be of some use. Because to start with, PHP is limited to the micro-second, and RFC implementation have to artificially meet the "official" 100ns interval, which actually weakens the uniqueness of the UUID, as the same RFC random bits are now protecting a ten times wider interval against collisions.

It's of course ok to trade some performance and even some usability to stay in standards, but all together it also seems weird to be stuck with such an inefficient format that does not even match the level of guarantees defined by the original RFC. Then, using the Gregorian calendar as origin of time just brings nothing to the table. Using the Epoch time seems more reasonable and more convenient to work with. Unless you where planning to simulate pre-70's Mac address (did they even exist before that), it's just a waist of data space.

While it may have made more sens at some point to bind UUIDs to some physical Mac Address for eternity, it is quite less obvious now as hardware has become a commodity and that Mac addresses are subject to change frequently with no other meaning than "a deployment was made". So it seems a bit awkward to have to deal with Mac Address format just to store a more meaningful id, especially with PHP where Mac address is a rather distant information. It's also kind of a limitation to be bound to any particular format for something that should belong to the application space, like a worker or job id.

So all together it felt like there was a room for some simple improvements that hopefully will help out in real life situations.

Installation
------------

[](#installation)

`SoUuid` can be installed using composer:

```
composer require "fab2s/souuid"

```

If you want to specifically install the php &gt;=7.1.0 version, use:

```
composer require "fab2s/souuid" ^1

```

If you want to specifically install the php 5.6/7.0 version, use:

```
composer require "fab2s/souuid" ^0

```

There are no changes other than further typing from 0.x to 1.x

In practice
-----------

[](#in-practice)

### Without identifier

[](#without-identifier)

```
$uuid = SoUuid::generate();
$uuid->getBytes(); // 16 bytes binary string b"\x05d¦U
