[Solved] Performance Tuning of SQL query for DB2 Z/oS with like predicate(Pattern Match)

EverSQL Database Performance Knowledge Base

Performance Tuning of SQL query for DB2 Z/oS with like predicate(Pattern Match)

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.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid LIKE Searches With Leading Wildcard (query line: 15): The database will not use an index when using like searches with a leading wildcard (e.g. '%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  2. Avoid LIKE Searches With Leading Wildcard (query line: 20): The database will not use an index when using like searches with a leading wildcard (e.g. '%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  3. Avoid LIKE Searches With Leading Wildcard (query line: 25): The database will not use an index when using like searches with a leading wildcard (e.g. '%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  4. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `INDIVIDUAL_DATA` ADD INDEX `individual_data_idx_dob_v_dob` (`DOB`,`V_DOB`);
The optimized query:
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 '%'
            )
        )

Related Articles



* original question posted on StackOverflow here.