tpetry / laravel-mysql-explain by tpetry

Get MySQL EXPLAIN plans that are understandable for humans
38,427
123
1
Package Data
Maintainer Username: tpetry
Maintainer Contact: github@tpetry.me (Tobias Petry)
Package Create Date: 2023-06-10
Package Last Update: 2024-03-13
Home Page: https://mysqlexplain.com
Language: PHP
License: MIT
Last Refreshed: 2024-04-27 03:16:30
Package Statistics
Total Downloads: 38,427
Monthly Downloads: 3,764
Daily Downloads: 211
Total Stars: 123
Total Watchers: 1
Total Forks: 4
Total Open Issues: 0

Laravel MySQL Explains For Humans

License PHP Laravel Latest Version on Packagist GitHub Unit Tests Action Status GitHub Static Analysis Action Status GitHub Code Style Action Status

MySQL Query optimization with the EXPLAIN command is unnecessarily complicated: The output contains a lot of cryptic information that is incomprehensible or entirely misleading.

This Larvel package collects many query metrics that will be sent to explainmysql.com and transformed to be much easier to understand.

Installation

You can install the package via composer:

composer require tpetry/laravel-mysql-explain

Usage

Query Builder

Three new methods have been added to the query builder for very easy submission of query plans:

| Type | Action | |------------------------|---------------------------------------------------------------------| | explainForHumans | returns URL to processed EXPLAIN output | | dumpExplainForHumans | dumps URL to processed EXPLAIN output and continue normal execution | | ddExplainForHumans | dumps URL to processed EXPLAIN output and stops execution |

// $url will be e.g. https://explainmysql.com/e/C0Omak70mLEXfok1a7Oo1n
$url = Film::where('description', 'like', '%astronaut%')
    ->explainForHumans();

// URL to EXPLAIN will be printed to screen
$users = Film::where('description', 'like', '%astronaut%')
    ->dumpExplainForHumans()
    ->get();

// URL to EXPLAIN will be printed to screen & execution is stopped
$users = Film::where('description', 'like', '%astronaut%')
    ->ddExplainForHumans()
    ->get();

Raw Queries

In some cases you are executing raw SQL queries and don't use the query builder. You can use the MysqlExplain facade to get the EXPLAIN url for them:

use Tpetry\MysqlExplain\Facades\MysqlExplain;

// $url will be e.g. https://explainmysql.com/e/H1pfKQ7FH3HnH87dS64Wk1
$url = MysqlExplain::submitQuery(
    DB::connection('mysql'),
    'SELECT * FROM actor WHERE first_name = ?',
    ['PENEL\'OPE'],
);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

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