Performance improvement to evergreen.ranked_volumes() database function.
authorhubert depesz lubaczewski <depesz@depesz.com>
Thu, 3 Oct 2013 18:26:33 +0000 (14:26 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Thu, 3 Oct 2013 20:39:45 +0000 (16:39 -0400)
commit3bf9f947a57e3621724c9a22441a47e9b9cf682d
treec8ee3ac46000e7aed73e561695402fd14301fc74
parentba743246932dd7586d43225f8efeeabcaac9ba33
Performance improvement to evergreen.ranked_volumes() database function.

For the client I analyzed logs from production Pg instance for ~ 10
days.

During this time, the single most time consuming query (summarized time
for all instances of the query, with different parameters) was:

SELECT * FROM unapi.bre ( ... ) AS "unapi.bre";

I profiled this function, and found that in my test case most of the
time (2.04s out of 2.06s, so ~ 99%) was spent in call to
unapi.holdings_xml() function.

When I profiled this function, I found that most of the time (sorry,
don't have the number now with me) was spent in call to
evergreen.ranked_volumes() function.

At this moment in my research something changed on the server I was
testing on, and all subsequent times were ~ 4-5 times lower, but the
ratios were more or less the same.

Anyway - call to evergreen.ranked_volumes() showed repeatable time (with
full caches/buffers) of ~ 380ms.

I modified the function by:
1. inlining actor.org_unit_descendants(?, ?)
2. inlining evergreen.rank_ou(?, ?, ?)
3. extracting depth calculation to separate call
4. switched to plpgsql (which gives me ability to use variables)
5. removed evergreen.rank_ou() and evergreen.rank_cp_status() from
   select clause - these are still in WINDOW definition, but they
   weren't used in the SELECT, so it's better to remove from there.
6. in passing renamed arguments to avoid name clash (argument depth vs.
   field depth)
7. in passing changed usage of $* to access parameters to using named
   parameters, for readability.

New function did the same work in ~ 18ms.
Open-ILS/src/sql/Pg/990.schema.unapi.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.function.ranked_volumes.sql [new file with mode: 0644]