在 Laravel migration 中创建 MySQL 索引前缀长度
在字符串类型的列中,索引取一定长度就可以高度区分。适当的索引长度可以节省不少空间,提高索引查询效率。由于仅部分数据库支持,作者并没有给 index 方法增加 length 参数的计划,我在 GitHUB中找到了相关解决方案。
Ability to define index key length while creating schema
[5.7] Support index length on MySQL
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->char('a', 10);
$table->index([DB::raw('a(7)')]);
});
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
建议改成这样,可以自定义索引名称