Changed to make single circ requests faster collab/miker/faster_bibs_by_item_age_depesz
authorLiam Whalen <whalen.ld@gmail.com>
Wed, 30 Oct 2013 03:13:38 +0000 (20:13 -0700)
committerLiam Whalen <whalen.ld@gmail.com>
Wed, 30 Oct 2013 03:13:38 +0000 (20:13 -0700)
Added a check of the a_circ_libs paramter to see if the number of circ
libs is greater than 1, and if it is not, then exclude skeys check and
exclude calculating v_oldest.

Signed-off-by: Liam Whalen <whalen.ld@gmail.com>
Open-ILS/src/sql/Pg/999.functions.global.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.function.bibs_by_item_age.sql

index 1574a05..1ac4231 100644 (file)
@@ -29,6 +29,7 @@ DECLARE
     v_oldest timestamptz := NUll;
     v_c_oldest timestamptz := NULL;
     v_key_count INT8;
+    v_circ_libs_size INT4;
 BEGIN
 
     IF array_length(p_circ_libs,1) > 0 THEN
@@ -37,15 +38,26 @@ BEGIN
         SELECT ARRAY_AGG(id) INTO a_circ_libs FROM actor.org_unit;
     END IF;
 
+    v_circ_libs_size := array_length(a_circ_libs, 1);
+
     FOREACH v_circ_lib IN ARRAY a_circ_libs LOOP
         v_found := 0;
         v_seen := '';
         v_c_oldest := NULL;
 
+      
+        -- If there is only one circ lib in the array then we do not need
+        -- to get the key count, so set v_key_count to 0 to force second
+        -- query.
+        IF v_circ_libs_size > 1 THEN
+            SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
+        ELSE
+            v_key_count = 0;
+        END IF;
+
         -- If the number of keys in v_results is greater than or equal to
         -- the number of results we need for the current page, then only look
         -- for records that are newer than items currently in v_results
-        SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
         IF v_key_count >= p_limit + p_offset THEN
             open v_cursor NO SCROLL FOR
                 SELECT c.call_number, c.create_date, c.status, c.location
@@ -101,7 +113,8 @@ BEGIN
         CLOSE v_cursor;
 
         -- Update oldest information based on oldest row added in current loop
-        IF v_oldest IS NULL OR v_oldest < v_c_oldest THEN
+        -- only if we have more than one circ lib
+        IF (v_oldest IS NULL OR v_oldest < v_c_oldest) AND v_circ_libs_size > 1 THEN
             v_oldest := v_c_oldest;
         END IF;
 
index 131a055..96a0d58 100644 (file)
@@ -17,6 +17,7 @@ DECLARE
     v_oldest timestamptz := NUll;
     v_c_oldest timestamptz := NULL;
     v_key_count INT8;
+    v_circ_libs_size INT4;
 BEGIN
 
     IF array_length(p_circ_libs,1) > 0 THEN
@@ -25,15 +26,26 @@ BEGIN
         SELECT ARRAY_AGG(id) INTO a_circ_libs FROM actor.org_unit;
     END IF;
 
+    v_circ_libs_size := array_length(a_circ_libs, 1);
+
     FOREACH v_circ_lib IN ARRAY a_circ_libs LOOP
         v_found := 0;
         v_seen := '';
         v_c_oldest := NULL;
 
+      
+        -- If there is only one circ lib in the array then we do not need
+        -- to get the key count, so set v_key_count to 0 to force second
+        -- query.
+        IF v_circ_libs_size > 1 THEN
+            SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
+        ELSE
+            v_key_count = 0;
+        END IF;
+
         -- If the number of keys in v_results is greater than or equal to
         -- the number of results we need for the current page, then only look
         -- for records that are newer than items currently in v_results
-        SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
         IF v_key_count >= p_limit + p_offset THEN
             open v_cursor NO SCROLL FOR
                 SELECT c.call_number, c.create_date, c.status, c.location
@@ -89,7 +101,8 @@ BEGIN
         CLOSE v_cursor;
 
         -- Update oldest information based on oldest row added in current loop
-        IF v_oldest IS NULL OR v_oldest < v_c_oldest THEN
+        -- only if we have more than one circ lib
+        IF (v_oldest IS NULL OR v_oldest < v_c_oldest) AND v_circ_libs_size > 1 THEN
             v_oldest := v_c_oldest;
         END IF;