marekpetras/yii2-merge-table
Composer 安装命令:
composer require marekpetras/yii2-merge-table
包简介
Merge engine table trait for Yii 2 Framework.
README 文档
README
Yii2 Merge Table
About
This trait allows you to split up a large dataset into multiple, more manageable smaller datasets (MyISAM) using MySQL and the MERGE engine
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
The preferred way to install this extension is through composer.
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
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.
<?php use yii\db\ActiveRecord; use marekpetras\mergetable\MergeTableTrait; use marekpetras\mergetable\MergeTableInterface; class Report extends ActiveRecord implements MergeTableInterface { use MergeTableTrait; /** * Get the default table name, substitutes the tableName function if not a merge table model * @return string default table name */ public static function defaultTableName() { return 'report'; } } ?>
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.
<?php $id = 15432; $tableName = Report::ensureExists($id); $sql = sprintf(" LOAD DATA INFILE '%s' REPLACE INTO TABLE `%s` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' ", $file, $tableName); $rows = Yii::$app->db->createCommand($sql)->execute(); ?>
Then if you want to access this data, you have to set which table you want to access (if you do not specify anything, the merge table will be queried)
<?php $id = 15432; Report::setTableName(Report::tableNameMerge($id)); $query = Report::find(); /* SELECT * FROM report_15432; */
There are a few ways to access the data, for example you might want to aggregate data from a few of those. Just send an array of the ids to the model first. The trait will create a temporary merge table which you can then query. It will be dropped at the
<?php $ids = [15432,12344]; Report::setTableName(Report::tableNameMerge($id)); $query = Report::find(); /* CREATE TEMPORARY TABLE unique_temp_name LIKE report_model; ALTER TABLE unqiue_temp_name ENGINE=MERGE, UNION=(report_15432,report_12344) INSERT_METHOD=NO; SELECT * FROM unqiue_temp_name; */
You will only ever query the whole merge table if you choose to do so, and if you do set a table name.
<?php $query = Report::find(); /* SELECT * FROM report; */
You can use the query as any other, in your data providers etc.
marekpetras/yii2-merge-table 适用场景与选型建议
marekpetras/yii2-merge-table 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.87k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「data」 「mysql」 「Active Record」 「extension」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marekpetras/yii2-merge-table 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marekpetras/yii2-merge-table 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marekpetras/yii2-merge-table 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
Adds the EDTF data type to Wikibase
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 2.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-02