定制 brick/db 二次开发

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

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

brick/db

Composer 安装命令:

composer require brick/db

包简介

Database tools library

README 文档

README

A collection of helper tools for interacting with databases.

Build Status codecov Latest Stable Version Total Downloads License

Installation

This library is installable via Composer:

composer require brick/db

Requirements

This library requires PHP 8.1 or later.

For PHP 7.4 and 8.0 compatibility, you can use version 0.2. PHP 7.1, 7.2 & 7.3, you can use version 0.1. Note that these PHP versions are EOL and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.

Package overview

This package contains two helpers: BulkInserter and BulkDeleter. These classes, built on top of PDO, allow you to speed up database rows insertion & deletion by performing multiple operations per query, with a clean OO API.

BulkInserter

This class takes advantage of the extended insert / multirow syntax available in MySQL, PostgreSQL and SQLite.

It basically replaces the need to send a batch of queries:

INSERT INTO user (id, name, age) VALUES (1, 'Bob', 20);
INSERT INTO user (id, name, age) VALUES (2, 'John', 22);
INSERT INTO user (id, name, age) VALUES (3, 'Alice', 24);

with a single, faster query:

INSERT INTO user (id, name, age) VALUES (1, 'Bob', 20), (2, 'John', 22), (3, 'Alice', 24);

To use it, create a BulkInserter instance with:

  • your PDO connection object
  • the name of your table
  • the name of the columns to insert
  • the number of inserts to perform per query (optional, defaults to 100)

Example

use Brick\Db\Bulk\BulkInserter;

$pdo = new PDO(...);
$inserter = new BulkInserter($pdo, 'user', ['id', 'name', 'age'], 100);

$inserter->queue(1, 'Bob', 20);
$inserter->queue(2, 'John', 22);
$inserter->queue(3, 'Alice', 24);

$inserter->flush();

The queue() method does not do anything until either flush() is called, or the number of inserts per query is reached.

Note: queue() returns false when the insert has been queued only, and true when the number of inserts per query has been reached and the batch has therefore been flushed to the database. This can be useful to monitor the progress of the batch.

Do not forget to call flush() after all your inserts have been queued. Failure to do so would result in records not being inserted.

BulkDeleter

This class allows you to delete multiple records at a time.

It basically replaces the need for these queries:

DELETE FROM user WHERE id = 1;
DELETE FROM user WHERE id = 2;
DELETE FROM user WHERE id = 3;

with a single, faster query:

DELETE FROM user WHERE (id = 1) OR (id = 2) OR (id = 3);

The constructor parameters are the same as BulkInserter.

For obvious performance reasons, the list of columns used to identify a record should match the primary key or a unique index of the table.

Example

With a single column primary key / unique index:

use Brick\Db\Bulk\BulkDeleter;

$pdo = new PDO(...);
$deleter = new BulkDeleter($pdo, 'user', ['id']);

$deleter->queue(1);
$deleter->queue(2);
$deleter->queue(3);

$deleter->flush();

With a composite key:

use Brick\Db\Bulk\BulkDeleter;

$pdo = new PDO(...);
$deleter = new BulkDeleter($pdo, 'user_product', ['user_id', 'product_id]);

$deleter->queue(1, 123);
$deleter->queue(2, 456);
$deleter->queue(3, 789);

$deleter->flush();

Do not forget to call flush() after all your deletes have been queued. Failure to do so would result in records not being deleted.

Performance tips

To get the maximum performance out of this library, you should:

  • wrap your operations in a transaction
  • disable emulation of prepared statements (PDO::ATTR_EMULATE_PREPARES=false)

These two tips combined can get you up to 50% more throughput in terms of inserts per second. Sample code:

$pdo = new PDO(...);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$inserter = new BulkInserter($pdo, 'user', ['id', 'name', 'age']);
$pdo->beginTransaction();

$inserter->queue(...);
// more queue() calls...

$inserter->flush();
$pdo->commit();

The library could do this automatically, but doesn't for the following reasons:

  • your PDO object's configuration should not be modified by a third-party library
  • you should have full control over your transactions, when to start them and when to commit them

Respecting the limits

Be careful when raising the number of operations per query, as you might hit these limits:

You can tweak these settings if you have access to your server's configuration, however it's important to benchmark with different batch sizes, to determine the optimal size and see if increasing the server limits is worth the effort. In most cases, 100 inserts per query should give you at least 80% of the maximum throughput:

Extended inserts benchmark

See this article for a more in-depth analysis.

MySQL also has a limit of 65535 placeholders per statement, effectively limiting the number of operations per query to floor(65535 / number of columns). This does not apply if PDO emulates prepared statements.

brick/db 适用场景与选型建议

brick/db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 177.87k 次下载、GitHub Stars 达 68, 最近一次更新时间为 2017 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 brick/db 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 68
  • Watchers: 6
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-28