承接 tpetry/php-mysql-explain 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tpetry/php-mysql-explain

最新稳定版本:1.0.0

Composer 安装命令:

composer require tpetry/php-mysql-explain

包简介

Visual MySQL EXPLAIN plans for PHP

README 文档

README

License PHP Latest Version on Packagist GitHub Unit Tests Action Status GitHub Static Analysis 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 PHP package collects many query metrics that will be sent to mysqlexplain.com and transformed to be much easier to understand.

Installation

You can install the package via composer:

composer require tpetry/php-mysql-explain

Usage

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

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 function:

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

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_* 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

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

Finally, the $metrics object containing all the information must be sent to the mysqlexplain.com API:

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 HTTP library (composer require guzzlehttp/guzzle) was used - which you probably have already installed. But you can also choose from many other implementations.

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

统计信息

  • 总下载量: 44.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-26