【问题标题】:How to implement partition in laravel database migrationlaravel 数据库迁移中如何实现分区
【发布时间】:2023-04-03 20:45:01
【问题描述】:

使用 Laravel 5.3 如何实现分区。以下是我尝试在迁移中添加的 mysql 表结构。

CREATE TABLE `settings` (
    `id` INT(10) unsigned NOT NULL AUTO_INCREMENT,
    `client_id` INT(11) NOT NULL,
    `key` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
    `value` TEXT COLLATE utf8_unicode_ci NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY `settings_id_primary` (`client_id`, `id`),
    UNIQUE KEY `settings_key_unique` (`client_id`, `key`),
    KEY `settings_id_key` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PARTITION BY KEY (`client_id`) PARTITIONS 50;

以下是我目前尝试过的,但这只是添加列和键。

    Schema::create('settings', function(Blueprint $table) {
        $table->integer('id'); // I can't use increments, because throwing an error when I try to add primary key below
        $table->integer('client_id');
        $table->string('key');
        $table->text('value');
        $table->timestamps();

        $table->primary(['client_id', 'id']);
        $table->unique(['client_id', 'key']);
    });

如何进行分区?如果迁移不支持分区。有没有办法可以在迁移中转储整个 SQL 查询并运行。

【问题讨论】:

  • 不,使用 Laravel Schema BUilder 无法做到这一点

标签: laravel laravel-5


【解决方案1】:

我觉得对你有帮助,

      Schema::create('settings', function(Blueprint $table) {
         $table-> increments('id');
         $table->integer('client_id')->primary();
         $table->string('key');
         $table->text('value');
         $table->timestamps();

         $table->unique(['client_id', 'key']);
       });         

      Schema::create('settings', function(Blueprint $table) {
         $table-> increments('id');
         $table->integer('client_id');
         $table->string('key');
         $table->text('value');
         $table->timestamps();

         $table->primary('client_id');
         $table->unique(['client_id', 'key']);
       });         

我到处搜索,我找不到分区的解决方案。
但是,

我的建议使用,下面将updown函数的迁移文件函数准备好

DB::unprepared()   

在迁移中运行带分区的 SQL 查询。

喜欢,

DB::unprepared('create table....')

【讨论】:

  • 分区在哪里?
  • 请使用未准备的或原始的数据库。也许对你有帮助。
  • @SauminiNavaratnam 如果它是工作,那就接受它.. 其他人会得到它的知识。
【解决方案2】:

现在有一个 Composer 包,称为 brokenice/laravel-mysql-partition:

https://packagist.org/packages/brokenice

这是来自文档的示例:

// You use their extended Schema class:
use Brokenice\LaravelMysqlPartition\Schema\Schema;
// You might also need this (I didn't need it for partitioning by hash):
use Brokenice\LaravelMysqlPartition\Models\Partition;

// I omitted class and method definition boilerplate...

// Create a table as you would normally:
Schema::create('partitioned', static function (Blueprint $table) {
    // ...
});
    
// Now partition it (it will run an ALTER TABLE query):
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])
    ]
);

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 2014-08-03
    • 2020-05-11
    • 2019-05-03
    • 2014-02-13
    • 2018-04-04
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多