Gabriel Maggiotti
Gabriel Maggiotti

Reputation: 37

why does nodejs orm performs worse than just running a simple query?

Just doing some performance testing using orm2 and seems to be 4 times slower than just querying directly with sql. Any thoughts?

https://github.com/gmaggiotti/rule-restApi/tree/orm-poc

Benchmark using ORM2

Document Path:          /rules/
Document Length:        6355 bytes

Concurrency Level:      100
Time taken for tests:   5.745 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      6484000 bytes
HTML transferred:       6355000 bytes
Requests per second:    174.06 [#/sec] (mean)
Time per request:       574.526 [ms] (mean)
Time per request:       5.745 [ms] (mean, across all concurrent requests)
Transfer rate:          1102.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       2
Processing:   118  552  83.1    555     857
Waiting:      116  552  83.1    555     857
Total:        119  552  83.0    555     857

Benchmark using just sql

Document Path:          /rules/
Document Length:        6355 bytes

Concurrency Level:      100
Time taken for tests:   1.630 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      6484000 bytes
HTML transferred:       6355000 bytes
Requests per second:    613.38 [#/sec] (mean)
Time per request:       163.032 [ms] (mean)
Time per request:       1.630 [ms] (mean, across all concurrent requests)
Transfer rate:          3883.92 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       2
Processing:    98  158  49.2    137     361
Waiting:       98  158  49.2    137     361
Total:         98  158  49.4    137     362

Upvotes: 0

Views: 988

Answers (1)

Paul
Paul

Reputation: 36349

Not sure this is worthy of an answer, but it's too long for a comment.

This is (in my experience) true in every language/platform, for every ORM. In general, you don't use ORMs for query performance, you use them for code maintenance optimization and developer speed.

Why is this the case? Well, as a rule, ORMs have to translate what you say in language X into SQL, and in doing so they won't often come up with the most optimized query. They will typically do the query generation on the fly, and so the actual "building" of the string of (ideally parameterized) SQL takes some small amount of time, as can reflection on the structure of the native code objects to figure out what the right column names, etc.

Many ORMs are also not completely deterministic in terms of how they do this, either, which means that the underlying DB has a harder time caching the query plan than they might otherwise have. Also I couldn't find your actual benchmark tests in the link you provided; it's possible that you're not actually measuring apples to apples.

So I can't answer specifically for the particular module you're using without spending more time on it than I care to, but in general I would discourage this line of questioning for the reasons stated above. The workflow I've often used is to do all my development using the ORM and worry about optimizing queries, etc, once I can do some production time profiling, and at that point I would replace the worst offenders with direct SQL or possibly stored procedures or views (depending on the DB engine) to improve performance where it actually matters.

Upvotes: 1

Related Questions