如何在laravel数据库迁移中实现分区

时间:2016-09-22 02:34:09

标签: laravel laravel-5

使用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查询。

2 个答案:

答案 0 :(得分:3)

我认为这对你很有帮助,

      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']);
       });         

我到处搜索,我无法解决分区问题   但是,

我的建议使用,低于未准备好的向上向下功能的迁移文件功能

DB::unprepared()   

在迁移中使用分区运行SQL查询。

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

答案 1 :(得分:1)

现在有一个针对此的Composer程序包,称为Breakice / laravel-mysql-partition:

https://packagist.org/packages/brokenice

以下是文档中的示例:

=ARRAYFORMULA(REGEXEXTRACT(A1:A15,"-*\d*.?\d+"))