Skip to content
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

Implement batch insert of models (multiline insert) #1279

Open
eugeneRover opened this issue Mar 31, 2023 · 2 comments
Open

Implement batch insert of models (multiline insert) #1279

eugeneRover opened this issue Mar 31, 2023 · 2 comments

Comments

@eugeneRover
Copy link

eugeneRover commented Mar 31, 2023

It would be great if multiple models can be saved in one SQL INSERT.
Batch operation (that already exists in JavaLite) does not provide required functionality (it uses the same single-line INSERT instead).
Example:

class User extends Model {
      public User(String name, Integer age){
           set("name", prodItem, "age", age);
      }
}

...

User c1 = new User("a", 4); 
User c2 = new User("b", 5); 
User c3 = new User("c", 6); 

// suggested method, it executes following SQL INSERT with appropriate query parameters set: 
// INSERT INTO users(name, age) VALUES (?,?), (?,?), (?,?) RETURNING id
User.saveMultiple(List.of(c1,c2,c3));

// after the operation, all input models are saved, and have ID set
assert(c1.getId() != null);
assert(c2.getId() != null);
assert(c3.getId() != null);
@ipolevoy
Copy link
Member

@eugeneRover the batch operations you are mentioning will do what you need, except setting the IDs on the models. Is there anything else missing?

@eugeneRover
Copy link
Author

Batch operation generate single-row INSERTs, for my example it will be 3 SQL calls:

INSERT INTO users(name, age) VALUES (?,?) RETURNING id
INSERT INTO users(name, age) VALUES (?,?) RETURNING id
INSERT INTO users(name, age) VALUES (?,?) RETURNING id

What I propose is to create multi-row INSERT , like this:

INSERT INTO users(name, age) VALUES (?,?), (?,?), (?,?) RETURNING id

This will give us drastic speed boost on massive INSERT operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants