thoughtbot / administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
http://administrate-demo.herokuapp.com
MIT License
5.85k stars 1.11k forks source link

Performance Problems Counting Associated records #2088

Closed jamie closed 2 years ago

jamie commented 2 years ago

So this has some overlap with #1697 but...

I'm attempting to make a vanilla admin for a work application, where we have one model with a large number of associated log records, but as time goes on we're starting to see excessively long request times from default-generated screens.

Example app has a fresh rails/administrate install, with two models. Setup steps are in the git commit messages. This gets me the following screen:

Users - Bug Repro

with the following queries via rails logs:

  User Load (0.1ms)  SELECT "users".* FROM "users" LIMIT ? OFFSET ?  [["LIMIT", 20], ["OFFSET", 0]]
  Message Load (85.3ms)  SELECT "messages".* FROM "messages" WHERE "messages"."user_id" = ?  [["user_id", 1]]

Loading all the associated messages when we just need a count is prohibitively slow once the model is of a decent size (even with this small 4-column table, there's a very measurable performance impact I believe due to ActiveRecord object creation). Running ab on a local dev server, so concurrency 1:

$ ab -n 100 http://localhost:3000/admin
Time per request:       28.603 [ms] (mean)    [with  0  messages]
Time per request:       214.491 [ms] (mean)   [with 10k messages]
Time per request:       416.564 [ms] (mean)   [with 20k messages]

All that to say: when generating a count of associated objects, I would expect Administrate to be running a select count(*) here for performance.

pablobm commented 2 years ago

Thank you for this report. It provided a more focused example that helped me understand the issue much better. I have proposed a solution at https://github.com/thoughtbot/administrate/issues/1697#issuecomment-1004606731 PRs welcome :-)

pablobm commented 2 years ago

I'm going to close this in favour of #1697.