-
-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Batch insert with one INSERT query #680
Comments
How is that a bug? |
I would have expected better from a data mapper library. Or is there a way to do it in ROM? |
OK but this is a potential optimization, the feature isn't broken. Can you come up with a reproduction script? |
Here you go: require 'logger'
require 'securerandom'
require 'rom'
require 'rom-sql'
class UsersRelation < ROM::Relation[:sql]
schema :users, infer: true
def self.create_table(gateway)
gateway.create_table(:users) do
primary_key :id
column :name, String, null: false
end
end
end
class Repo < ROM::Repository[:users]
def insert_fake_records(n)
users.transaction do
records = Array.new(n) do
{
name: SecureRandom.uuid,
}
end
# FIXME: This causes many INSERTs instead of one.
# FIXME: result: :one doesn't seem to do do anything different than :many. Also, :none is missing.
users.command(:create, result: :one).call records
end
end
end
logger = Logger.new STDERR
config = ROM::Configuration.new :sql, 'sqlite::memory' do |config|
config.default.use_logger logger
end
UsersRelation.create_table config.gateways[:default]
config.register_relation UsersRelation # NOTE: If done before table creation, this can lead to a race condition.
container = ::ROM.container config
repo = Repo.new container
repo.insert_fake_records 10 Currently the output looks like this:
I'd like to only see one |
I noticed class Repo < ROM::Repository[:users]
def fast_insert_fake_records(n)
records = Array.new(n) do
{
name: SecureRandom.uuid,
}
end
users.multi_insert records
end
end
|
Looks like behavior in Sqlite is different when you do |
Closing in favor of rom-rb/rom-sql#411 |
Using the changeset API, I see that many
INSERT
queries are executed one by one. It should be one query with multiple row values.The text was updated successfully, but these errors were encountered: