PHPackages                             brick/db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. brick/db

ActiveLibrary[Database &amp; ORM](/categories/database)

brick/db
========

Database tools library

0.3.0(1y ago)68167.3k↓10.8%12[1 issues](https://github.com/brick/db/issues)MITPHPPHP ^8.1CI passing

Since Nov 8Pushed 2mo ago6 watchersCompare

[ Source](https://github.com/brick/db)[ Packagist](https://packagist.org/packages/brick/db)[ GitHub Sponsors](https://github.com/BenMorel)[ RSS](/packages/brick-db/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (10)Used By (0)

Brick\\Db
=========

[](#brickdb)

[![](https://raw.githubusercontent.com/brick/brick/master/logo.png)](https://raw.githubusercontent.com/brick/brick/master/logo.png)

A collection of helper tools for interacting with databases.

[![Build Status](https://github.com/brick/db/workflows/CI/badge.svg)](https://github.com/brick/db/actions)[![codecov](https://camo.githubusercontent.com/d0a33f478e7510a697b6fcec0ae6e1f5c160fa6217ad868f95d46ae6e40333de/68747470733a2f2f636f6465636f762e696f2f6769746875622f627269636b2f64622f67726170682f62616467652e737667)](https://codecov.io/github/brick/db)[![Latest Stable Version](https://camo.githubusercontent.com/aa8211634ddf90eccffe85b804048859ef0e353265c9f545ad5351f9ae2807e9/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f64622f762f737461626c65)](https://packagist.org/packages/brick/db)[![Total Downloads](https://camo.githubusercontent.com/69ad35f664f008cfcca3ec7c54ff641ab46bdb4d20e7e68ed2363b618be82157/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f64622f646f776e6c6f616473)](https://packagist.org/packages/brick/db)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

Installation
------------

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require brick/db
```

Requirements
------------

[](#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 &amp; 7.3, you can use version `0.1`. Note that [these PHP versions are EOL](http://php.net/supported-versions.php) and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.

Package overview
----------------

[](#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 &amp; deletion by performing multiple operations per query, with a clean OO API.

### BulkInserter

[](#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

[](#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

[](#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

[](#example-1)

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

[](#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

[](#respecting-the-limits)

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

- PHP's [memory\_limit](http://php.net/manual/en/ini.core.php#ini.memory-limit)
- MySQL's [max\_allowed\_packet](https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html)

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](https://camo.githubusercontent.com/64f725411515dff8915435295270b684e4049160ce513653d12d34cdcbbef89b/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a6b5f5153317174674e352d557972446b6a5352675f772e706e67)](https://camo.githubusercontent.com/64f725411515dff8915435295270b684e4049160ce513653d12d34cdcbbef89b/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a6b5f5153317174674e352d557972446b6a5352675f772e706e67)

See [this article](https://medium.com/@benmorel/high-speed-inserts-with-mysql-9d3dcd76f723) 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.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance66

Regular maintenance activity

Popularity47

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~661 days

Total

5

Last Release

467d ago

PHP version history (4 changes)0.1.0PHP &gt;=5.6

0.1.2PHP &gt;=7.1

0.2.0PHP ^7.4 || ^8.0

0.3.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (70 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")[![proyb6](https://avatars.githubusercontent.com/u/16257425?v=4)](https://github.com/proyb6 "proyb6 (1 commits)")

---

Tags

bulk-insertsdatabasephpbrickpdodbBulkInserterBulkDeleter

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brick-db/health.svg)

```
[![Health](https://phpackages.com/badges/brick-db/health.svg)](https://phpackages.com/packages/brick-db)
```

###  Alternatives

[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[envms/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

925511.7k13](/packages/envms-fluentpdo)[lichtner/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921274.8k6](/packages/lichtner-fluentpdo)[fpdo/fluentpdo

FluentPDO is a quick and light PHP library for rapid query building. It features a smart join builder, which automatically creates table joins.

921244.9k7](/packages/fpdo-fluentpdo)[atlas/query

Object-oriented query builders and performers for MySQL, Postgres, SQLite, and SQLServer.

41249.0k7](/packages/atlas-query)[simplon/mysql

Simplon MySQL Library

6988.1k7](/packages/simplon-mysql)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
