My main goal with this question is optimization and faster run time.
After doing lot of processing in the Stored Proc I finally return a count like below:
OPEN cv_1 FOR
SELECT COUNT(*) num_of_members
FROM HOUSEHOLD_MEMBER a,
HOUSEHOLD b
WHERE RTRIM(LTRIM(a.mbr_last_name)) LIKE v_MBR_LAST_NAME || '%'
AND a.number = '01'
AND a.code = v_CODE
AND a.ssn_head = v_SSN_HEAD
AND TO_CHAR( a.mbr_dob, 'MM/DD/YYYY') = v_DOB;
But in my code that is calling the SP does not need the actual count. It just cares that count is greater than 1.
Question:
- How can I change this to return just 1 or 0. 1 when count is > 0 and 0 when count > 1.
- Will it be faster to do this rather than returning the whole count?
I would not have thought that introducing a filter on the count(*) would help with performance, but as has already been said, you can do this with a HAVING clause.
Your biggest bottleneck will probably be with your joins.
This line is not going to help. Whenever you start performing functions of fields, it has trouble using the correct indexes, so if you can avoid trimming the name, that may help.
This looks like the biggest problem. If v_DOB could be converted to a date before the query, that would help. Alternatively, I think that switching the statement to use a TO_DATE instead may help.
The best way to optimize this is to get the explain plan: