在执行原始SQL后,是否应该调用PG :: Result#clear?(Should PG::Result#clear be called after you've executed raw

编程入门 行业动态 更新时间:2024-10-12 05:43:43
在执行原始SQL后,是否应该调用PG :: Result#clear?(Should PG::Result#clear be called after you've executed raw SQL?)

当你使用ActiveRecord::Base.connection.execute(sql_string) ,你应该调用结果clear以释放内存吗?

在这个播客的 19:09,扬声器(一个在Active Record上做了大量工作的Rails提交者)说如果我们使用ActiveRecord::Base.connection.execute ,我们应该在结果上调用clear ,或者我们应该使用方法ActiveRecord::Base.connection.execute_and_clear ,它占用一个块。

(他对方法名称有点不清楚.MySQL适配器的方法是free ,Postgres适配器的方法是clear 。他还提到了release ,但该方法不存在。)

我的理解是他说我们应该改变

result = ActiveRecord::Base.connection.execute(sql_string).to_a process_result(result)

ActiveRecord::Base.connection.execute_and_clear(sql_string, "SCHEMA", []) do |result| process_result(result) end

要么

result = ActiveRecord::Base.connection.execute(sql_string) process_result(result) result.clear

那个播客是我听到这个声明的唯一地方,我找不到任何关于它的信息。 我正在使用的Rails应用程序在许多实例中使用execute而clear ,并且我们不知道由它引起的任何问题。 在某些情况下,未通知clear更有可能导致内存问题吗?

When you use ActiveRecord::Base.connection.execute(sql_string), should you call clear on the result in order to free memory?

At 19:09 in this podcast, the speaker (a Rails committer who has done a lot of work on Active Record) says that if we use ActiveRecord::Base.connection.execute, we should call clear on the result, or we should use the method ActiveRecord::Base.connection.execute_and_clear, which takes a block.

(He’s a bit unclear on the method names. The method for the MySQL adapter is free and the method for the Postgres adapter is clear. He also mentions release, but that method doesn't exist.)

My understanding is that he's saying we should change

result = ActiveRecord::Base.connection.execute(sql_string).to_a process_result(result)

to

ActiveRecord::Base.connection.execute_and_clear(sql_string, "SCHEMA", []) do |result| process_result(result) end

or

result = ActiveRecord::Base.connection.execute(sql_string) process_result(result) result.clear

That podcast was the only place I've heard this claim, and I couldn't find any other information about it. The Rails app I'm working on uses execute without clear in a number of instances, and we don't know of any problems caused by it. Are there certain circumstances under which failing to call clear is more likely to cause memory problems?

最满意答案

这取决于适配器。 请记住,Rails不控制execute返回的对象。 如果您正在使用PostgreSQL,您将获得PG::Result ,并使用mysql2适配器,您将获得一个Mysql2::Result 。

对于PG( 此处记录 ),除非autoclear?否则需要调用clear autoclear? 返回true或者你会得到内存泄漏。 如果您有足够大的结果集以确保在清理之前不会导致内存问题,您可能还需要手动调用clear 。

Mysql2似乎没有通过Ruby API公开它的free ,并且似乎总是在GC期间自我清理。

It depends on the adapter. Keep in mind that Rails doesn't control the object that is returned by execute. If you're using PostgreSQL, you'll get back a PG::Result, and using the mysql2 adapter, you'll get back a Mysql2::Result.

For PG (documented here), you need to call clear unless autoclear? returns true or you'll get a memory leak. You may also want to call clear manually if you've got a large enough result set to ensure it doesn't cause memory issues before it gets cleaned up.

Mysql2 doesn't appear to expose its free through the Ruby API, and appears to always clean itself up during GC.

更多推荐

本文发布于:2023-08-05 17:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原始   PG   SQL   Result   raw

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!