Skip to content

Instantly share code, notes, and snippets.

@sloanesturz
Created July 29, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloanesturz/3b65b793738d139702dab3a1a8a8eefa to your computer and use it in GitHub Desktop.
Save sloanesturz/3b65b793738d139702dab3a1a8a8eefa to your computer and use it in GitHub Desktop.
class BulkInsertSpec {
val ctx: PostgresAsyncContext[SnakeCase] = // ...
import ctx._
case class Examples(i: Int)
private val bulk = (1 to 2000).map(Examples(_))
describe("bulk insert") {
it("I expect this to be much faster") {
val start = Instant.now
ctx.run(liftQuery(bulk).foreach(query[Examples].insert(_))).map { _ =>
val end = Instant.now
println("Doing it the wrong way:", end.toEpochMilli - start.toEpochMilli)
// Takes about 4 seconds
succeed
}
}
it("This is actually fast") {
val values = bulk
.map {
case Examples(i) =>
s"(${i})"
}
.mkString(", ")
val q = s"INSERT INTO examples (i) VALUES $values"
val start = Instant.now
ctx.run(infix"#$q".as[Insert[Examples]]).map { _ =>
val end = Instant.now
println("Doing it the right way:", end.toEpochMilli - start.toEpochMilli)
// takes about 37 milliseconds
succeed
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment