定制 tpetry/php-mysql-explain 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tpetry/php-mysql-explain

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.

tpetry/php-mysql-explain 适用场景与选型建议

tpetry/php-mysql-explain 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67.3k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mysql」 「explain」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tpetry/php-mysql-explain 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tpetry/php-mysql-explain 我们能提供哪些服务?
定制开发 / 二次开发

基于 tpetry/php-mysql-explain 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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