volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Query Batching #286

Closed tiger5226 closed 6 years ago

tiger5226 commented 6 years ago

I could not find any documentation on batching. I am running into insert performance issues which I think could be solved by batching the inserts.

What I am trying to do is create a <model>Slice and insert them in one query. Is this not possible with SQLBoiler?

I do see that there are batch updates and a batch delete but not a batch insert.

aarondl commented 6 years ago

sqlboiler doesn't support batching inserts. Mostly because in some drivers (like pq for example) the fast batching is actually both a syntactic and stateful nightmare. Mind you this is based on doing this some number of years ago, maybe it's improved since then. In these cases I recommend you use the driver's capabilities directly to do your batch inserts.

It's also true that we could have done this:

func (yourTypeArray) InsertAll() {
  for _, y := range yourTypeArray {
    y.Insert()
  }
}

But this feels like it defeats the point of the helper since it's just hiding for loops and the performance penalties inherent in doing that particular loop with an array of models objects (which is also a bit against the spirit of Go). In the case of (Update/Delete)All it's actually giving performance benefits over the for loop by constructing efficient queries.