-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add support for batch insert #259
Comments
You can obtain connection with ConnectionFactoryUtils helper method: |
Thanks, so far I have used |
Currently, we don't have support for batching. There are two types of batches that R2DBC supports:
Unparametrized batches could be supported through Parametrized statements seem straight-forward, but there's a caveat that is due to named parameter resolution. Named parameter support considers if a bound value is a collection type. If so, then the SQL expansion creates a parameter placeholder for each element in the Example: client.execute("INSERT INTO foo VALUES(:my_param)")
.bind("my_param", "a-value") Resulting SQL (Postgres syntax): INSERT INTO foo VALUES($1) client.execute("INSERT INTO foo VALUES(:my_param)")
.bind("my_param", Arrays.asList("one", "two")) Resulting SQL (Postgres syntax): INSERT INTO foo VALUES($1, $2) For batching, this example makes little sense as binding a collection to an client.execute("INSERT INTO foo VALUES(:my_param)")
.bind("my_param", Arrays.asList("one"))
.add()
.bind("my_param", Arrays.asList("one", "two")) |
I tried the code sample you provided above client.execute("INSERT INTO foo VALUES(:my_param)")
.bind("my_param", Arrays.asList("one"))
.add()
.bind("my_param", Arrays.asList("one", "two")) I assume the Is it possible to use parameterized statements to form batching via In addition, is it possible to use parameterized insert statement with multiple values? For instance, final List<Object[]> tuples = new ArrayList<>();
tuples.add(new Object[] {"foo", "bar"});
tuples.add(new Object[] {"FOO", "BAR"});
client.execute("INSERT INTO foo VALUES :tuples")
.bind("tuples", tuples) |
Hi, is there any update on this? I couldn't find the support for batch operations using DatabaseClient. Can you please confirm if the current batch operations support is limited to Statement and Connection objects? |
Hey @mp911de , This is my table:
To save a list of items, the code I am using:
The generated SQL statement (from DEBUG Logs):
I even tried using
|
Depending on the database type, you need to tell the database to return generated keys (see the Postgres documentation on |
Any updates on this? It's quite an important feature to be overlooked for so long. Tomorrow it's the 1 year birthday of no batch inserts :( |
Thanks @luiccn for reminding us. Meanwhile, Keep in mind that contributing to open source is essential if you want something to happen sooner than it would take the regular way. We'd be more than happy to work with you on the final design if you find the time to come up with a proposal and a pull request. |
Hi @mp911de, I'm interested to work on this issue, please could you give me more information about it Thanks |
Is there still no batching possible, except directly via the Connection? Using the raw batches at the connection is risky because all escaping has to be done "by hand". Batches perform way better than multiple inserts, even if using Statement.add().add()....execute(). I wonder why Statement.add() does not use a batch under the hood? |
Databases provide various options for batching. Users expect an improved approach to apply multiple operations with a noticeable throughput gain from batching.
The other approach to batching that we've seen in databases is the usage of prepared statements. Each prepared statement operation requires multiple roundtrips (prepare, execute, close). We've seen that API in some databases such as Postgres. Some drivers implement a prepared statement cache that can reuse prepared statements if the SQL text and the parameter types match a previously executed statement. That way, And that leads us back to the starting point: If you can render SQL statements (e.g. inserts) with safe inline parameters, then concatenate these into a semicolon-combined SQL string, then running that query will yield the best performance from a networking perspective. |
Thanks for pointing me to the batch usage of Though the
Thus I still would very much like it if the support for batching would be more integrated into the API, e.g.
Please don't consider this as a complaint. It is just a feature request :-) |
+1 for above, not sure what underlying complexity is , but I would love to see batch inserts through saveAll() API similar to jdbc as well ! |
I have try to perform a batch insert using
DatabaseClient
.I have not found a solution.
I have a wrokaround using a
Statement
but to get a statement I should create my ownConnection
.Have you planned to support a batch insert using
add
like withStatement
SPIThe text was updated successfully, but these errors were encountered: