midrange.com code scratchpad
Name:
Sample SPL
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
04/20/2021 11:46:07 am
IP:
Logged
Description:
Sample SQL stored procedure.
You could store this in a source member and use RUNSQLSTM to "compile" it.
Code:
  1. -- category: SPL
  2. -- description: Submit jobs to purge deleted rows 
  3. -- Cycle through a list of partitions (members) in a table with numerous deleted rows and submit
  4. -- a job to RGZPFM those deleted rows out, if there are no locks on that member.
  5. -- Cautions:
  6. --   One:  If you rely upon relative record number (RRN) then you should not use RGZPFM on that table.
  7. --      Not normally a technique often used anymore.
  8. --  Two:  If someone tries to access the file while it is being reorganized it will be locked.
  9. --  Three:  There are ways to "reorg while active" but it has side effects you need to understand.
  10. --    I did not try reorg while active for this procedure.
  11.  
  12. -- Getting the list of members from syspartitionstat is pretty efficient.
  13. -- Adding the process to count locks on that member does add some run time
  14. -- The following got done submitting the jobs to the job queue in 10.62 minutes on my system
  15. -- Power 9 9009-42A lpar running IBM i 7.4 using SSD's hosted by another lpar of IBM i.  
  16. -- Development only lpar with few users on it.
  17.  
  18. CREATE PROCEDURE REMOVE_DELETED_ROWS
  19.         LANGUAGE SQL
  20.         MODIFIES SQL DATA
  21.         SET OPTION DATFMT = *ISO
  22. P1: BEGIN
  23.     DECLARE WORK_SCHEMA_NAME CHAR(10);
  24.     DECLARE WORK_TABLE_NAME CHAR(10);
  25.     DECLARE WORK_MEMBER_NAME CHAR(10);
  26.     DECLARE COMMAND CHAR(200); -- Ensure this is large enough.
  27.     DECLARE END_TABLE INT DEFAULT 0;
  28.     DECLARE C1 CURSOR FOR WITH T1 AS (
  29.                                    SELECT system_table_schema,
  30.                                           system_table_name,
  31.                                           system_table_member,
  32.                                           number_deleted_rows,
  33.                                           number_deleted_rows * avgrowsize AS deleted_space,
  34.                                           avgrowsize
  35.                                        FROM qsys2.syspartitionstat
  36.                                        WHERE number_deleted_rows > 0
  37.                                ),
  38.                                t2 AS (
  39.                                    SELECT t1.*,
  40.                                           number_of_locks
  41.                                        FROM T1,
  42.                                             LATERAL (
  43.                                                 SELECT COUNT(*)
  44.                                                     FROM qsys2.object_lock_info l
  45.                                                     WHERE t1.system_table_schema = l.system_object_schema
  46.                                                           AND t1.system_table_name = l.system_object_name
  47.                                                           AND t1.system_table_member = l.system_table_member
  48.                                                           AND l.object_type = '*FILE'
  49.                                             ) L1 (number_of_locks)
  50.                                )
  51.         SELECT t2.system_table_schema,
  52.                t2.system_table_name,
  53.                t2.system_table_member
  54.             FROM t2
  55.             WHERE t2.number_of_locks = 0
  56.             ORDER BY t2.deleted_space DESC
  57.             FETCH first 100 ROWS ONLY -- Change for your situation
  58.     ;
  59.     DECLARE CONTINUE HANDLER FOR NOT FOUND SET END_TABLE = 1;
  60.     --  DECLARE EXIT HANDLER FOR SQLEXCEPTION
  61.     --    SET <insert column here> = NULL;
  62.     OPEN C1;
  63.     FETCH C1 INTO work_schema_name,
  64.                   work_table_name,
  65.                   work_member_name;
  66.     WHILE END_TABLE = 0 DO
  67.         SET COMMAND = 'SBMJOB CMD(RGZPFM FILE(' CONCAT TRIM(work_schema_name) CONCAT '/' CONCAT TRIM(work_table_name) CONCAT
  68.                     ') MBR(' CONCAT TRIM(work_member_name) CONCAT ')) JOB(' CONCAT TRIM(work_member_name) CONCAT
  69.                     ') JOBQ(ROB/RGZPFM)'; -- Adjust Job queue accordingly
  70.         CALL QSYS2.QCMDEXC(COMMAND);
  71.         FETCH C1 INTO work_schema_name,
  72.                       work_table_name,
  73.                       work_member_name;
  74.     END WHILE;
  75.     CLOSE C1;
  76. END P1          
  77. ;
  78. -- Test
  79. CALL ROB.REMOVE_DELETED_ROWS;         
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css