All Projects → brick → Db

brick / Db

Licence: mit
Helper tools for interacting with databases

Labels

Projects that are alternatives of or similar to Db

Django Databrowse
Databrowse is a Django application that lets you browse your data.
Stars: ✭ 41 (-12.77%)
Mutual labels:  database
Couchdb Couch
Mirror of Apache CouchDB
Stars: ✭ 43 (-8.51%)
Mutual labels:  database
Exonum
An extensible open-source framework for creating private/permissioned blockchain applications
Stars: ✭ 1,037 (+2106.38%)
Mutual labels:  database
Android Debug Database
A library for debugging android databases and shared preferences - Make Debugging Great Again
Stars: ✭ 7,946 (+16806.38%)
Mutual labels:  database
Rethinkdb Lite
A RethinkDB-compatible database written in Crystal
Stars: ✭ 43 (-8.51%)
Mutual labels:  database
Nutriana
nutriana = nutrition analysis (MySQL and Oracle ports of the Canadian and USDA nutrient databases)
Stars: ✭ 44 (-6.38%)
Mutual labels:  database
Rxrealm
RxSwift extension for RealmSwift's types
Stars: ✭ 1,007 (+2042.55%)
Mutual labels:  database
Laravel Translatable
It's a Laravel database translations manager
Stars: ✭ 47 (+0%)
Mutual labels:  database
Dynamic Data Source Demo
基于事务的读写分离
Stars: ✭ 43 (-8.51%)
Mutual labels:  database
Neuralyzer
Neuralyzer is a library and a command line tool to anonymize databases (by updating existing data or populating a table with fake data)
Stars: ✭ 45 (-4.26%)
Mutual labels:  database
Nano
🍀 Fast, decentralized and git-trackable database.
Stars: ✭ 42 (-10.64%)
Mutual labels:  database
Active Record
Active Record database abstraction layer
Stars: ✭ 43 (-8.51%)
Mutual labels:  database
Cosyan
Transactional SQL based RDBMS with sophisticated multi table constraint logic.
Stars: ✭ 45 (-4.26%)
Mutual labels:  database
Avbook
AV 电影管理系统, avmoo , javbus , javlibrary 爬虫,线上 AV 影片图书馆,AV 磁力链接数据库,Japanese Adult Video Library,Adult Video Magnet Links - Japanese Adult Video Database
Stars: ✭ 8,133 (+17204.26%)
Mutual labels:  database
Docker Redis Cluster
Dockerfile for Redis Cluster (redis 3.0+)
Stars: ✭ 1,035 (+2102.13%)
Mutual labels:  database
Declarativesql
Attribute-based database access
Stars: ✭ 41 (-12.77%)
Mutual labels:  database
Laravel Tags
Add tags and taggable behaviour to your Laravel app
Stars: ✭ 1,026 (+2082.98%)
Mutual labels:  database
Bigbang
Android base project used by Xmartlabs team
Stars: ✭ 47 (+0%)
Mutual labels:  database
Indradb
A graph database written in rust
Stars: ✭ 1,035 (+2102.13%)
Mutual labels:  database
Traildb
TrailDB is an efficient tool for storing and querying series of events
Stars: ✭ 1,029 (+2089.36%)
Mutual labels:  database

Brick\Db

A collection of helper tools for interacting with databases.

Build Status Coverage Status Latest Stable Version License

Installation

This library is installable via Composer:

composer require brick/db

Requirements

This library requires PHP 7.1 or later.

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']);

$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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].