mysql设置外键会影响性能吗_关于,在Mysql中,外键是否会影响性能的问题???...

In MySQL, a foreign key requires an index. If an index already exists, the foreign key will use that index (even using the prefix of an existing multi-column index). If no index exists, defining the foreign key will build the index.

So the size increase and time to create a foreign key is about the same as to create an index on the same column(s).

The performance of a SELECT is not impacted significantly by the presence of a foreign key. Only a slight additional work for the optimizer, to consider the new indexes.

The performance of updating is more, because for each foreign key, an INSERT/UPDATE/DELETE has to check to see if the constraint is satisfied. That means a primary key lookup to the referenced tables. This impact is measurable, and it is greater if the referenced tables are not in the buffer pool.

Another impact is the locking. If I update a row in a child row that has a foreign key, InnoDB places a shared-lock on the referenced rows in the parent tables. That means no one can update those parent rows until I commit. If you have lots of threads updating child rows, then the parent rows may be locked most of the time, and this can make it hard to do concurrent work in the parent tables. Not so much a performance problem, but a concurrency problem.

As with all "how does that perform" questions, the answer really depends on your workload. If you don't have concurrent updates, for example, that issue may not effect you for all practical purposes. Testing it yourself with load testing is the only way to be sure. It's not something anyone on StackOverflow can answer precisely for you.

读后小结:

1.外键就想其他类型的索引一样,要说性能方面的影响,它影响的主要是写入操作(如,UPDATE/INSERT/DELETE);

2.但它与单表索引不同的是,它会引用一张或多张父表,这样当对子表进行写入操作(UPDATE/INSERT)的时候,父表就会被加上“共享锁”,这样在对子表高并发进行写入操作的情况下,对父表的写入操作就会由于“共享锁”的存在,而会长时间不能得到更新!当然查询是可以的。

3.作者给出了一个观点:外键主要造成的问题,并不是影响性能多少的问题,而是一个并发访问的问题!

4.所以是否使用外键,关键是要看你对并发要求的高低了!

原文链接:

http://dba.stackexchange.com/questions/77906/what-is-the-cost-of-adding-a-foreign-key-in-mysql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL创建外键约束的语法如下: ``` ALTER TABLE 子表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (子表外键字段) REFERENCES 父表名(父表主键字段); ``` 其,子表名为需要添加外键约束的表名,子表外键字段为需要添加外键约束的字段名,父表名为被参照的表名,父表主键字段为被参照的表的主键字段名。 例如,如果我们需要在名为orders的表添加一个外键约束,以确保其的customer_id字段只能引用名为customers的表的id字段,我们可以使用以下语句: ``` ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (customer_id) REFERENCES customers(id); ``` 在MySQL Workbench,我们可以通过可视化的方式来设置外键约束。具体操作步骤如下: 1. 打开MySQL Workbench,并连接到相应的MySQL服务器。 2. 打开需要添加外键约束的表所在的schema,并在左侧的导航栏该表。 3. 在右侧的“Columns”选项卡,找到需要添加外键约束的字段,并选该字段。 4. 在“Foreign Keys”选项卡,点击“Add Foreign Key”按钮。 5. 在弹出的窗口设置“Referenced Table”为被参照的表名,“Referenced Column”为被参照的表的主键字段名,“Foreign Key Name”为外键约束的名称,并点击“Apply”按钮。 需要注意的是,在MySQL Workbench设置外键约束后,需要点击工具栏上的“Apply”按钮才能将修改保存到数据库。 关于MySQL Workbench上的一个可能存在的bug,我需要更加具体的信息以便进行分析和解答。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值