Bug 22813

Summary: searchResults queries the Koha::Patron object inside two nested loops
Product: Koha Reporter: Tomás Cohen Arazi <tomascohen>
Component: Architecture, internals, and plumbingAssignee: Tomás Cohen Arazi <tomascohen>
Status: CLOSED FIXED QA Contact: Martin Renvoize <martin.renvoize>
Severity: normal    
Priority: P5 - low CC: jonathan.druart, josef.moravec, lucas, martin.renvoize, nick
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: Trivial patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
19.05.00, 18.11.06
Attachments: Bug 22813: remove repetitive queries inside two nested loops in searchResults
Bug 22813: remove repetitive queries inside two nested loops in searchResults
Bug 22813: (follow-up) Add missing condition
Bug 22813: remove repetitive queries inside two nested loops in searchResults
Bug 22813: (follow-up) Add missing condition

Description Tomás Cohen Arazi 2019-04-30 15:22:04 UTC
I haven't benchmarked this, but it seems really bad and easy to fix.

To verify:
Go to C4::Search:2119, notice the IF includes a query for a Koha::Patron object, and then for its related Koha::Patron::Category object. This is done for each item, on each record in the resultset WTF
Comment 1 Tomás Cohen Arazi 2019-04-30 15:36:11 UTC
Created attachment 89137 [details] [review]
Bug 22813: remove repetitive queries inside two nested loops in searchResults

This patch moves a query on Koha::Patrons and then the related
Koha::Patron::Category that needlessly happens inside two nested loops
(all items of all MARC records in the resultset).

The Koha::Patron and Koha::Patron::Category are always the same as it is
fetched from C4::Context->userenv each time.

To test:
- Run:
  $ kshell
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests pass
- Apply this patch
- Run:
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests still pass!
- Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 2 Liz Rea 2019-04-30 18:02:53 UTC
Created attachment 89150 [details] [review]
Bug 22813: remove repetitive queries inside two nested loops in searchResults

This patch moves a query on Koha::Patrons and then the related
Koha::Patron::Category that needlessly happens inside two nested loops
(all items of all MARC records in the resultset).

The Koha::Patron and Koha::Patron::Category are always the same as it is
fetched from C4::Context->userenv each time.

To test:
- Run:
  $ kshell
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests pass
- Apply this patch
- Run:
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests still pass!
- Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Comment 3 Tomás Cohen Arazi 2019-04-30 18:49:37 UTC
Created attachment 89151 [details] [review]
Bug 22813: (follow-up) Add missing condition

This patch restores a missing condition. $patron gets renamed to make
it obvious that it related to the logged in user.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 4 Martin Renvoize 2019-05-02 10:58:27 UTC
Created attachment 89220 [details] [review]
Bug 22813: remove repetitive queries inside two nested loops in searchResults

This patch moves a query on Koha::Patrons and then the related
Koha::Patron::Category that needlessly happens inside two nested loops
(all items of all MARC records in the resultset).

The Koha::Patron and Koha::Patron::Category are always the same as it is
fetched from C4::Context->userenv each time.

To test:
- Run:
  $ kshell
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests pass
- Apply this patch
- Run:
 k$ prove t/db_dependent/Search.t
=> SUCCESS: Tests still pass!
- Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 5 Martin Renvoize 2019-05-02 10:58:30 UTC
Created attachment 89221 [details] [review]
Bug 22813: (follow-up) Add missing condition

This patch restores a missing condition. $patron gets renamed to make
it obvious that it related to the logged in user.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 6 Martin Renvoize 2019-05-02 10:59:14 UTC
Well spotted!

Passing QA
Comment 7 Nick Clemens 2019-05-07 12:03:02 UTC
Awesome work all!

Pushed to master for 19.05
Comment 8 Martin Renvoize 2019-05-08 15:36:59 UTC
Pushed to 18.11.x for 18.11.06
Comment 9 Lucas Gass 2019-05-30 13:52:07 UTC
backported to 18.05.x for 18.05.13