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

feat(sql) Sql batch insert #1458

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from

Conversation

eleroy
Copy link

@eleroy eleroy commented Jun 12, 2024

Hello,

This a pull request for implementing an additional method for batch inserting into a SqlLite database (should also work for other sql databases).

It is based on the QueryBuild push_values method of sqlx rust module.

It is possible to push any number of values but the JS function will split the request into batches of values that does not exceed 999 values since it seems to be the limit for sqlite.

From my benchmark it is 100 times faster than looping through execute commands (with 10000 values, but maybe my benchmark code is not the best). Here is the code I use for testing :

let db = await Database.load('sqlite:mydatabase.db')
let data = []
for(let i= 0;i<10000;i++){
data.push([(Math.random() + 1).toString(36).substring(7)])
}
let startTime = Date.now()
await db.batchInsert("INSERT into users (name) ", data)
console.log("Batch insert time ", Date.now()-startTime)

startTime = Date.now()
await Promise.all(data.map(v=> db.execute("INSERT into users (name) VALUES ($1)", v)))
console.log("Loop insert time ", Date.now()-startTime)
Batch insert time  190
Loop insert time  31462

The results for 1000 values:

Batch insert time  11
Loop insert time  557

@eleroy eleroy requested a review from a team as a code owner June 12, 2024 08:07
@FabianLars
Copy link
Member

Hey, thanks for contributing! Just one thing, i don't think we should care about the chunk size in the plugin code as it may be different per config and/or per database type. In my opinion this should be something the user should worry about / can decide how to handle (a docs comment is totally fine for that).

@eleroy
Copy link
Author

eleroy commented Jun 23, 2024

Hi,

No problem, I reverted the js method to a simpler form and added a note in the Readme.

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

Successfully merging this pull request may close these issues.

2 participants