Simmi Badhan


50 Experience
0 Lessons Completed
0 Questions Solved


My ActiveRecord queries are taking too much load time and slowing down my API response times. I don't even have much data in the databse, like for accounts table there are only 10 rows. I'm running a simple query on pkey and here is the load time from logs:

Account Load (37.5ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]]

Result of Explain Analyze:

EXPLAIN ANALYZE SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 1 LIMIT 1;

Limit (cost=0.00..1.12 rows=1 width=34) (actual time=0.012..0.012 rows=1 loops=1)
-> Seq Scan on accounts (cost=0.00..1.12 rows=1 width=34) (actual time=0.011..0.011 rows=1 loops=1)
Filter: (id = 1)
Planning Time: 0.052 ms
Execution Time: 0.022 ms

Another example:
Account Load (6.0ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."name" = $1 LIMIT $2 [["name", "clinic"], ["LIMIT", 1]]

Project configuration:

Rails - 5.2.3
Ruby - 2.5.1
Posgres - 11.5

Any pointers will be very helpful. Thanks

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.