Bug 33627 - OAI-PMH script build_oai_sets.pl is slow and memory hungry
Summary: OAI-PMH script build_oai_sets.pl is slow and memory hungry
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Searching (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Bugs List
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-04-27 12:01 UTC by Didier Gautheron
Modified: 2026-01-15 04:04 UTC (History)
3 users (show)

See Also:
GIT URL:
Initiative type: ---
Sponsorship status: ---
Comma delimited list of Sponsors:
Crowdfunding goal: 0
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Didier Gautheron 2023-04-27 12:01:30 UTC
Bug 15108 optimize set search but there's the same issue in:
./misc/migration_tools/build_oai_sets.pl
Comment 1 David Cook 2025-04-03 02:30:20 UTC
My frustration around speed/memory use are what caused me to write the patch for bug 37486

But even then... it's still very slow and memory hungry.

At a glance, we're fetching the entire database of metadata (deleted and active records) into an array instead of using an iterator, and the only reason we're doing that is do get a count of the array entries. 

An easy first step optimization would be to first do a count() query and then do the query itself. Database level caching should make that reasonably quick.

That should improve memory usage, although I don't know about speed. In theory, that might actually be slower since it'll have to fetch the records one by one from the database result set. Although it might be faster since it won't have to spend time sitting trying to allocate a big enough chunk of memory to manage everything. Time will tell about time I guess.

--

Anyway, just a thought for first steps.
Comment 2 David Cook 2025-04-03 02:30:57 UTC
I don't have to build sets often, so I'm probably not going to work on this in the short-term... but it would be great to improve this one day...
Comment 3 David Cook 2025-04-03 02:34:51 UTC
I was thinking we could also add parallel processing. That would improve speed but also increase memory usage.
Comment 4 David Cook 2025-04-03 02:39:07 UTC
Another idea for build_oai_sets.pl specifically... adding a --where option.

Typically, this script is only needed for the initial population of an OAI-PMH set, so we fetch everything and compare it to the mappings. 

However, especially with bug 37486, we can probably put together a SQL WHERE clause which helps us reduce - at the database level - the number of records to consider for the mappings.

For instance, I only want bib records that have items for branch A. I don't need all biblio_metadata and deletedbiblio_metadata. I could add a WHERE clause that dramatically reduces the number of metadata records fetched from the database.
Comment 5 Aleisha Amohia 2026-01-08 21:52:16 UTC
Improving the performance of this script would be of interest to us.

Another idea Alex had was to have a table of the records in the OAI set, being added to as creation/deletion actions were taken, then to clear it out regularly. Not sure if I'm describing that right, but must be faster than trying to filter on timestamps and fetch everything altogether.
Comment 6 Katrin Fischer 2026-01-09 16:49:00 UTC
(In reply to Aleisha Amohia from comment #5)
> Improving the performance of this script would be of interest to us.
> 
> Another idea Alex had was to have a table of the records in the OAI set,
> being added to as creation/deletion actions were taken, then to clear it out
> regularly. Not sure if I'm describing that right, but must be faster than
> trying to filter on timestamps and fetch everything altogether.

Would this be like a "queue" table? Just wondering if it's different to OAI-PMH:AutoUpdateSets or if that could cover it.
Comment 7 Alex Buckley 2026-01-15 04:04:34 UTC
(In reply to Katrin Fischer from comment #6)
> (In reply to Aleisha Amohia from comment #5)
> > Improving the performance of this script would be of interest to us.
> > 
> > Another idea Alex had was to have a table of the records in the OAI set,
> > being added to as creation/deletion actions were taken, then to clear it out
> > regularly. Not sure if I'm describing that right, but must be faster than
> > trying to filter on timestamps and fetch everything altogether.
> 
> Would this be like a "queue" table? Just wondering if it's different to
> OAI-PMH:AutoUpdateSets or if that could cover it.

Hey Katrin, 

What I'm thinking of is, either, a new database table or indeed adding a new timestamp column to the existing oai_sets_biblios table.

With the purpose of optimising when Koha generates the OAI-PMH content, for harvest by other systems.

---

Context:

Currently the Koha/OAI/Server/ListBase.pm->GetRecords() function fetches data from a lot of different database tables to generate a list of records to display on the Koha OAI-PMH page. 

The UNIONS and other data processing (DISTINCT, ORDER BY, LIMIT) across multiple database tables in the SQL queries slow how long it takes to generate the record list.

For libraries which include items in their OAI-PMH content, the SQL query does a UNION and fetches data from 4 database tables: oai_sets_biblios, biblio_metadata, deleteditems, items. See https://git.koha-community.org/Koha-community/Koha/src/branch/main/Koha/OAI/Server/ListBase.pm#L98-L120

---

Real world example:

We have a partner library, who does include items in their OAI config. This library undertakes a lot of record imports/updates, making their VuFind harvests from Koha OAI-PMH large.

This library has encountered failed OAI-PMH harvests from Koha because out timeouts on the Koha OAI-PMH side. 

We debugged and found the Koha/OAI/Server/ListBase.pm->GetRecords() SQL query took a very significant amount of time to complete in the database. In one case, it took 27 minutes to run, despite the database being fully configured and optimised as per community recommendations.

---

Ideal solution:

If the timestamp information can be stored partially, or in full, in a new oai_sets_biblios.timestamp table. Then the SQL queries in https://git.koha-community.org/Koha-community/Koha/src/branch/main/Koha/OAI/Server/ListBase.pm#L98-L120 could fetch data from fewer tables, thereby optimising how how the OAI-PMH page takes to generate.

I hope this helps clarify our thinking?