PHPackages                             marekpetras/yii2-merge-table - 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. marekpetras/yii2-merge-table

ActiveYii2-extension[Database &amp; ORM](/categories/database)

marekpetras/yii2-merge-table
============================

Merge engine table trait for Yii 2 Framework.

v1.0.5(8y ago)22.7k↓50%1MITPHP

Since Nov 2Pushed 8y ago1 watchersCompare

[ Source](https://github.com/marekpetras/yii2-merge-table)[ Packagist](https://packagist.org/packages/marekpetras/yii2-merge-table)[ Docs](https://github.com/marekpetras/yii2-merge-table)[ RSS](/packages/marekpetras-yii2-merge-table/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (0)

yii2-merge-table
================

[](#yii2-merge-table)

Yii2 Merge Table
================

[](#yii2-merge-table-1)

About
-----

[](#about)

This trait allows you to split up a large dataset into multiple, more manageable smaller datasets (MyISAM) using MySQL and the [MERGE engine](http://dev.mysql.com/doc/refman/5.7/en/merge-storage-engine.html)

The idea is to have create a model table which is always empty and the trait then manages all other datasets.

We had the issue of having loads of data that we usually needed to access only by parts, and only very rarely aggregated.

So lets say you have a lots of accounts in the database and you need to give access to your users only to their own accounts but you also need to give overall access to the manager/admin.

If you have milions and milions of rows in the database but only access bits, you always have a few options how to scale your data, you can either replicate, partiotionate, use primary keys/indexes etc.

Another option is to use more identical tables and query only those actually required. If you have a common denominator that you can use (account id, customer id, date ranges etc) and by which you can select appropriate tables its really easy to manage.

You end up with a few tables based on your denominators e.g.:

```
report              - this is the merge table
report_15432        - these are the MyIsam tables with actual data
report_15435
report_12344
...
report_12312
report_model        - this is the model table which is used to create the partial tables - this is the only one you actually have to create in your database yourself

```

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist marekpetras/yii2-merge-table "^1.0"

```

or add

```
"marekpetras/yii2-merge-table": "^1.0"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Create the model class with which you will work pretty much the same way as with any other model class, the only thing you need to add is the defaultTableName() static function, whatever you specify here, the merge table will be called this in your database, the trait will create it by itself.

```

```

You need to come up with some sort of logical way to split your data. It could be account id, client id, yearmonth, or anything that will allow you to work with partial data if you need to. I usually just load the data (lot of it - milions of rows) from file.

Before you start to insert data, make sure the desired table exists, and if not the trait will create it.

```
