Rails ActiveRecord Eager Load takes a long time


I am using eager loading to join two of my tables


The Model1 table has about 800 rows and has many references to other tables. Whenever that line is called, it takes about 3 minutes to load the view that shows the info.

I then tried making a straight Postgres connection and running the same SQL query that was generated from the eager load and that finished in 50ms.

Why is it taking so much longer in ActiveRecord and is there anyways I can cut down on the time?

In the console, it gets to the following SQL query (this is a simplified version, of course) and it hangs for about 3 minutes before it continues and loads the page.

SELECT model1.*, model2.* FROM model1 LEFT OUTER JOIN model2 ON model2 .foreign_key = model1.id


It's likely that this is not due to eager loading per se, but reflects the overhead of instantiating many model objects. ActiveRecord must initialize objects when retrieving them from a database. You can prove this to yourself by adding a callback to the associated model:

after_find :yodel def yodel puts "Yodel-a-e-hoo!" end

This is pretty fast, but it is not instantaneous. When you are getting back many objects, that initialization time will start to affect performance.

The typical solution is to paginate results if possible. Otherwise you may need to re-write the query, using raw SQL if necessary, to be more selective.


  • Singular Value Decomposition Implementation
  • Can someone please explain how this implementation of bucket sort works?
  • docker-compose: connection refused between containers, but service accessible from host
  • Where to put clearQueue in jQuery code
  • Enterprise Architect - Cancel Connectors Bridges
  • Django Migrations fail during django initialization
  • PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea
  • Wait for .each() .getJSON request to finish before executing a callback
  • Flex items with same property values are rendering in different sizes
  • How to skip require in ruby?
  • What causes the runtime difference in this trivial fortran code?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Why the SequenceFile is truncated?
  • Do query loads all the data in memory
  • what makes a request a new request in asp.net C#
  • RxJava debounce by arbitrary value
  • pyodbc doesn't report sql server error
  • Check for zero lines output from command over SSH
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • What and where is mdimport
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • HTTP/2 streams vs HTTP/1.1 connections
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Atlas images wrong size on iPad iOS 9
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • How to set/get protobuf's extension field in Go?
  • Menu Color Fade on Hover with Jquery
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • ActionScript 2 vs ActionScript 3 performance
  • Importing jscolor library in angular 2
  • Display Images one by one with next and previous functionality
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • SQL merge duplicate rows and join values that are different