Code Monkey home page Code Monkey logo

orm-demo's Introduction

Comparing GORM vs SQL

Just wanted to make self-documentation about tradeoff between using ORM like (gorm) and raw SQL query using database/sql package in golang.

Summary:

  • Use a simple model which has multiple relationships (has one, belongs to, has many and many-to-many).
  • Create two repositories: one that is using *gorm.DB and another one that is using *sql.DB. Those two data repositories are demonstrating the API differences between gorm and sql. You can see how big differences gorm makes in simplifying the database queries and struct construction.
  • You can refer to the unit test to see that those two repositories are fetching the same number of data and fields.
  • For fetching simple data structure, gorm and sql package has no big difference in terms of performance. But, when it is trying to fetch complex data structure, sql outperforms gorm. Look at the benchmark result below!

Below is the benchmark of the experiments. I dont want to explain, look at these data below by yourself and try to make sense of it by looking at the source code for each package.

Benchmark with SQLMock

Gorm

pkg: github.com/imrenagi/orm-demo/db/orm/
====
BenchmarkFindAll_WithMock-16                       10000            172983 ns/op
BenchmarkFindByIDWithJoin_WithMock-16              10000            220720 ns/op
BenchmarkFindCompletedByID_WithMock-16              3914           1142499 ns/op

Raw SQL

pkg: github.com/imrenagi/orm-demo/db/sql/ 
===
BenchmarkFindAll_WithMock-16                       10000            157948 ns/op
BenchmarkFindByIDWithJoin_WithMock-16              10000            183222 ns/op
BenchmarkFindCompletedByID_WithMock-16              7498           1062009 ns/op

Benchmark with Postgres Running Locally

Gorm

pkg: github.com/imrenagi/orm-demo/db/orm
====
BenchmarkFindAll_WithDB-16                           538           1959670 ns/op
BenchmarkFindByIDWithJoin_WithDB-16                  618           1755951 ns/op
BenchmarkFindCompletedByID_WithDB-16                 160           6792193 ns/op

Raw SQL

pkg: github.com/imrenagi/orm-demo/db/sql
===
BenchmarkFindAll_WithDB-16                           699           1672481 ns/op
BenchmarkFindByIDWithJoin_WithDB-16                  615           1756467 ns/op
BenchmarkFindCompletedByID_WithDB-16                 618           1743564 ns/op

orm-demo's People

Contributors

imrenagi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.