vkoori/laravel-mysql-partition

Laravel 的 MySQL 分区扩展。

1.0.0 2023-04-16 11:12 UTC

This package is auto-updated.

Last update: 2024-09-17 17:37:41 UTC


README

laravel-mysql-partition 是一个用于轻松处理 MySQL 分区的 Laravel 扩展包。请根据您的 MySQL 版本检查文档。分区需要 MySQL 版本 >= 5.1.0。

InAppNotify

★★ 请为我们的 GitHub 仓库点赞,或者 ☕ 赠我一杯咖啡 ★★

Luca Becchetti 创建

安装

InAppNotify

使用 composer 添加包

composer vkoori/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();

Artisan 命令

本包包含一组有用的工匠命令

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开发者。在过去几年中,我专注于移动应用程序编程,为iOS世界使用Swift,为Android世界使用Java。

我是一位经验丰富的移动开发者和架构师,拥有多年的团队管理、设计和开发所有主要移动平台(iOS、Android(3年以上经验))的经验。

我还在客户端和服务器端以及API/网络设计方面具有广泛的经验。

我的所有最后作品都托管在AWS Amazon云或Google Cloud Platform上,我能够配置网络、Unix服务器。对于我的最后作品,我配置了apache2、ssl、ejabberd在集群模式下、带有负载均衡器的API服务器等。

当我11岁时,我创建了一个名为openasp的ASP CMS,它已经被超过10,000个网站使用了多年。当ASP被弃用时,该项目被关闭,我开始学习PHP。

我住在意大利的阿西西(佩鲁贾),一个小镇,如果您有任何问题, 联系我