为什么Laravel自动为约束列添加索引 [英] Why Does Laravel automatically add an index for constrained columns

查看:232
本文介绍了为什么Laravel自动为约束列添加索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Laravel为此情况自动添加"索引"?

Why Does Laravel automatically add an "INDEX" for this condition?

$table->foreignId('user_id')->nullable()
    ->constrained('users','id')
    ->onDelete('no action')
    ->onUpdate('no action');

推荐答案

Laravel 仅添加外键约束,而不隐式添加 index .但是某些数据库(例如MySQL)会自动索引外键列.

Laravel only adds a foreign key constraint and doesn't add index implicitly. But some databases such as MySQL automatically index foreign key columns.

在数据库表中定义外键约束时,将不会自动创建索引 >外键" 列,如 PRIMARY KEY 约束情况,其中将自动创建集群索引 在定义它时. 强烈建议在外键列上创建索引,以增强主键和外键之间的联接性能,并降低维护关系的成本 >在表和表之间.在添加任何新索引之前,最好在开发环境上进行测试并监视实施后的总体性能,以确保所添加的索引可以提高性能并且不会对系统性能产生负面影响.

When you define a foreign key constraint in your database table, an index will not be created automatically on the foreign key columns , as in the PRIMARY KEY constraint situation in which a clustered index will be created automatically when defining it. It is highly recommended to create an index on the foreign key columns, to enhance the performance of the joins between the primary and foreign keys, and also reduce the cost of maintaining the relationship between the child and parent tables. Before adding any new indexes, it is better to test on a development environment and monitor the overall performance after the implementation, to make sure that the added indexes improve performance and do not negatively impact the system performance.

这篇关于为什么Laravel自动为约束列添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆