#253·uuid

Most efficient type to use Uuids as identities in doctrine entities

Author: flaushiCreated Dec 19, 2018Updated May 1, 2020
Labelsquestion

Hi, I have switched to Uuids as primary keys for my doctrine project. All entities have such a field:

/**
     * @var UuidInterface
     *
     * @ORM\Id
     * @ORM\Column(type="uuid", unique=true)
     */
    protected $id;

    public function __construct() {
        $this->id = Uuid::uuid4();
    }

Attached you find an profiler screenshot. bildschirmfoto 2018-12-19 um 09 04 32

As one can see, almost 10% of the time are spent in the unserialize method of this lib. Would it maybe be more efficient to store as String? Like so:

/**
     * @var string
     *
     * @ORM\Id
     * @ORM\Column(type="string", length=64, unique=true)
     */
    protected $id;

    public function __construct() {
        $this->id = Uuid::uuid4()->toString();
    }

Please note, I want to keep my code and model compatible with Postgres and MySQL, which is the case currently. @ocramius