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] Add explain() to Query\Builder and Eloquent\Builder #34969

Merged
merged 4 commits into from
Oct 26, 2020

Conversation

hivokas
Copy link
Contributor

@hivokas hivokas commented Oct 26, 2020

explain() method allows you to receive the explanation from the builder (both Query and Eloquent).

Before you would typically call toSql(), copy the result, open TablePlus, prepend the query with EXPLAIN , replace all ?, and check the result.

Now you can just call explain() to return the explanation or explain()->dd() to die & dump the explanation.

>>> Webhook::where('event', 'users.registered')->explain()
=> Illuminate\Support\Collection {#15414
     all: [
       {#15407
         +"id": 1,
         +"select_type": "SIMPLE",
         +"table": "webhooks",
         +"partitions": null,
         +"type": "ref",
         +"possible_keys": "webhooks_event_index",
         +"key": "webhooks_event_index",
         +"key_len": "1022",
         +"ref": "const",
         +"rows": 1,
         +"filtered": 10.0,
         +"Extra": "Using where",
       },
     ],
   }
>>> DB::table('webhooks')->where('event', 'users.registered')->explain()
=> Illuminate\Support\Collection {#5350
     all: [
       {#5448
         +"id": 1,
         +"select_type": "SIMPLE",
         +"table": "webhooks",
         +"partitions": null,
         +"type": "ref",
         +"possible_keys": "webhooks_event_index",
         +"key": "webhooks_event_index",
         +"key_len": "1022",
         +"ref": "const",
         +"rows": 1,
         +"filtered": 100.0,
         +"Extra": null,
       },
     ],
   }
>>> User::where('name', 'Illia Sakovich')->explain()
=> Illuminate\Support\Collection {#5344
     all: [
       {#15407
         +"id": 1,
         +"select_type": "SIMPLE",
         +"table": "users",
         +"partitions": null,
         +"type": "ALL",
         +"possible_keys": null,
         +"key": null,
         +"key_len": null,
         +"ref": null,
         +"rows": 9,
         +"filtered": 11.11111164093,
         +"Extra": "Using where",
       },
     ],
   }
>>> DB::table('users')->where('name', 'Illia Sakovich')->explain()
=> Illuminate\Support\Collection {#5348
     all: [
       {#5342
         +"id": 1,
         +"select_type": "SIMPLE",
         +"table": "users",
         +"partitions": null,
         +"type": "ALL",
         +"possible_keys": null,
         +"key": null,
         +"key_len": null,
         +"ref": null,
         +"rows": 9,
         +"filtered": 11.11111164093,
         +"Extra": "Using where",
       },
     ],
   }

@taylorotwell taylorotwell merged commit c393ca2 into laravel:8.x Oct 26, 2020
@deleugpn
Copy link
Contributor

Woah, this is another level awesomeness. I hated replacing all those ? for large queries

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.

3 participants