'ActiveAdmin still does subquery_for_count even when pagination_total: false

I have a redshift table in the billions of records, lets call it SomeModel.

ActiveAdmin.register Redshift::SomeModel do
  menu parent: 'Redshift'

  config.filters = true
  config.sort_order = '' <-- I figured out this removes the order by, which is intentional

  filter :message_uuid, as: :string
  filter :epoch, as: :numeric

  index pagination_total: false do
    column :message_uuid, sortable: false
    column :epoch, sortable: false
    column :status, sortable: false
    actions
  end

However as you can see, the table gets called 3 times. 2 of which is doing some sort of sub query. (this is local log)

screenshot of logs

When I explore the trace on newrelic, you can see the 3 queries are very costly. A 46 second response time is no beauno. I'm not really sure what the 2 counts are doing, and why its done twice. How can I remove those count queries?

new relic trace



Solution 1:[1]

This appears to be a longstanding bug having to do with how the collection helper queries collection size — the current definition triggers unnecessary/redundant COUNT queries, and it's called several times when building an index page with paginated results. I submitted a PR to ActiveAdmin, but you can try overriding the helper in your project with this:

module ActiveAdmin
  module Helpers
    module Collection 
      def collection_size(c=collection)
        return c.length if c.is_a?(Array) || c.limit_value.present?

        c = c.except :select, :order
        c.group_values.present? ? c.size.size : c.size
      end
    end
  end
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1