Can I save memory by writing SQL instead of ActiveRecord?

Is ActiveRecord useful in any way besides its concise, readable syntax? Is it true that ActiveRecord takes up more memory than pure SQL? (I am using PostgreSQL.)

I read on the Alexander Dymo blog post on Rails performance that ActiveRecord takes up more memory than pure SQL:

Easily manipulate data with ActiveRecord. But ActiveRecord is essentially a wrapper on top of your data. If you have 1G of data in the table, the ActiveRecord view from it will take 2G, and in some cases, more. Yes, in 90% of cases the overhead is justified by the additional convenience that you receive. But sometimes you don’t need it.

I also read in the documentation that ActiveRecord is "better":

If you use the original SQL to search for database records, then you will usually find that there are better ways to operate in Rails. Active Record isolates you from having to use SQL in most cases.

I tested Error R14 (Memory quota exceeded)on Heroku, therefore, to solve the problem, I learned about bloating and memory leak. I have identified some ways to improve, including loading ActiveRecord data with impatience, as in List.joins(:quantities).find(@list_id), as well as reducing overall calls to the database. But I'm still hungry for memory.

ActiveRecord , , SQL, . , SO , , AR.

AR?

+4
1

, :

, ActiveRecord, - . - , .

Book.where('title LIKE ?', '%Rails%').update_all(author: 'David')

, raw SQL ( ) Rails ActiveRecord Rails ( ), , , Rails, , , .

:

Book.where('title LIKE ?', '%Rails%').update_all(author: 'David')

Book.where('title LIKE ?', '%Rails%').each do |b|
  b.update(author: 'David')
end

SQL :

update books
set author = 'David'
where title like '%Rails%'

. - Rails-, , . /

, ActiveRecord, Ruby ( , , ,...), SQL UPDATE . , Rails CPU Ruby.

" ActiveRecord", , Rails, - - " ".


ActiveRecord . Model.all, , , . Rails, , , . ORM , .

2.2 :

. . ? Ruby . . , Ruby . , , 1G 3G . , 3G. . .

, .

+4

Source: https://habr.com/ru/post/1656867/


All Articles