--
---
You received this message because you are subscribed to the Google Groups "Dallas Ruby Brigade" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dallasrb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
buffer = Multisert.new connection: dbclient, database: 'some_database', table: 'some_table', fields: ['field_1', 'field_2', 'field_3', 'field_4'] buffer.with_buffering do |buffer| (0..1_000_000).each do |i| res = some_magical_calculation(i) buffer << res end end
We start by creating a new Multisert instance, providing the database connection, database and table, and fields as attributes. Next, we leverage #with_buffering to wrap our sample iteration. Within the block, we shovel the results fromsome_magical_calculation into the Multisert instance, which then handles all the heavy lifting in terms of writing to the database.
As an aside, #with_buffering is handling the following under the hood:
(0..1_000_000).each do |i| res = some_magical_calculation(i) buffer << res end buffer.write!
As we iterate through, the Multisert instance will build up the records and then write itself to the specified database table when it hits an internal count (default is 10_000 entries, but this can be adjusted via the max_buffer_count attribute). The buffer.write! at the end ensures that any pending entries are written to the database table that were not automatically taken care of by the auto-write that will kick in during the iteration.