Fix performance issue in definition of approved_collections.

Description

Currently, when you call Work.find(id).approved_collections on a work that has at least one approved collection, it generates an extra database query:

The first query, of course, is the work being loaded from Work.find(5). The second query loads the approved_collections, just as desired. But the third query loads every single CollectionItem in one of the work's approved collections (not just the work's CollectionItems). This means that if you have a single work w in a collection with thousands of collection items, calling w.approved_collections will load every one of those thousands of CollectionItems.

This shouldn't happen. The third query is the result of a mistake in the definition of approved_collections, and should disappear when the definition is fixed.

Note that this also affects user_approved_collections and rejected_collections.

Testing instructions

  1. Go into the rails console.

  2. Make sure that ActiveRecord::Base.logger.level is set low enough that you see SQL queries.

  3. Find a work with at least one approved collection.

  4. Call Work.find(id).approved_collections, and make sure that it results in exactly two database queries.

Activity

Show:
Sarken
November 12, 2019, 9:15 AM

Checks out!

 

redsummernight
November 13, 2019, 6:15 AM

Checked beta again, there are 3 queries: Work Load, Collection Load, CollectionItem Load.

On staging:

Looks good!

redsummernight
November 13, 2019, 6:19 AM

user_approved_collections and rejected_collections also look good, generating just 2 queries for Work Load and Collection Load.

DeployedToBeta

Assignee

ticking instant

Reporter

ticking instant

Roadmap

Collections

Priority

Medium

Affects versions

Fix versions

Components

BackEnd

Difficulty

Medium

Milestone

Internal 0.9
Configure