sql - rails - 我不应该在 rails 迁移中使用默认值吗

标签 sql ruby-on-rails postgresql ruby-on-rails-4

我不确定我是否应该在 SO 上问这个问题。但是我必须这样做,因为我对自己的疑问没有清楚的认识。我被告知不要在 Rails 迁移中设置默认值,而是在我们的 CTO 的代码审查中使用空值。他的解释是这样的一旦数据库变得足够大,使用默认值的迁移就会运行缓慢并且可能会引发数据库超时错误。但我认为在数据库级别它足够快,可以作为事物的初始设置来执行。我说得对吗?

最佳答案

当表行数很大时,在添加列迁移中使用默认值需要时间。

这是 Postgres Doc 给出的提示:https://www.postgresql.org/docs/9.5/static/ddl-alter.html

Tip: Adding a column with a default requires updating each row of the table (to store the new column value). However, if no default is specified, PostgreSQL is able to avoid the physical update. So if you intend to fill the column with mostly nondefault values, it's best to add the column with no default, insert the correct values using UPDATE, and then add any desired default as described below.

关于sql - rails - 我不应该在 rails 迁移中使用默认值吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915955/

相关文章:

postgresql - 如何将 Postgres 中序列的值设置为该表的 max(value) + 1?

ruby-on-rails - rspec 路由测试和主机

sql - 使用 Filter 参数创建 View

sql - 验证 T-SQL 存储过程的可靠方法

sql - 如何在 PostgreSQL 中将所有列的默认值设置为 null

javascript - jQuery 切换仅适用于 <ul> 中的顶部元素

ruby-on-rails - 如何开始在 Rails 中进行单元测试?

PostgreSQL 错误 : there is no built-in function named "(SQL state 42883)

postgresql - 在 postgreSQL 中更改序列名称

sql - T-SQL获取2个日期之间的工作日数