https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=23260

--- Comment #242 from Martin Renvoize (ashimema) 
<[email protected]> ---
Created attachment 192232
  -->
https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=192232&action=edit
Bug 23260: (QA follow-up) Optimize anonymize_last_borrowers to avoid N+1
queries

This commit refactors Koha::Patrons->anonymize_last_borrowers() to use
a single bulk UPDATE instead of looping through patrons individually.

Performance Problem:
-------------------
The original implementation had a severe N+1 query problem:
1. Query all patrons with items_last_borrower records
2. Loop through each patron (First N+1)
3. For each patron, search their items_last_borrowers (Second N+1)
4. Update each result set

For a library with 10,000 patrons and 100,000 items, this would
execute 20,000+ database queries when run as a cronjob.

The Fix:
--------
Query the items_last_borrower table directly with all conditions
in a single search, then perform one bulk UPDATE:

  $schema->resultset('ItemsLastBorrower')->search({...})->update({...})

This reduces execution from potentially thousands of queries to a
single SQL UPDATE statement, regardless of data volume.

Why This Pattern:
-----------------
While Koha::Objects->update() exists, it loops through records
individually if the object class has update()/store() methods,
unless {no_triggers => 1} is passed. Working directly with the
resultset guarantees bulk update behavior.

This pattern is used throughout Koha for bulk operations (see
Koha/Item.pm, Koha/Hold.pm, admin/koha2marclinks.pl, etc).

Testing:
--------
prove t/db_dependent/Koha/Patrons.t

All existing tests pass, confirming identical behavior with
dramatically improved performance.

Signed-off-by: Martin Renvoize <[email protected]>

-- 
You are receiving this mail because:
You are watching all bug changes.
_______________________________________________
Koha-bugs mailing list
[email protected]
https://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-bugs
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to