Laravel和MySQL理解索引和复合/复合索引

问题描述 投票:0回答:1

在Laravel(v 6.8)中,我为users表创建了以下迁移。

用户迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('username')->unique()->index();
        $table->string('email')->unique()->index();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password')->nullable();
        $table->enum('role', ['super', 'admin', 'manager', 'subscriber', 'user'])->default('user');
        $table->boolean('is_root')->default(FALSE);
        $table->rememberToken();
        $table->timestamps();

        $table->unique(['username', 'email'], 'user_unique_credentials');
        $table->index(['username', 'email'], 'user_index_columns');
    });
}

说明

我知道index的基本知识及其工作原理,但是我对indexcolumn composite/compound上的index并不太清楚。

[App可能仅通过usernameemail进行查询,或者我可能同时对两个表进行查询。因此,如您在迁移代码中所见,我已经使用创建index $this->index()compound为每列以及两列设置了index

问题

我想知道是否正确设置了所有indexes,或者按照我的方式进行设置是一个坏主意?

如果不正确,那么我可以知道正确的方法吗?

在Laravel(v 6.8)中,我为users表创建了以下迁移。用户迁移public function up(){Schema :: create('users',function(Blueprint $ table){$ table-> ...

mysql laravel database-migration
1个回答
1
投票

[@danblack谢谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.