-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PaperTrail adapter: poor version load performance with lots of versions #2808
Comments
It's been quite a while since the PR, but as far as I remember thing is that original implementation was using plain array: versions.collect { |version| VersionProxy.new(version, @user_class) } so pagination links were not working properly and you could not paginate versions at all, thus usage of |
For what it's worth, I've introduced a tiny hacky workaround in my codebase to improve performance. Since Kaminari is only used by Rails Admin in my codebase, I've monkey patched the module Kaminari
module ActiveRecordRelationMethods
def total_count(column_name = :all, _options = nil)
limit(5000).count(column_name)
end
end
end This makes the result exact if the true count is <= 5000, but returns 5000 if the true count is > 5000. This takes way less time than counting all 3 million records, but still lets people scroll through the first handful of pages. |
FYI I added some related performance tips to the wiki: https://github.com/sferik/rails_admin/wiki/Performance |
TL;DR: Loading a version with the PaperTrail adapter incurs a
COUNT(*) FROM versions WHERE item_type = 'Foo'
, which is slow as hell (> 30s) when there are lots of versions. Can we rewrite to avoid this?I'd love to help fix, but could use some pointers. Any ideas? Are we allowed to just not pass Kaminari's paginated array a
total_count
? Can we pass it lazily somehow, so it only gets computed if someone callstotal_count
on the proxy array?As our application's number of versions has grown (we're at ~3 million versions now), performance for the history tab has become abysmal, and is at the point that a single request can often take multiple minutes--which browsers usually time out, meaning we're simply unable to load that page.
To investigate, I looked at what queries the server makes when I click "View changes" for a specific version in the history tab. Here's an example:
The main unimproveable culprit is the
SELECT COUNT(*) FROM "versions" ...
query. Even with an index onitem_type
this query still takes a very long time since counting in Postgres always requires a full scan of the rows to be counted.Tracing back to the source, this query appears to be caused by
lib/rails_admin/extensions/paper_trail/auditing_adapter.rb
, line 109:... which was introduced by #2054.
The text was updated successfully, but these errors were encountered: