I have the following DB2 stored procedure that allows the user to specify a variable number of parameters (specifying NULL for parameters they don't want to provide), where each parameter can be used to filter records in a WHERE clause.
CREATE PROCEDURE XX.DUMMY(IN I_FIRST_NAME VARCHAR(32),
I_LAST_NAME VARCHAR(100),
I_COMPANY_NAME VARCHAR(100))
SPECIFIC XX.DUMMY
RESULT SETS 1
MODIFIES SQL DATA
NOT DETERMINISTIC
NULL CALL
LANGUAGE SQL EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN
DECLARE RESULTS CURSOR WITH RETURN TO CLIENT FOR
WITH RELEVANT_RECORDS (RECORD_ID) AS (
SELECT RECORD_ID
FROM XX.RECORDS R
WHERE (I_FIRST_NAME IS NULL OR R.SEARCH_FIRST_NAME = I_FIRST_NAME)
AND (I_LAST_NAME IS NULL OR R.SEARCH_LAST_NAME = I_LAST_NAME)
AND (I_COMPANY_NAME IS NULL OR R.SEARCH_COMPANY_NAME = I_COMPANY_NAME)
WITH UR
)
SELECT RR.RECORD_ID, RD.*
FROM RELEVANT_RECORDS RR
JOIN XX.RECORD_DETAILS RD ON RD.RECORD_ID = RR.RECORD_ID
FOR READ ONLY WITH UR;
OPEN RESULTS;
END
;
It's my understanding that this procedure will perform very poorly because the procedure's query plan will only be evaluated when the procedure is bound (or ran the first time?), where if different combinations of parameters are specified in later executions the original query plan will be used, resulting in the wrong plan/indexes being used (thus making the proc very slow).
I read that SQL Server allows you to specify OPTION(RECOMPILE) in the procedure's definition to tell the server to re-evaluate query plan(s) for each execution of the procedure, however since I'm using DB2 I'm looking for a DB2 equivalent.
Is there a way in DB2 that you can tell a stored procedure to re-evaluate it's query plan each execution? Do I have to use something like SET CURRENT QUERY OPTIMIZATION inside my procedure before any statements are ran?