Giter Club home page Giter Club logo

Comments (11)

staudenmeir avatar staudenmeir commented on June 2, 2024 1

SQLite is extremely fast, so I don't think performance should be an issue.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 2, 2024

Hi @antoniopaisfernandes,
What version of SQLite are you using?

from eloquent-json-relations.

antoniopaisfernandes avatar antoniopaisfernandes commented on June 2, 2024

Hi,
I'm using 3.39.2

image

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 2, 2024

How are you using the package in your app? What relationships did you define and test?

from eloquent-json-relations.

antoniopaisfernandes avatar antoniopaisfernandes commented on June 2, 2024

I have a Workshop model like this:

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;

class Workshop extends Model
{
    use HasFactory,
        HasJsonRelationships;

    public $incrementing = false;

    protected $guarded= [];

    protected $casts = [
        'team_leader_ids' => 'json',
    ];

    public function teamLeaders(): BelongsToJson
    {
        return $this->belongsToJson(User::class, 'team_leader_ids');
    }
//...
}

And a related model with a scope:

class WorkshopWarranty extends Model
{
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new WorkshopWarrantyScope());
    }

    public function workshop(): BelongsTo
    {
        return $this->belongsTo(Workshop::class);
    }
//...
}


class WorkshopWarrantyScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        $builder->whereHas('workshop', function (Builder $query) {
            $query->whereHas('teamLeaders', fn (Builder $query) => $query->where('id', auth()->id()));
        });
    }
}

Running in MariaDB gets the following SQL:

SELECT
    *
FROM
    `workshop_warranties`
WHERE
    EXISTS (
        SELECT
            *
        FROM
            `workshops`
        WHERE
            `workshop_warranties`.`workshop_id` = `workshops`.`id`
            AND EXISTS (
                SELECT
                    *
                FROM
                    `users`
                WHERE
                    json_contains(
                        `workshops`.`team_leader_ids`,
                        json_array(`users`.`id`)
                    )
                    AND `id` = 835
                    AND `users`.`deleted_at` IS NULL
            )
    )

Running in SQLite

SELECT
    *
FROM
    "workshop_warranties"
WHERE
    EXISTS (
        SELECT
            *
        FROM
            "workshops"
        WHERE
            "workshop_warranties"."workshop_id" = "workshops"."id"
            AND EXISTS (
                SELECT
                    *
                FROM
                    "users"
                WHERE
                    EXISTS (
                        SELECT
                            1
                        FROM
                            json_each("workshops"."team_leader_ids")
                        WHERE
                            "json_each"."value" IS json_array(`users`.`id`)
                    )
                    AND "id" = 835
                    AND "users"."deleted_at" IS NULL
            )
    )

Running that SQL in HeidiSQL:
image

Best

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 2, 2024

Thanks, I'll look into it. SQLite didn't support these relationships when I created the package, but that was five years ago.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 2, 2024

I've released a new version with SQLite support for relationships like yours.

This update has only been possible since Laravel added support for whereJsonContains() on SQLite two weeks ago.
Out of curiosity: Were you waiting for that or was it a coincidence that you startet developing right after the Laravel update?

from eloquent-json-relations.

antoniopaisfernandes avatar antoniopaisfernandes commented on June 2, 2024

Wow!! That was fast!

It was a serendipity :)
Developed last week
image

Your package added 3 months ago:
image

Thank you!

from eloquent-json-relations.

ren0v avatar ren0v commented on June 2, 2024

@staudenmeir is there any query performance trick for SQLite as you already have documented for MySQL / PostgreSQL?
https://github.com/staudenmeir/eloquent-json-relations?tab=readme-ov-file#query-performance

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 2, 2024

@ren0v Not that I know of. Are you having performance issues?

from eloquent-json-relations.

ren0v avatar ren0v commented on June 2, 2024

from eloquent-json-relations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.