|
| 1 | +# Laravel MySQL Explains For Humans |
| 2 | + |
| 3 | +![License][icon-license] |
| 4 | +![PHP][icon-php] |
| 5 | +![Laravel][icon-laravel] |
| 6 | +[![Latest Version on Packagist][icon-version]][href-version] |
| 7 | +[![GitHub Unit Tests Action Status][icon-tests]][href-tests] |
| 8 | +[![GitHub Static Analysis Action Status][icon-staticanalysis]][href-staticanalysis] |
| 9 | +[![GitHub Code Style Action Status][icon-codestyle]][href-codestyle] |
| 10 | + |
| 11 | +MySQL Query optimization with the `EXPLAIN` command is unnecessarily complicated: The output contains a lot of cryptic information that is incomprehensible or entirely misleading. |
| 12 | + |
| 13 | +This Larvel package collects many query metrics that will be sent to [explainmysql.com](explainmysql.com) and transformed to be much easier to understand. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +You can install the package via composer: |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require tpetry/laravel-mysql-explain |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Query Builder |
| 26 | + |
| 27 | +Three new methods have been added to the query builder for very easy submission of query plans: |
| 28 | + |
| 29 | +| Type | Action | |
| 30 | +|------------------------|---------------------------------------------------------------------| |
| 31 | +| `explainForHumans` | returns URL to processed EXPLAIN output | |
| 32 | +| `dumpExplainForHumans` | dumps URL to processed EXPLAIN output and continue normal execution | |
| 33 | +| `ddExplainForHumans` | dumps URL to processed EXPLAIN output and stops execution | |
| 34 | + |
| 35 | + |
| 36 | +```php |
| 37 | +// $url will be e.g. https://explainmysql.com/explain/613cf8a0-d76c-4386-9680-5b6b537c3463 |
| 38 | +$url = Film::where('description', 'like', '%astronaut%') |
| 39 | + ->explainForHumans(); |
| 40 | + |
| 41 | +// URL to EXPLAIN will be printed to screen |
| 42 | +$users = Film::where('description', 'like', '%astronaut%') |
| 43 | + ->dumpExplainForHumans() |
| 44 | + ->get(); |
| 45 | + |
| 46 | +// URL to EXPLAIN will be printed to screen & execution is stopped |
| 47 | +$users = Film::where('description', 'like', '%astronaut%') |
| 48 | + ->ddExplainForHumans() |
| 49 | + ->get(); |
| 50 | +``` |
| 51 | + |
| 52 | +### Raw Queries |
| 53 | + |
| 54 | +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: |
| 55 | + |
| 56 | +```php |
| 57 | +use Tpetry\MysqlExplain\Facades\MysqlExplain; |
| 58 | + |
| 59 | +// $url will be e.g. https://explainmysql.com/explain/89eef861-e01b-4ab1-8fa4-4f7dd9d33ead |
| 60 | +$url = MysqlExplain::submitQuery( |
| 61 | + DB::connection('mysql'), |
| 62 | + 'SELECT * FROM actor WHERE first_name = ?', |
| 63 | + ['PENEL\'OPE'], |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +## Testing |
| 68 | + |
| 69 | +```bash |
| 70 | +composer test |
| 71 | +``` |
| 72 | + |
| 73 | +## Changelog |
| 74 | + |
| 75 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 76 | + |
| 77 | +## Credits |
| 78 | + |
| 79 | +- [Tobias Petry](https://github.com/tpetry) |
| 80 | +- [All Contributors](../../contributors) |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 85 | + |
| 86 | +[href-codestyle]: https://github.com/tpetry/laravel-mysql-explain/actions/workflows/code-style.yml |
| 87 | +[href-staticanalysis]: https://github.com/tpetry/laravel-mysql-explain/actions/workflows/static-analysis.yml |
| 88 | +[href-tests]: https://github.com/tpetry/laravel-mysql-explain/actions/workflows/unit-tests.yml |
| 89 | +[href-version]: https://packagist.org/packages/tpetry/laravel-mysql-explain |
| 90 | +[icon-laravel]: https://img.shields.io/badge/Laravel-6.*--10.*-blue |
| 91 | +[icon-license]: https://img.shields.io/github/license/tpetry/laravel-mysql-explain?color=blue&label=License |
| 92 | +[icon-codestyle]: https://img.shields.io/github/actions/workflow/status/tpetry/laravel-mysql-explain/code-style.yml?label=Code%20Style |
| 93 | +[icon-php]: https://img.shields.io/packagist/php-v/tpetry/laravel-mysql-explain?color=blue&label=PHP |
| 94 | +[icon-staticanalysis]: https://img.shields.io/github/actions/workflow/status/tpetry/laravel-mysql-explain/static-analysis.yml?label=Static%20Analysis |
| 95 | +[icon-tests]: https://img.shields.io/github/actions/workflow/status/tpetry/laravel-mysql-explain/unit-tests.yml?label=Tests |
| 96 | +[icon-version]: https://img.shields.io/packagist/v/tpetry/laravel-mysql-explain.svg?label=Packagist |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments