phlib/db-helper

DB 辅助工具,用于补充 phlib/db

3.0.0 2024-04-05 16:28 UTC

This package is auto-updated.

Last update: 2024-09-05 17:27:09 UTC


README

Code Checks Codecov Latest Stable Version Total Downloads Licence

DB 辅助工具,用于补充 phlib/db

安装

composer require phlib/db-helper

使用

// Get an Adapter
$config = [
    'host' => 'localhost',
    'username' => 'myuser',
    'password' => 'mypassword',
    'dbname' => 'mydatabase'
];
$db = new \Phlib\Db\Adapter($config);

批量插入

将多行插入到表中。

这增加了添加大量行(例如数千行)的写入性能,超过了原生 PDO 使用的典型伪预处理语句。

$insertFields = [
    'product_id',
    'product_name',
    'product_qty'
];
$updateFields = [
    'product_name',
    'product_qty'
];
$bulkInsert = new BulkInsert($adapter, 'product', $insertFields, $updateFields);

// Many calls to add() will write to the DB in batches
$bulkInsert->add($singleProductData);

// One final manual call to write() to complete
$bulkInsert->write();

查询规划器

测试 SELECT 语句将查询的行数。

$queryPlanner = new QueryPlanner($adapter, $sqlSelect);
$queryPlanner->getNumberOfRowsInspected(); // eg. 46234

大数据集

运行一个预期会慢(例如 >5s)的 SELECT 语句,因为数据量较大。

禁用查询缓冲以减少首次行的时间,并避免消耗 PHP 的内存用于语句结果,以及增加 MySQL 的查询超时。

$bigResult = new BigResult($adapter);
$pdoStmt = $bigResult->query($sqlSelect, $bind);

可选地,使用查询规划器检查防止非常大的查询运行

$bigResult = new BigResult($adapter);
$queryRowLimit = 20000000;
$pdoStmt = $bigResult->query($sqlSelect, $bind, $queryRowLimit);

许可证

此软件包是免费软件:您可以在自由软件基金会发布的 GNU Lesser General Public License 的条款下重新分配它和/或修改它,无论是许可证的第 3 版,还是(根据您的选择)任何较新版本。

本程序的分发是希望它会有用,但没有提供任何保证;甚至没有关于其商誉或适用于特定目的的隐含保证。有关详细信息,请参阅 GNU Lesser General Public License。

您应已收到一份 GNU Lesser General Public License 的副本。如果没有,请参阅 https://gnu.ac.cn/licenses/