14
ANORM Anorm is Not an Object Relational Model

Relate scala-user-group-2014

Embed Size (px)

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.

Citation preview

Page 1: Relate scala-user-group-2014

ANORMAnorm is Not an Object Relational Model

Page 2: Relate scala-user-group-2014

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

Page 3: Relate scala-user-group-2014

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;

Page 4: Relate scala-user-group-2014

ORM Generated Queries Suck

Page 5: Relate scala-user-group-2014

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)

Page 6: Relate scala-user-group-2014

Using Anorm - INSERT

SQL(""“INSERT INTO users (username, password) VALUES ({username}, {password})

""").on(‘username -> “matthew”‘password -> “mypassword”

).executeInsert()(jdbcConnection)

Page 7: Relate scala-user-group-2014

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!

Page 8: Relate scala-user-group-2014

RelateA lightweight database access layer for Scala

Page 9: Relate scala-user-group-2014

A Closer LookMark Siebert – Software Engineer at Lucid Software

Page 10: Relate scala-user-group-2014

Relate beats Anorm

• Extremely FAST!

• Uses Scala’s CanBuildFrom for creation of custom data structures

• Data Streaming

• Data Pagination

Page 11: Relate scala-user-group-2014

Benchmarks - SELECT

Page 12: Relate scala-user-group-2014

Benchmarks - INSERT

Page 13: Relate scala-user-group-2014

Thank YouAny Questions?

Page 14: Relate scala-user-group-2014

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