brokenice / laravel-mysql-partition
MySQL Partition 扩展程序,适用于 Laravel。
Requires
- php: >=7.1
- ext-json: *
- ext-mbstring: *
- ext-pdo: *
- illuminate/database: ^5.2|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- doctrine/dbal: ^2.5|^3.5
- laravel/browser-kit-testing: ^2.0|^6.4
- laravel/laravel: ^5.2|^6.0|^7.0|^8.0|^9.0|^10.0
- mockery/mockery: ~1.3.0|^1.4.4
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ~4.8|~5.7|^9.5.10
README
Laravel-mysql-partition 是一个实用的 Laravel 扩展包,可以轻松处理 MySQL 分区。请检查您 MySQL 版本的文档。分区需要 MySQL 版本 >= 5.1.0
★★ 请为我们点赞,或者在 ☕ 给我买杯咖啡 ★★
由 Luca Becchetti 创建
安装
使用 composer 添加包
$ composer require brokenice/laravel-mysql-partition
对于 Laravel 版本低于 5.5 或未使用自动发现,请在 config/app.php 中注册服务提供者
'providers' => [ /* * Package Service Providers... */ Brokenice\LaravelMysqlPartition\PartitionServiceProvider::class, ],
快速入门
创建迁移
从命令行
php artisan make:migration create_partitioned_table
然后通过添加此包提供的分区模式之一来编辑您刚刚创建的迁移;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Brokenice\LaravelMysqlPartition\Models\Partition; use Brokenice\LaravelMysqlPartition\Schema\Schema; class CreatePartitionedTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('partitioned', static function (Blueprint $table) { $table->bigInteger('id'); $table->string('name'); $table->date('date'); $table->timestamps(); $table->primary(['id','date']); }); // Force autoincrement of one field in composite primary key Schema::forceAutoIncrement('partitioned', 'id'); // Make partition by LIST Schema::partitionByList('partitioned', 'id', [ new Partition('server_east', Partition::LIST_TYPE, [1,43,65,12,56,73]), new Partition('server_west', Partition::LIST_TYPE, [534,6422,196,956,22]) ] ); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('partitioned'); } }
运行迁移
php artisan migrate
分区支持
包支持以下分区方法
支持的 MySQL 分区类型
- RANGE
- LIST
- HASH
- KEY
支持的特殊分区类型
- YEARS
- YEARS AND MONTH
- MONTH (即将推出)
- DAYS (即将推出)
按 RANGE 分区
此类分区根据列值落在给定范围内的行分配给分区。
Schema::partitionByRange('partitioned', 'YEAR(date)', [
new Partition('anno2000', Partition::RANGE_TYPE, 2000),
new Partition('anno2001', Partition::RANGE_TYPE, 2001),
new Partition('anno2002', Partition::RANGE_TYPE, 2002),
new Partition('anno2003', Partition::RANGE_TYPE, 2003),
]);
注意:使用上述代码,您将无法插入不属于任何范围的记录,例如:2010,为此,您必须指定创建未来值的分区,您可以这样做,通过将参数 **includeFuturePartition** 设置为 true,如下例所示
Schema::partitionByRange('partitioned', 'YEAR(date)', [
new Partition('anno2000', Partition::RANGE_TYPE, 2000),
new Partition('anno2001', Partition::RANGE_TYPE, 2001),
new Partition('anno2002', Partition::RANGE_TYPE, 2002),
new Partition('anno2003', Partition::RANGE_TYPE, 2003),
], true);
按 LIST 分区
类似于按 RANGE 分区,但分区是根据匹配一组离散值的列来选择的。
Schema::partitionByList('partitioned', 'id',
[
new Partition('server_east', Partition::LIST_TYPE, [1,43,65,12,56,73]),
new Partition('server_west', Partition::LIST_TYPE, [534,6422,196,956,22])
]
);
按 HASH 分区
在此类型分区中,分区是根据用户定义的表达式返回的值来选择的,该表达式操作于要插入表中的行中的列值。函数可以是 MySQL 中有效的任何表达式,产生非负整数值。也提供了此类型的扩展,LINEAR HASH。
Schema::partitionByHash('partitioned', 'YEAR(date)', 10);
按 KEY 分区
此类分区类似于按 HASH 分区,区别在于只提供了要评估的一个或多个列,MySQL 服务器提供自己的哈希函数。这些列可以包含除了整数以外的值,因为 MySQL 提供的哈希函数保证了无论列数据类型如何,结果都是整数。
Schema::partitionByKey('partitioned', 10);
按 YEARS 分区
此类型分区允许您根据指定的年份范围对表进行分区。
Schema::partitionByYears('partitioned', 'date', 2000, 2010);
您可以省略范围的结束年份,当前年份将被使用
Schema::partitionByYears('partitioned', 'date', 2000);
按 YEARS AND MONTHS 分区
此类型分区允许您根据指定的年份范围对表进行分区,并将每年进一步按月份分区。
Schema::partitionByYearsAndMonths('test_part', 'date', 2019);
您可以省略范围的结束年份,当前年份将被使用
复合主键
要分区表,列必须是索引,如果您想使用不同于 id 的列,您必须更改迁移文件中的此行
$table->bigIncrements('id');
到以下,创建一个复合主键
$table->bigInteger('id');
$table->primary(['id','date']);
注意:使用上述代码,您将丢失 id 字段的自动递增,如果需要,您可以在运行分区之前使用此代码强制执行
Schema::forceAutoIncrement('partitioned', 'id');
使用 Eloquent 查询分区
使用此包,您可以直接从 Eloquent 模型中查询单个分区或多个分区
创建模型
php artisan make:model Partitioned
然后编辑您刚刚创建的模型
namespace App; use Illuminate\Database\Eloquent\Model; class Partitioned extends Model { protected $table = 'partitioned'; }
查询单个分区
Psy Shell v0.9.9 (PHP 7.3.6 — cli) by Justin Hileman >>> use App\Models\Partitioned; >>> Partitioned::partition('name')->first();
查询多个分区
Psy Shell v0.9.9 (PHP 7.3.6 — cli) by Justin Hileman >>> use App\Models\Partitioned; >>> Partitioned::partitions(['name', 'name1'])->first();
工匠命令
此包附带一组有用的工匠命令
php artisan laravel-mysql-partition
{action : Action to perform}
{--table=} {--method=} {--number=} {--excludeFuture} {--column=} {--partitions=*}
可用命令
关于动作的详细信息,请参阅:此链接。
列表
php artisan laravel-mysql-partition list --table=partitioned
创建
// Create by RANGE php artisan laravel-mysql-partition create --table=partitioned --column="YEAR(date)" --method=RANGE Enter a comma separated value for partitions of:YEAR(date): > 2019,2020,2021 Table did partitioned successfully! // Create by LIST php artisan laravel-mysql-partition create --table=partitioned --column="id" --method=LIST How many partition do you want to create?: > 3 Enter a comma separated value for list 0: > 1,2,3 Enter a comma separated value for list 1: > 4,5,6 Enter a comma separated value for list 2: > 7,8,9 Table did partitioned successfully! // Create by YEAR php artisan laravel-mysql-partition create --table=partitioned --column=date --method=YEAR Enter start year for partition:: > 2016 Enter end year for partition (leave blank for current year):: > 2020 Table did partitioned successfully! // Create by KEY php artisan laravel-mysql-partition create --table=partitioned --method=KEY --number=10 Table did partitioned successfully! // Create by HASH php artisan laravel-mysql-partition create --table=partitioned --method=HASH --column="MONTH(date)" --number=10 Table did partitioned successfully!
删除
php artisan laravel-mysql-partition delete --table=partitioned --partitions=year2018,year2020
Partition year2018,year2020 did delete successfully!
截断
php artisan laravel-mysql-partition truncate --table=partitioned --partitions=year2019,year2020
Partition year2019,year2020 did truncate successfully!
优化
php artisan laravel-mysql-partition optimize --table=partitioned --partitions=year2019,year2020
修复
php artisan laravel-mysql-partition repair --table=partitioned --partitions=year2019,year2020
检查
php artisan laravel-mysql-partition check --table=partitioned --partitions=year2019,year2020
分析
php artisan laravel-mysql-partition analyze --table=partitioned --partitions=year2019,year2020
重建
php artisan laravel-mysql-partition rebuild --table=partitioned --partitions=year2019,year2020
Partitions year2019,year2020 did rebuilt successfully!
测试
$ composer test # or $ composer test:unit
即将推出
$ composer test:integration
集成测试需要一个正在运行的MySQL数据库。如果您已安装Docker,可以轻松启动一个
$ make start_db # starts MySQL 8.0 # or $ make start_db V=5.7 # starts MySQL 5.7
贡献
欢迎提出建议和拉取请求!带测试的拉取请求是最好的!。
您在使用此包吗?
我感兴趣制作一个使用此库的所有项目的列表。请在GitHub上免费创建一个带有您项目名称和链接的问题;我们将将其添加到本站。
鸣谢 & 许可证
larave-mysql-partition 由 Luca Becchetti 拥有和维护
作为开源创作,任何帮助都受欢迎!
此库的代码根据MIT许可证许可;您可以在商业产品中无限制地使用它。
唯一的要求是在您的鸣谢/关于部分添加以下文本的一行
Partition by laravel-mysql-partition - http://www.lucabecchetti.com
Created by Becchetti Luca and licensed under MIT License.
关于我
我是一名专业程序员,背景在软件开发和设计,目前是 "Brokenice" 的领导者,该团队为知名品牌提供高性能的按需开发团队。
在过去几年中,我在一家名为 "Frind" 的初创公司担任项目经理和iOS高级软件工程师,我们开发了一个新的社交网络应用程序,它包括基于XMPP框架和Ejabberd服务器的聊天系统。
我在软件开发方面有很高的技能,我从小就作为网站管理员工作,并且是一名经验丰富的PHP开发者。在过去的几年里,我一直在努力进行移动应用程序编程,Swift用于iOS世界,Java用于Android世界。
我是一名经验丰富的移动开发者和架构师,在iOS、Android(3+年经验)等所有主要移动平台上都有团队管理、设计和开发的经验。
我在客户端和服务器端以及API/网络设计方面也有广泛的经验。
我的所有最新作品都托管在AWS Amazon云或Google Cloud Platform上,我能够配置网络和Unix服务器。对于我的最新作品,我配置了apache2、ssl、ejabberd集群模式、带有负载均衡器的API服务器等。
当我11岁时,我创建了一个名为openasp的ASP CMS,它在超过10,000个网站上使用了多年。当ASP被弃用时,该项目被关闭,我开始学习PHP。
我住在意大利的阿西西(佩鲁贾),一个小镇,如果您有任何问题,请联系我

