Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slick query very slow

I'm trying to learn Slick and have got a Postgres database up and running. I made a tiny program to test it like this:

import scala.slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

object Names extends Table[(String)]("names")     
{
    def name = column[String]("name", O.PrimaryKey)
    def * = name
}

object DbTest
{
    val db = Database.forURL("jdbc:postgresql://localhost:5432/names", 
                              driver = "org.postgresql.Driver")

    def main(args : Array[String]) =
    {
        print("Doing something... ")

        db withTransaction 
        {
            Query(Names) foreach 
            { 
                case (name) =>
                    println(name)
            }
        }
        println("... done!")
    }
}

The problem is that it takes about 5 seconds for anything to happen after print("Doing something... "). If I duplicate the db withTransaction block, both blocks are executed in quick succession after those first 5 seconds. Any ideas?

like image 508
Panzerschlacht Avatar asked Nov 01 '22 10:11

Panzerschlacht


1 Answers

Not sure I know the specific problem, but there are a few things in your code example that would be a concern for performance.

1) Make sure you are using Query Templates. There is quite a bit of overhead building the query with Slick that you don't want to repeat on each query.

2) You also shouldn't use threadLocalSession in real code (see thread here). It should look something like below.

3) Use a connection pool like C3P0.

Example:

val pool = // some connection pool like C3P0 or other

Database.forDataSource(pool).withSession { implicit session: Session =>
  ...
}
like image 77
Taylor Leese Avatar answered Nov 09 '22 17:11

Taylor Leese