PHPackages                             tpetry/php-mysql-explain - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. tpetry/php-mysql-explain

ActiveLibrary[Database &amp; ORM](/categories/database)

tpetry/php-mysql-explain
========================

Visual MySQL EXPLAIN plans for PHP

1.0.0(1y ago)651.5k↓11.5%1MITPHPPHP &gt;=7.4

Since Sep 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tpetry/php-mysql-explain)[ Packagist](https://packagist.org/packages/tpetry/php-mysql-explain)[ Docs](https://github.com/tpetry/php-mysql-explain)[ RSS](/packages/tpetry-php-mysql-explain/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (10)Versions (2)Used By (1)

MySQL Visual Explains with PHP
==============================

[](#mysql-visual-explains-with-php)

[![License](https://camo.githubusercontent.com/7bc49c52a5aab392b9758587bc5ab675ae032878e6db4773a4caa1aaed1101a2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7470657472792f7068702d6d7973716c2d6578706c61696e3f636f6c6f723d626c7565266c6162656c3d4c6963656e7365)](https://camo.githubusercontent.com/7bc49c52a5aab392b9758587bc5ab675ae032878e6db4773a4caa1aaed1101a2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7470657472792f7068702d6d7973716c2d6578706c61696e3f636f6c6f723d626c7565266c6162656c3d4c6963656e7365)[![PHP](https://camo.githubusercontent.com/4177913505f727e842a674128504d2e61d2255913894d461eaad890be391f325/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7470657472792f7068702d6d7973716c2d6578706c61696e3f636f6c6f723d626c7565266c6162656c3d504850)](https://camo.githubusercontent.com/4177913505f727e842a674128504d2e61d2255913894d461eaad890be391f325/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7470657472792f7068702d6d7973716c2d6578706c61696e3f636f6c6f723d626c7565266c6162656c3d504850)[![Latest Version on Packagist](https://camo.githubusercontent.com/93ee616e4152f959bde57fcd2562eb05591c02bcd32d9c7cfd6119d86bc95a18/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7470657472792f7068702d6d7973716c2d6578706c61696e2e7376673f6c6162656c3d5061636b6167697374)](https://packagist.org/packages/tpetry/php-mysql-explain)[![GitHub Unit Tests Action Status](https://camo.githubusercontent.com/e082208ad64c2f71da80b5635a6922177ddac77502b31bd5b734d1c115ab62cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7470657472792f7068702d6d7973716c2d6578706c61696e2f706870756e69742e796d6c3f6c6162656c3d5465737473)](https://github.com/tpetry/php-mysql-explain/actions/workflows/phpunit.yml)[![GitHub Static Analysis Action Status](https://camo.githubusercontent.com/5de81590afed16c960cf64ea14c63398f69b8d98ff9b2e69c61dc274f0143f5d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7470657472792f7068702d6d7973716c2d6578706c61696e2f7068707374616e2e796d6c3f6c6162656c3d537461746963253230416e616c79736973)](https://github.com/tpetry/php-mysql-explain/actions/workflows/phpstan.yml)

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 PHP package collects many query metrics that will be sent to [mysqlexplain.com](https://mysqlexplain.com) and transformed to be much easier to understand.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require tpetry/php-mysql-explain
```

Usage
-----

[](#usage)

### 1. Query Definition

[](#1-query-definition)

The query you want to analyze must first be defined with all the parameters used and its database connection. Included are implementations for PHP's mysqli and PDO drivers. However, you can also build framework-specific ones by implementing the `QueryInterface`.

Note

The minimum required PHP version is 7.4 but the examples use the named arguments syntax of PHP 8 for easier reading.

#### mysqli Driver

[](#mysqli-driver)

In its most simple form, you only provide the mysqli connection and the query to execute:

```
use Tpetry\PhpMysqlExplain\Queries\MysqliQuery;

$mysqli = new mysqli('127.0.0.1', 'root', 'root', 'github');

$query = new MysqliQuery(
  connection: $mysqli,
  sql: 'SELECT * FROM issues',
);
```

You can also provide variables that are bound to the prepared statement:

```
$query = new MysqliQuery(
  connection: $mysqli,
  sql: 'SELECT * FROM issues WHERE type = ? AND num > ?',
  params: ['finished', 85],
);
```

Please be aware that by default all passed variables are interpreted as strings. However, you can pass a string as last parameter following the characteristics of the `$types` parameter from mysqli's [bind\_param](https://www.php.net/manual/en/mysqli-stmt.bind-param.php) function:

```
$query = new MysqliQuery(
  connection: $mysqli,
  sql: 'SELECT * FROM issues WHERE type = ? AND num > ?',
  params: ['finished', 85],
  types: 'si',
);
```

#### PDO Driver

[](#pdo-driver)

```
use Tpetry\PhpMysqlExplain\Queries\PdoQuery;

$pdo = new PDO('mysql:host=127.0.0.1;dbname=github', 'root', 'root');

$query = new PdoQuery(
  connection: $pdo,
  sql: 'SELECT * FROM issues',
);
```

You can also provide variables that are bound to the prepared statement with positional or named parameters:

```
$query = new PdoQuery(
  connection: $pdo,
  sql: 'SELECT * FROM issues WHERE type = ? AND num > ?',
  params: ['finished', 85],
);

$query = new PdoQuery(
  connection: $pdo,
  sql: 'SELECT * FROM issues WHERE type = :type AND num > :num',
  params: ['type' => 'finished', 'num' => 85],
);
```

Please be aware that by default all passed variables are interpreted as strings. However, you can pass an array as last parameter with [PDO::PARAM\_\*](https://www.php.net/manual/en/pdo.constants.php) types:

```
$query = new PdoQuery(
  connection: $pdo,
  sql: 'SELECT * FROM issues WHERE type = ? AND num > ?',
  params: ['finished', 85],
  types: [PDO::PARAM_STR, PDO::PARAM_INT],
);

$query = new PdoQuery(
  connection: $pdo,
  sql: 'SELECT * FROM issues WHERE type = :type AND num > :num',
  params: ['type' => 'finished', 'num' => 85],
  types: ['type' => PDO::PARAM_STR, 'num' => PDO::PARAM_INT],
);
```

### 2. Metric Collection

[](#2-metric-collection)

Now, the query profiling information must be collected for the previously configured query:

```
use Tpetry\PhpMysqlExplain\Metrics\Collector;

$metrics = (new Collector())->execute($query);
```

### 3. API Call

[](#3-api-call)

Finally, the `$metrics` object containing all the information must be sent to the [mysqlexplain.com API](https://api.mysqlexplain.com):

```
use GuzzleHttp\Client as GuzzleClient;
use Tpetry\PhpMysqlExplain\Api\Client;

$client = new Client(new GuzzleClient());
$response = $client->submit($metrics);

var_dump($response->getUrl());
```

Important

This package has no dependency on any specific HTTP client library to avoid conflicts with actively used versions within your project. Therefore, you must pass an object of any http library implementing `psr/http-client`. In this example, the popular [Guzzle](https://docs.guzzlephp.org/en/stable/index.html) HTTP library (`composer require guzzlehttp/guzzle`) was used - which you probably have already installed. But you can also choose from [many other implementations](https://packagist.org/providers/psr/http-client-implementation).

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [tpetry](https://github.com/tpetry)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

599d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccc334e20491a79e2d81037803a4c96fe45dc2b0f721c631ab23e390242383b0?d=identicon)[tpetry](/maintainers/tpetry)

---

Top Contributors

[![tpetry](https://avatars.githubusercontent.com/u/315686?v=4)](https://github.com/tpetry "tpetry (2 commits)")

---

Tags

mysqlexplain

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tpetry-php-mysql-explain/health.svg)

```
[![Health](https://phpackages.com/badges/tpetry-php-mysql-explain/health.svg)](https://phpackages.com/packages/tpetry-php-mysql-explain)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[mevdschee/php-crud-api

Single file PHP script that adds a REST API to a SQL database.

3.7k63.8k9](/packages/mevdschee-php-crud-api)[tpetry/laravel-mysql-explain

Get Visual MySQL EXPLAIN for Laravel.

264154.2k](/packages/tpetry-laravel-mysql-explain)[cytopia/mysqldump-secure

Secure mysqldump script with encryption, compression, logging, blacklisting and Nagios monitoring integration

1474.7k1](/packages/cytopia-mysqldump-secure)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
