将 varchar 类型的 postgres 字段转换为数字
我有查询修改 postgres 中超过 200 万条记录。
问题是其中一个字段的类型为 VARCHAR,我需要对其进行一些数学运算。现在我正在将字段转换为数字,如下所示:-
CAST(attribute as numeric)
整个查询大约需要 4 小时才能运行。
我正在寻找缩短执行时间的方法。在执行查询之前,有什么方法可以先将字段类型从 varchar 更改为 numeric 吗?我无法使用 alter table alter columns type 来执行此操作。
I have query to modify 2 million over records in postgres.
The problem is that one of the field is of type VARCHAR
, and I need to do some mathematical operations on it. Right now I'm casting the field to numeric like this :-
CAST(attribute as numeric)
The whole query takes approximately 4 hours to run.
I am looking at ways to fasten then execution time. Are the any way I can change the field type from varchar to numeric first before I execute the query? I can't use alter table alter column type to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您无法长时间阻止表读取,毫无疑问,最好的方法是创建“第二个”表并进行后续更新,就像@OMG Ponies 所说的那样。
另外,如果触发器中的任何一个将使用新值执行某些操作(例如:日志的触发器,因为我们不会更改它本身的“值”),请尝试禁用触发器。这可以大大提高性能,具体取决于您的触发器的作用。
像这样的东西:
If you can't block the table for reading for such long time, there's not doubt that the best approuch is create a "second" table and do a subsequent update, like @OMG Ponies said.
Also, try to disable triggers if any of them will do something with new value (eg: log's trigger, since we are not changing the "value" it self). This can increase performance a lot, depending what your triggers do.
Something like this: