relate scala-user-group-2014
DESCRIPTION
Initiallly presented at Scala User Group in SLC, UT on 3 Sep 2014. Anorm is part of the Typesafe Play! framework stack. It has nice features, but is lacking performance. Relate is a new library, inspired by Anorm, whose performance is closer to the underlying JDBC library. This makes it a better candidate for enterprise and time sensitive workloads.TRANSCRIPT
ANORMAnorm is Not an Object Relational Model
On SQL vs ORM:
“SQL is already the best DSL for accessing relational databases. We don’t need to invent something new. Moreover the SQL syntax and features can differ from one database vendor to another.”
-- Play! Framework
CakePHP Query Generation
$this->documents->find(‘all’,array(
‘conditions’ => array(‘Document.creator_id’ => 5
))
);
SELECT * FROM documents AS d LEFT JOIN users AS u ON d.creator_id =
u.creator_id LEFT JOIN folder_entriesAS f ON d.id = f.document_id LEFT
JOIN templates AS t ON t.id = d.template_id LEFT JOIN
document_attributes AS a ON a.document_id = d.id LEFT JOIN
documents_users AS du ON du.document_id = d.id LEFT JOIN
………WHERE d.creator_id = 5;
ORM Generated Queries Suck
Using Anorm - SELECT
val userParser = long(“id”) ~ str(“username”) ~ str(“password”) map {case id ~ username ~ password => UserRecord(id, username, password)
}
SQL(""“SELECT * FROM users WHERE id = {id}
""").on(‘id -> 5
).as(userParser.singleOpt)(jdbcConnection)
Using Anorm - INSERT
SQL(""“INSERT INTO users (username, password) VALUES ({username}, {password})
""").on(‘username -> “matthew”‘password -> “mypassword”
).executeInsert()(jdbcConnection)
Anorm Analysis
PROs
• Easy to use
• Works with all JDBC-compatible databases
• Only queries the tables, columns, and rows you need
• Good documentation and examples
CONs
• Play! Framework is dropping support
• Only recently added support for lists of things (IN clause)
• Extremely SLOW!
RelateA lightweight database access layer for Scala
A Closer LookMark Siebert – Software Engineer at Lucid Software
Relate beats Anorm
• Extremely FAST!
• Uses Scala’s CanBuildFrom for creation of custom data structures
• Data Streaming
• Data Pagination
Benchmarks - SELECT
Benchmarks - INSERT
Thank YouAny Questions?
Links
• Relate Source: https://github.com/lucidsoftware/relate
• Relate Documentation: https://github.com/lucidsoftware/relate/wiki
• Relate Benchmarks: https://github.com/lucidsoftware/relate-benchmarks
• Anorm Documentation: https://www.playframework.com/documentation/2.3.x/ScalaAnorm
• We’re Hiring: https://www.golucid.co/jobs