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

[8.x] Passthru Eloquent\Query::explain function to Query\Builder:explain for the ability to use database-specific explain commands #40075

Merged
merged 1 commit into from Dec 17, 2021

Conversation

tpetry
Copy link
Contributor

@tpetry tpetry commented Dec 16, 2021

After a twitter discussion about laravel's ability to explain database queries and the insight that many optional features are missing from the explain output for PostgreSQL, i wanted to add them to tpetry/laravel-postgresql-enhanced.

I then discovered that the eloquent builder and the query builder are using the same trait for generating explain information. As the eloquent builder is already passing many database query specific functions to the query builder i extended it to the explain command.

By this change database connections can use database-specific explain commands when they are using a custom query builder. And there is no need to change the model to make use of it. The enhanced laravel postgresql driver will use it to include many more optional explain options.

@tpetry tpetry changed the title [8.x] passthru model explain query to query builder [8.x] Passthru Model::explain function to Builder:explain for the ability to use database-specific explain commands Dec 16, 2021
@tpetry tpetry changed the title [8.x] Passthru Model::explain function to Builder:explain for the ability to use database-specific explain commands [8.x] Passthru Eloquent\Query::explain function to Query\Builder:explain for the ability to use database-specific explain commands Dec 16, 2021
@tpetry
Copy link
Contributor Author

tpetry commented Dec 16, 2021

I have my implementation for a PostgreSQL-specific query plan finished. The difference is remarkable. The more complex a query is, the more information my implementation will provide compared to the one of Laravel.

Laravel Query-Plan:

DB::table('migrations')->where('batch', 1)->explain()->dd();

// Output:
//array:2 [
//  0 => {#882
//    +"QUERY PLAN": "Seq Scan on migrations  (cost=0.00..11.75 rows=1 width=524)"
//  }
//  1 => {#881
//    +"QUERY PLAN": "  Filter: (batch = 1)"
//  }
//]

My Query-Plan:

DB::table('migrations')->where('batch', 1)->explain()->dd();

// Output:
// array:1 [
//  0 => """
//    Seq Scan on public.migrations  (cost=0.00..11.75 rows=1 width=524)\n
//      Output: id, migration, batch\n
//      Filter: (migrations.batch = 1)\n
//    Settings: search_path = 'public'\n
//    Planning Time: 0.370 ms
//    """
//]

With the current Laravel implementation, the new query plan is activated as soon as my enhanced driver is installed. But there's currently no way doing this as well for Eloquent queries without adding a trait to every single model. With my PR the eloquent explain() method will simply forward to the query builder explain() method as the eloquent method is not doing anything different anyways. By this any custom database connection driver can implement very specific functionality.

@mstaack
Copy link
Contributor

mstaack commented Dec 16, 2021

👍🏼

@taylorotwell taylorotwell merged commit e8ccdac into laravel:8.x Dec 17, 2021
@tpetry tpetry deleted the explain-passthru branch December 18, 2021 08:57
tpetry added a commit to tpetry/laravel-postgresql-enhanced that referenced this pull request Dec 22, 2021
For enhanced explain output of Eloquent queries changes to laravel had been required. My pull request laravel/framework#40075 has been merged and released with Laravel 8.77. The version dependency has therefore been increased to make use of it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants