It is important to use the correct strategy when bulk creating new records in Spanner using ActiveRecord. ActiveRecord will by default create a separate insert statement for each entity that you create. The Spanner ActiveRecord adapter is able to translate this into a set of insert Mutations instead of a list of insert DML statements, which will execute a lot faster.
There are two ways to achieve this:
- Create a batch of entities without an explicit transaction.
- Use a read/write transaction with isolation level
:buffered_mutations
See also the Mutations
sample in this repository.
The sample will automatically start a Spanner Emulator in a docker container and execute the sample against that emulator. The emulator will automatically be stopped when the application finishes.
Run the application with the command
bundle exec rake run