irap/multi-query
Composer 安装命令:
composer require irap/multi-query
包简介
Wrapper object around mysqli to combine queries into one.
关键字:
README 文档
README
This is a package for simplifying the sending multiple PHP Mysqli queries in a single request and handling the responses. This often greatly improve performance by removing the round-trip-time, which is most noticeable when the database is on a remote host.
Example Usage
Basic Example
$db = new mysqli($host, $user, $password, $db_name); $queries = array( "DROP TABLE `table1`", "DROP TABLE `table2`", "DROP TABLE `table3`" ); $multiQuery = new iRAP\MultiQuery\MultiQuery($db, $queries);
Full Example
In the example below we run lots of different queries and use the index we get when we added the query in order to get it's result from the multiQuery object later. We also demonstrate how to check that nothing went wrong by checking the status of the object after having run it.
$connection = new mysqli('host', 'user', 'password', 'db_name'); $queries = array( 'SELECT * FROM `table1`', 'SHOW TABLES`', 'SELECT * FROM `table2`' ); $select1QueryIndex = 0; $showTablesQueryIndex = 1; $select2QueryIndex = 2; $multiQuery = new iRAP\MultiQuery\MultiQuery($connection, $queries); if ($multiQuery->hasErrors()) { $errors = $multiQuery->getErrors(); // do something with the errors array such as use them in an // exception message.... } else { $tablesResult = $multiQuery->getResult($showTablesQueryIndex); if ($tablesResult === FALSE) { throw new Exception("Failed to fetch tables"); } else { $tables = array(); while (($row = $tablesResult->fetch_array()) !== null) { $tables[] = $row[0]; } print "tables: " . implode(", ", $tables); } }
Merged Result Example
If you have partitioned your data using separate tables (e.g. the tables all have the same structure), then you may want to make use of the get_merged_result() method to just stitch all the query results into one
# // Example 2 - Fetch data from two tables that have exactly the same structure # // e.g. a case of partitioning data using table names like "dataset1", "dataset2" $queries = array( 'SELECT * FROM `table1`', 'SELECT * FROM `table2`' ); $multiQuery2 = new iRAP\MultiQuery\MultiQuery($connection, $queries); $mergedResult = $multiQuery2->getMergedResult(); print "merged result: " . print_r($mergedResult, true) . PHP_EOL;
Transactions
This package also has a class to help with making MySQL transactions. Below is an example of using this class:
$queries = array( 'DELETE FROM `myTable` WHERE id = ' . $id, 'INSERT INTO `myTable` SELECT * FROM `myTable2` WHERE id = ' . $id ); $transaction = new iRAP\MultiQuery\Transaction($mysqli, $queries); if (!$transaction->wasSuccessful()) { throw new Exception("Failed to reset the record in xyz"); }
The transaction will automatically detect and rollback if any of the queries within the transaction object fail. By default, if the transaction fails the object will not retry, but you can configure it to do so. Below is the same example, but this time we have set it to retry up to 5 times when the transaction is run, and to wait 3 seconds between each attempt. The default for the sleep period if you do not set it, is 1 second.
$queries = array( 'DELETE FROM `myTable` WHERE id = ' . $id, ... # more quries here. ); $transaction = new iRAP\MultiQuery\Transaction( $mysqli, $queries, $attempts=5, $sleepPeriod=3 ); ...
irap/multi-query 适用场景与选型建议
irap/multi-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.49k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mysql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 irap/multi-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 irap/multi-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 irap/multi-query 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
PHP module for MySql database
VV database abstraction layer with query builder and DB structure models
VV DB Driver over PDO extension
A framework for data models in PHP7.
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
统计信息
- 总下载量: 4.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: none
- 更新时间: 2015-06-13