Please help me in tuning the performance of below query or suggest any alternate logic.
Select FNAME, MNAME, SURNAME, DOB, ADDRESS, PHONE
from INDIVIDUAL_DATA
WHERE DOB = V_DOB
AND (SURNAME = V_SURNAME
OR (SURNAME LIKE '%' || ' ' || V_SURNAME)
OR (SURNAME LIKE V_SURNAME || ' ' || '%')
OR (SURNAME LIKE '%' || ' ' ||
V_SURNAME || ' ' || '%'));
V_SURNAME is variable having surname input and V_DOB is having input for DOB(Date Of Birth). I have created an index using SURNAME and DOB column. This query is part of an Stored Procedure. and this query will be the first query which executes on invoking the Stored Procedure.
I have around 7 Million records in DB2 database on which this query will be executed.We are facing performance issue, its taking much time. I suspect because of Like Predicate with OR operator is the cause of issue. This logic is implemented to perform Pattern search. Please have a look into below test cases :
case 1.
DOB= 1992-10-10 and SURNAME = 'ALEX MATHEWS'
V_DOB = '1992-10-10' and SURNAME = 'ALEX'
this should find a positive match.
case 2.
DOB= 1965-05-10 and SURNAME = 'FRANKLIN JERRY'
V_DOB = '1965-05-10' and V_SURNAME = 'FRANK'
This should not be fetched. its a negative case.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `INDIVIDUAL_DATA` ADD INDEX `individual_data_idx_dob_v_dob` (`DOB`,`V_DOB`);
SELECT
INDIVIDUAL_DATA.FNAME,
INDIVIDUAL_DATA.MNAME,
INDIVIDUAL_DATA.SURNAME,
INDIVIDUAL_DATA.DOB,
INDIVIDUAL_DATA.ADDRESS,
INDIVIDUAL_DATA.PHONE
FROM
INDIVIDUAL_DATA
WHERE
INDIVIDUAL_DATA.DOB = INDIVIDUAL_DATA.V_DOB
AND (
INDIVIDUAL_DATA.SURNAME = INDIVIDUAL_DATA.V_SURNAME
OR (
INDIVIDUAL_DATA.SURNAME LIKE '%'
OR ' '
OR INDIVIDUAL_DATA.V_SURNAME
)
OR (
INDIVIDUAL_DATA.SURNAME LIKE INDIVIDUAL_DATA.V_SURNAME
OR ' '
OR '%'
)
OR (
INDIVIDUAL_DATA.SURNAME LIKE '%'
OR ' '
OR INDIVIDUAL_DATA.V_SURNAME
OR ' '
OR '%'
)
)