使用 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 查询并运行。
我觉得对你有帮助
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']);
});
我到处找,找不到分区的解决方案。
但是,
我的建议使用,下面不准备进入up和down函数的迁移文件函数
DB::unprepared()
迁移以使用分区运行 SQL 查询。
喜欢,
DB::unprepared('create table....')
现在有一个 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])
]
);
您不需要任何库来创建分区。
只需将分区代码添加到“engine”属性中即可。
示例(假设您使用 MySQL 或 MariaDB):
public function up(): void
{
Schema::create('event_sources', function (Blueprint $table) {
$table->id()->primary();
$table->uuid('commit_id')->index();
$table->string('entity', 100);
$table->timestamps();
$table->engine = "InnoDB PARTITION BY RANGE COLUMNS(`id`)
(
PARTITION `p_first` VALUES LESS THAN (1000000),
PARTITION `p_two` VALUES LESS THAN (2000000),
PARTITION `p_more` THAN (MAXVALUE)
)";
});
}
您还可以直接将引擎选项添加到引擎属性中:
示例:
$table->engine = 'InnoDB ROW_FORMAT=COMPRESSED';