Do we have a stream mode where the entire resultset is not loaded in memory?

286 views
Skip to first unread message

nvqua...@gmail.com

unread,
Jan 1, 2016, 10:15:16 AM1/1/16
to ruby-pg
Mysql2 gem has a streaming option when dealing with large resultset, so that the load on memory is reduced. https://github.com/brianmario/mysql2#streaming

Do we have such option or a way to solve it when dealing with query that returns a large amount of data?

Thanks!
Huy

Lars Kanis

unread,
Jan 1, 2016, 1:01:21 PM1/1/16
to rub...@googlegroups.com
You have two options with PostgreSQL: The single row mode or the COPY command:

For the single row mode use the example here: http://deveiate.org/code/pg/PG/Result.html#method-i-stream_each

For COPY you can use the copy_data method: http://deveiate.org/code/pg/PG/Connection.html#method-i-copy_data
This is most interesting, if you need the rows in CSV format. If you need to process the result set in ruby, you can use a CopyRow-decoder instead: http://deveiate.org/code/pg/PG/TextDecoder/CopyRow.html

COPY is typically a bit faster than the single row mode for data from the database. For data to the database COPY is typically way faster than any other method.

--
Kind Regards,
Lars

Reply all
Reply to author
Forward
0 new messages