helbrary/db-performance
Composer 安装命令:
composer require helbrary/db-performance
包简介
Library for increase performance of db in Nette Database using multi-update and multi-insert.
README 文档
README
This library is useful for bulk import to database.
Install in composer.json
require: "helbrary/db-performance": "dev-master"
Multi-insert
Create instance
$multiInsert = new MultiInsert($this->context, 'table_name'); $multiInsert->onFailure[] = function(\Exception $e) { .... };
Third parameter in constructor is buffer limit (optionaly). Default value is 100. It means that if buffer contains 100 records and we wont add next record, it's build multi insert and send to database. Buffer is empty and to buffer is add new record.
Add record to buffer (function don't insert record to database yet)
$multiInsert->add(array( "firstname" => "John", "lastname" => "Smith", ));
If we add to buffer all records, it's necessary insert records to database from buffer.
$multiInsert->save();
Multi-insert Failed
In case throw exception in database, for example constraint violations, is multi-insert divide into single inserts. These single inserts are send to database individually (one record = one insert). When single insert throw exception, it's invoke callback $onFailure.
OnFailure callback
$multiInsert->onFailure[] = function(\Exception $e) { Debugger::log($e); };
Multi-update
Create instance
$multiUpdate = new MultiUpdate($context, 'page', 'title', 50); $multiUpdate->onFailure[] = function(\Exception $e) { Debugger::log($e); };
Add record to buffer (function don't update record in database yet)
$multiUpdate->add(1, 'updated column text');
If we add to buffer all records to update, it's necessary send update records to database from buffer
$multiUpdate->save();
Recommended buffer limit
Buffer limit defines how many SQL commands will be joined to one multi-command. Default value of buffer limit is 100. It's possible to change this value in constructor by variable $limit. If SQL commands will throw some type of Exception often, it's better set buffer smaller. If SQL commands will throw some type of Exception rarely it's better set buffer higher.
Testing - Insert 100 000 records (no SQL INSERT throw Exceptions)
- One by one record: 16 min 55 sec
for ($i = 0; $i < 100000; $i++) { $this->database->table('tableName')->insert(array( "title" => "title" . $i, "description" => "description" . $i, )); }
- One by one record in transaction: 4 min 50 sec
$this->database->beginTransaction(); for ($i = 0; $i < 100000; $i++) { $this->database->table('tableName')->insert(array( "title" => "title" . $i, "description" => "description" . $i, )); } $this->database->commit();
- Using Helbrary\DbPerformance\MultiInsert with (default) buffer size 100: 0 min 20 sec
$multiInsert = new MultiInsert($this->context, 'tableName', 100); for ($i = 0; $i < 100000; $i++) { $multiInsert->add(array( "title" => "title" . $i, "description" => "description" . $i, )); } $multiInsert->save();
-
Using Helbrary\DbPerformance\MultiInsert with buffer size 500: 0 min 14 sec
-
Using Helbrary\DbPerformance\MultiInsert with buffer size 1000: 0 min 12 sec
-
Using Helbrary\DbPerformance\MultiInsert with buffer size 10000: 0 min 11 sec
helbrary/db-performance 适用场景与选型建议
helbrary/db-performance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 127 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 helbrary/db-performance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 helbrary/db-performance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 127
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-05-02