Help! Simple query with multiple LEFT JOINs taking too much time with Slick 3.0.1

758 views
Skip to first unread message

Walgus

unread,
Aug 31, 2015, 5:36:43 AM8/31/15
to Slick / ScalaQuery

Hello,


I'm new to the Slick world and currently playing with Slick 3.0.1, got to say that it's been pretty hard to learn it, not sure if this is just me, but the documentation isn't quite newbie friendly I can say, lots of googling and githubbing involved to get to the Scala/Slick code you will see in a bit.


I've this SQL query I need it executed against an INNODB (mysql) database:


SELECT *
FROM article a
LEFT JOIN author au ON (a.authorID = au.id)
LEFT JOIN author pic ON (a.pictureID = pic.id)
LEFT JOIN game g ON (a.gameID = g.id)
LEFT JOIN picture gpic ON (g.pictureID = gpic.id)
LEFT JOIN company p ON (g.publisherID = p.id)
LEFT JOIN company d ON (g.developerID = d.id)
LEFT JOIN game_platform gp ON (g.id = gp.gameID)
LEFT JOIN platform pf ON (gp.platformID = pf.id)
LEFT JOIN game_sport gs ON (g.id = gs.gameID)
LEFT JOIN sport s ON (gs.sportID = s.id)
WHERE a.id = ?


Now, the thing is that, trying to get that query using the Slick DSL in scala, with something like the code below:


lazy protected val findByIDQueryWithJoins = for {
id <- Parameters[Int]
((((((((((article, author), articlePic), game), developer), publisher), gamePic), g2p), platform), g2s), sport) <-
articles.filter(_.id === id)
.joinLeft(authors).on(_.authorID === _.id)
.joinLeft(pictures).on(_._1.pictureID === _.id)
.joinLeft(games).on(_._1._1.gameID === _.id)
.joinLeft(companies).on(_._2.map(_.publisherID) === _.id)
.joinLeft(companies).on(_._1._2.flatMap(_.developerID) === _.id)
.joinLeft(pictures).on(_._1._1._2.flatMap(_.pictureID) === _.id)
.joinLeft(games2platforms).on(_._1._1._1._2.map(_.id) === _.gameID)
.joinLeft(platforms).on(_._2.map(_.platformID) === _.id)
.joinLeft(games2sports).on(_._1._1._1._1._1._2.map(_.id) === _.gameID)
.joinLeft(sports).on(_._2.map(_.sportID) === _.id)
} yield (article, author.map(_.nameAndSlug), articlePic.map(p => (p.id, p.fileHash, p.fileExtension)), game, gamePic.map(p => (p.id, p.fileHash, p.fileExtension)), developer.map(_.name), publisher.map(_.name), platform.map(_.name), sport.map(_.name))


Yeah, not the best looking code esp with all of those _._1._1._1 to associate the rows, I can live with that, for now at least. What i can't live with is the SQL code that get's generated by the Scala compiler, it's around 12500 characters long with many sub-select queries. On average the full generated query takes around 200ms compared the SQL code above taking only ~5ms, that's a huge difference I'm not used to. 


So, I'm I don't know if i'm really bad at writing queries in Scala/Slick or Slick is really bad a generating the SQL code, even if it was relying on the Database Query Optimizer, I think it's a lot of work for the optimizer. 


I've tried this on Slick 3.1.0-M2 as well but same results, even went as far as trying it on Amazon RDS Aurora (MySQL API) and I've got the same result. 


I don't want to give up on it just yet but I've been battling this for quite some time now and this thought of going back to my previous architecture stack is really getting louder and louder, which I'm reeeeally tempted to if i'm going to start writing plain SQL queries again!


Anyways, thanks for having your time reading my first experience with Slick. I hope someone will shed some light and help me shut that evil voice :)


Walter.

Walgus

unread,
Sep 1, 2015, 2:47:36 AM9/1/15
to Slick / ScalaQuery
Sorry for the formatting mess, it surely didn't look like that when I was editing my post.

I gave it another try using postgres as the database engine, migrated the mysql database using pgloader and I'm getting sub 5ms on the same slick query! Now that was super easy to do, didn't had to modify anything other than application.conf connection string and adding the schema name to each Table[T] extend.

Ideally though I would of have preferred to stay with mysql as the db engine due to the years of experience with it, but yeah if there is no other option left, well I've to get used to it I guess.

Stefan Zeiger

unread,
Sep 1, 2015, 5:18:31 AM9/1/15
to scala...@googlegroups.com
On 2015-08-31 11:36, Walgus wrote:
I've tried this on Slick 3.1.0-M2 as well but same results, even went as far as trying it on Amazon RDS Aurora (MySQL API) and I've got the same result.

Please try it on master. I recently (after M2) improved code generation for outer joins in https://github.com/slick/slick/pull/1247.

--
Stefan Zeiger
Slick Tech Lead
Typesafe - Build Reactive Apps!
Twitter: @StefanZeiger

Walgus

unread,
Sep 4, 2015, 3:57:33 AM9/4/15
to Slick / ScalaQuery
Nice, will give it a try and post back with the results. Thanks :)
Reply all
Reply to author
Forward
0 new messages