Skip to content

Generating several N+1 queries when adding multiple resources to an export thus slowing down query and timing out Heroku #40

Closed

Description

@subratrout

When I am trying to export a resource which has two or three other related models data to be exported along with it, it is generating several N+1 queries and with couple of thousand rows I am facing server time out issues.
Upon installing bullet gem I get following on my bullet.log

USE eager loading detected
  Resource => [:sponsorships]
  Add to your query: .includes([:sponsorships])

GET /admin/resources/export.csv
USE eager loading detected
  Resource => [:organizations]
  Add to your query: .includes([:organizations])

Just for example my resource_dashboard contains following: 


```ATTRIBUTE_TYPES = {
    id: Field::Number,
    name: Field::String,
    resource_type: Field::Enumerate,
    organizations: Field::HasMany.with_options(
      transform_on_export: -> (field) { field.data&.map(&:name)&.join(", ")  if field.data }
    ),
    event_detail: Field::HasOne,
    questions: Field::NestedHasMany.with_options(
      skip: :resource,
      transform_on_export: -> (field) { field.data&.map(&:text)&.join(", ") if field.data }
    ),


Is there a way to speed up query or prevent eager loading?

Activity

  1. andreibondarev commented on Jun 6, 2021

    @andreibondarev
    Member

    @subratrout A couple of thoughts-- it looks like within the transform_on_export you may be select-ing all fields from associations. Replacing field.data&.map(&:name) with field.data&.pluck(:name) might pull a smaller ActiveRecord object.

    If you're on Heroku, how many records are you attempting to export and how much RAM do you have configured on the dyno?

  2. subratrout commented on Jun 7, 2021

    @subratrout
    Author

    The table contains only 2100 rows. I am able to export if exporting from single table, however when I join one or more tables that's when it times out. The app using two standard-2x-dyno with 1GB Memory each.

  3. MichalJaneda commented on Oct 5, 2022

    @MichalJaneda
    Contributor

    You may preload them manualy by overriding controller method.
    Here is my example:

    def scoped_resource
      return super.preload(:operational_region, :logistic_region) if action_name == 'export'
    
      super
    end
  4. DavidGeismarLtd commented on Dec 1, 2022

    @DavidGeismarLtd

    Is there any way to configure the gem to run the export method in a sidekiq job ?

  5. andreibondarev commented on Dec 8, 2022

    @andreibondarev
    Member

    @DavidGeismarLtd Not currently. It's rather complex. What issue are you having? Server timing out?

  6. mattcarey commented on Nov 18, 2024

    @mattcarey

    We are getting a H12 error on Heroku when exporting to CSV using this gem. Our export isn't big but has a few pieces of data from other tables. Any idea of a solution please?

  7. andreibondarev commented on Nov 18, 2024

    @andreibondarev
    Member

    We are getting a H12 error on Heroku when exporting to CSV using this gem. Our export isn't big but has a few pieces of data from other tables. Any idea of a solution please?

    If you copied your production DB to local -- are you able to successfully generate the export?

  8. andreibondarev commented on Oct 21, 2026

    @andreibondarev
    Member

    This is addressed in #59 and will close automatically when it's merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions