Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A part of the SQL stmt where the substring function in where clause is eating CPU time.

Can I rearrange the below query in any other form?

SELECT ELIG_TYP FROM TABLE1
WHERE COMPANY_CD IN
(SELECT SUBSTR(CMPNYID, 1, 4) FROM TABLE2 WHERE USR = 'Badshah')

Pls Reply

What I have tried:

SELECT ELIG_TYP FROM TABLE1 WHERE COMPANY_CD IN (SELECT SUBSTR(CMPNYID, 1, 4) FROM TABLE2 WHERE USR = 'Badshah')
Posted
Updated 15-Aug-17 23:44pm
Comments
Member 12613458 16-Aug-17 1:43am    
The Database is DB2

Hi,

Check these steps...if data in your tables is in huge.

1. 1st Fetch your data from table 2 with required WHERE conditions in temp table.
2. Make your desired Select query with this temp table by using Substring.


If required i can post query in SQL server.


Cheers
 
Share this answer
 
SELECT
t1.ELIG_TYP
FROM
TABLE1 t1
inner join
TABLE2 t2 on(t1.COMPANY_CD=left(t2.CMPNYID,4)) WHERE t2.USR ='Badshah';
 
Share this answer
 
Comments
Member 12613458 16-Aug-17 6:28am    
There is no difference at all between left and substring because left is translated to substring in the execution plan.
Santosh kumar Pithani 16-Aug-17 7:37am    
OK but sql code execution plan is fast while using joins so left function doesn't affect CPU performance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900