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)
committerMike Rylander <mrylander@gmail.com>
Wed, 19 Feb 2014 19:58:02 +0000 (14:58 -0500)
commite70bb03ce4188bd8e292042c1a445c507cd3b308
tree24630b2448259a9d6663cd81ffbbbb26d89ec99b
parentc0c10611404f55ef1d1146de053854b17e9b66ab
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]