All Projects → spatie → Laravel Binary Uuid

spatie / Laravel Binary Uuid

Licence: mit
Optimised binary UUIDs in Laravel

Projects that are alternatives of or similar to Laravel Binary Uuid

Stacker
Stacker - The environment for local web development, ready for use.
Stars: ✭ 356 (-31.93%)
Mutual labels:  mysql, laravel
Docker Laravel
🐳 Build a simple laravel development environment with docker-compose.
Stars: ✭ 415 (-20.65%)
Mutual labels:  mysql, laravel
Enlightn
Your performance & security consultant, an artisan command away.
Stars: ✭ 378 (-27.72%)
Mutual labels:  laravel, performance
Laravel S
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.
Stars: ✭ 3,479 (+565.2%)
Mutual labels:  laravel, performance
Vagrant Php Dev Box
PHP 7 vagrant development box with nginx, php-fpm, MySQL, Symfony, Laravel, ... on Ubuntu 16.04
Stars: ✭ 473 (-9.56%)
Mutual labels:  mysql, laravel
Jianzhi V2
基于laravel5.5跟Vue2前后端分离的兼职平台
Stars: ✭ 327 (-37.48%)
Mutual labels:  mysql, laravel
Laravel Model Cleanup
Clean up unneeded records
Stars: ✭ 388 (-25.81%)
Mutual labels:  laravel, performance
Laravel Angular Cms
CMS built on Laravel, AngularJS and Material Design
Stars: ✭ 272 (-47.99%)
Mutual labels:  mysql, laravel
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+4027.53%)
Mutual labels:  laravel, performance
Web Performance Monitoring System
A complete performance monitoring system.
Stars: ✭ 436 (-16.63%)
Mutual labels:  mysql, performance
Reading
整理阅读过的干货文章, 帖子
Stars: ✭ 318 (-39.2%)
Mutual labels:  mysql, laravel
Laravel Pjax
A pjax middleware for Laravel
Stars: ✭ 487 (-6.88%)
Mutual labels:  laravel, performance
Laravel Varnish
Making Varnish and Laravel play nice together
Stars: ✭ 291 (-44.36%)
Mutual labels:  laravel, performance
Gitamin
An open-source, self-hosted git repository management system. QQ群:656868
Stars: ✭ 350 (-33.08%)
Mutual labels:  mysql, laravel
Laravel Vue
运用laravel5.4 + vue2.0 + elementui
Stars: ✭ 276 (-47.23%)
Mutual labels:  mysql, laravel
Laravel Eloquent Uuid
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
Stars: ✭ 388 (-25.81%)
Mutual labels:  laravel, uuid
Blog
Hi, I am CrazyCodes, and here are all my articles
Stars: ✭ 212 (-59.46%)
Mutual labels:  mysql, laravel
Daza Backend
[DEPRECATED]
Stars: ✭ 244 (-53.35%)
Mutual labels:  mysql, laravel
Performance
⏱ PHP performance tool analyser your script on time, memory usage and db query. Support Laravel and Composer for web, web console and command line interfaces.
Stars: ✭ 429 (-17.97%)
Mutual labels:  laravel, performance
Maghead
The fastest pure PHP database framework with a powerful static code generator, supports horizontal scale up, designed for PHP7
Stars: ✭ 483 (-7.65%)
Mutual labels:  mysql, performance

THIS PACKAGE IS NOT MAINTAINED ANYMORE

Alternatives: https://github.com/michaeldyrynda/laravel-efficient-uuid & https://github.com/michaeldyrynda/laravel-model-uuid

Using optimised binary UUIDs in Laravel

Latest Version on Packagist Build Status Code coverage Quality Score StyleCI Total Downloads

Using a regular uuid as a primary key is guaranteed to be slow.

This package solves the performance problem by storing slightly tweaked binary versions of the uuid. You can read more about the storing mechanism here: http://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/.

The package can generate optimized versions of the uuid. It also provides handy model scopes to easily retrieve models that use binary uuids.

Want to test the perfomance improvements on your system? No problem, we've included benchmarks.

The package currently only supports MySQL and SQLite.

Installation

You can install the package via Composer:

composer require spatie/laravel-binary-uuid

Usage

To let a model make use of optimised UUIDs, you must add a uuid field as the primary field in the table.

Schema::create('table_name', function (Blueprint $table) {
    $table->uuid('uuid');
    $table->primary('uuid');
});

To get your model to work with the encoded UUID (i.e. to use uuid as a primary key), you must let your model use the Spatie\BinaryUuid\HasBinaryUuid trait.

use Illuminate\Database\Eloquent\Model;
use Spatie\BinaryUuid\HasBinaryUuid;

class TestModel extends Model
{
    use HasBinaryUuid;
}

If don't like the primary key named uuid you can manually specify the getKeyName method. Don't forget set $incrementing to false.

use Illuminate\Database\Eloquent\Model;
use Spatie\BinaryUuid\HasBinaryUuid;

class TestModel extends Model
{
    use HasBinaryUuid;

    public $incrementing = false;
    
    public function getKeyName()
    {
        return 'custom_uuid';
    }
}

If you try converting your model to JSON with binary attributes, you will see errors. By declaring your binary attributes in $uuidAttributes on your model, you will tell the package to cast those UUID's to text whenever they are converted to array. Also, this adds a dynamic accessor for each of the uuid attributes.

use Illuminate\Database\Eloquent\Model;
use Spatie\BinaryUuid\HasBinaryUuid;

class TestModel extends Model
{
    use HasBinaryUuid;
    
    /**
     * The suffix for the uuid text attribute 
     * by default this is '_text'
     * 
     * @var
     */
    protected $uuidSuffix = '_str';
    
    /**
     * The binary UUID attributes that should be converted to text.
     *
     * @var array
     */
    protected $uuids = [
        'country_uuid' // foreign or related key
    ];
}

In your JSON you will see uuid and country_uuid in their textual representation. If you're also making use of composite primary keys, the above works well enough too. Just include your keys in the $uuids array or override the getKeyName() method on your model and return your composite primary keys as an array of keys. You can also customize the UUID text attribute suffix name. In the code above, instead of '_text' it's '_str'.

The $uuids array in your model defines fields that will be converted to uuid strings when retrieved and converted to binary when written to the database. You do not need to define these fields in the $casts array in your model.

A note on the uuid blueprint method

Laravel currently does not allow adding new blueprint methods which can be used out of the box. Because of this, we decided to override the uuid behaviour which will create a BINARY column instead of a CHAR(36) column.

There are some cases in which Laravel's generated code will also use uuid, but does not support our binary implementation. An example are database notifications. To make those work, you'll have to change the migration of those notifications to use CHAR(36).

// $table->uuid('id')->primary();

$table->char('id', 36)->primary();

Creating a model

The UUID of a model will automatically be generated upon save.

$model = MyModel::create();

dump($model->uuid); // b"\x11þ╩ÓB#(ªë\x1FîàÉ\x1EÝ." 

Getting a human-readable UUID

UUIDs are only stored as binary in the database. You can however use a textual version for eg. URL generation.

$model = MyModel::create();

dump($model->uuid_text); // "6dae40fa-cae0-11e7-80b6-8c85901eed2e" 

If you want to set a specific UUID before creating a model, that's also possible.

It's unlikely though that you'd ever want to do this.

$model = new MyModel();

$model->uuid_text = $uuid;

$model->save();

Querying the model

The most optimal way to query the database:

$uuid = 'ff8683dc-cadd-11e7-9547-8c85901eed2e'; // UUID from eg. the URL.

$model = MyModel::withUuid($uuid)->first();

The withUuid scope will automatically encode the UUID string to query the database. The manual approach would be something like this.

$model = MyModel::where('uuid', MyModel::encodeUuid($uuid))->first();

You can also query for multiple UUIDs using the withUuid scope.

$models = MyModel::withUuid([
    'ff8683dc-cadd-11e7-9547-8c85901eed2e',
    'ff8683ab-cadd-11e7-9547-8c85900eed2t',
])->get();

Note: Version 1.3.0 added simplified syntax for finding data using a uuid string.

$uuid = 'ff8683dc-cadd-11e7-9547-8c85901eed2e'; // UUID from eg. the URL.

$model = MyModel::find($uuid);  

$model = MyModel::findOrFail($uuid);

Version 1.3.0 query for multiple UUIDs.

$uuids = [
    'ff8683dc-cadd-11e7-9547-8c85901eed2e',
    'ff8683ab-cadd-11e7-9547-8c85900eed2t',
];

$model = MyModel::findMany($uuids);

Querying relations

You can also use the withUuid scope to query relation fields by specifying a field to query.

$models = MyModel::withUuid('ff8683dc-cadd-11e7-9547-8c85901eed2e', 'relation_field')->get();

$models = MyModel::withUuid([
    'ff8683dc-cadd-11e7-9547-8c85901eed2e',
    'ff8683ab-cadd-11e7-9547-8c85900eed2t',
], 'relation_field')->get();

Running the benchmarks

The package contains benchmarks that prove storing uuids in a tweaked binary form is really more performant.

Before running the tests you should set up a MySQL database and specify the connection configuration in phpunit.xml.dist.

To run the tests issue this command.

phpunit -d memory_limit=-1 --testsuite=benchmarks

Running the benchmarks can take several minutes. You'll have time for several cups of coffee!

While the test are running average results are outputted in the terminal. After the tests are complete you'll find individual query stats as CSV files in the test folder.

You may use this data to further investigate the performance of UUIDs in your local machine.

Here are some results for the benchmarks running on our machine.

A comparison of the normal ID, binary UUID and optimised UUID approach. Optimised UUIDs outperform all other on larger datasets.

Comparing different methods

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].