Need your advice on the below scenario.
An old COBOL program has SELECT SQL where it has multiple WHERE clauses. But these WHERE clauses don't act together. Depending on variable values a single WHERE condition is switched on and others are ignored.
SQL Query:
SELECT EMP_ID, EMP_SSN, EMP_PH, EMP_SALARY, EMP_EMAIL
INTO <:DCLGEN variable1, DCLGEN variable2...variable5>
FROM <Table name>
WHERE ((EMP_ID = <:HOST Var1> OR 'N' = :HOST switch1)
AND
(EMP_SSN = <:HOST Var2> OR 'N' = :HOST switch2)
AND
(EMP_PH = <:HOST Var3> OR 'N' = :HOST switch3)
AND
(EMP_EMAIL = <:HOST Var4> OR 'N' = :HOST switch4))
Now in the above query as you can see there are 4 WHERE conditions connected with AND.
In the program, when we want to run the first condition (EMP_ID) under WHERE clause we are passing SPACE to HOST switch1 and 'N' to rest all three. Similarly when we want to activate the 3rd condition (EMP_PH) then SPACE to HOST switch3 and 'N' to rest all three and so on.
Now we are facing a performance issue with this query. The reason is, though we have 4 indexes (all 4 columns in WHERE condition) but because of these HOST Switch variables DB2 is doing TS space instead of Index scan. So we need to rewrite the query and we can not use this kind of HOST Switch variables in the query.
- One solution is to remove all HOST switch variables and break the query into 4 diff queries. But that program will be lengthy and lots of code changes needed. So we are trying to avoid this.
Could someone please share suggestions on how we can achieve this within a single SQL query without using the above kind of Host Switch Variables?
NOTE- Due to business logic we need to use specific WHERE conditions depending on certain variable values.
I broke the query into 4 parts (each query with single WHERE condition) and removed all HOST switch variables. Depending on the business condition I am calling each SQL query. But that we are trying to avoid because the whole program needs to be changed to accomodate this new approach.