Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scopeOrderByTranslation really slow #393

Open
yooplait opened this issue Mar 2, 2024 · 0 comments
Open

scopeOrderByTranslation really slow #393

yooplait opened this issue Mar 2, 2024 · 0 comments

Comments

@yooplait
Copy link

yooplait commented Mar 2, 2024

Hello,

When dealing with tables with a big number of translations (we're talking about around 1 million records), the query time is far too long.

Here are my tables:

Schema::create('herbs', function (Blueprint $table) {
    $table->id();
    $table->string('sciname')->nullable()->unique();
    $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
    $table->timestamps();
});

Schema::create('herb_translations', function (Blueprint $table) {
    $table->id();
    $table->string('locale')->index();
    $table->string('name')->index();
    $table->foreignId('herb_id')->constrained()->cascadeOnDelete();
    $table->unique(['herb_id', 'locale']);
    $table->unique(['herb_id', 'locale', 'name']);
});

The herbs table contains about 300,000 records and the translation table 1,000,000.

Imagine I want to get the first 10 herbs sorted by their name.

Herb::query()->orderByTranslation('name')->limit(10)->get()

Unfortunately, OrderByTranslation creates an issue here due to the ORDER BY clause and the LEFT JOIN (whereas with an INNER JOIN, it's significantly faster).

The INNER JOIN utilizes indexes, while the LEFT JOIN relies on file sorting (so bad performance).

Has anyone tested this method with large data sets?

Thank you in advance for your help,
Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants