I have two SQL statements whose performance I expect to be similar, but in fact SQL1 used 0.065 seconds and SQL2 used over 10 seconds with just 8000 records in total. Could anyone help to explain this? How can I optimize SQL2?
SQL 1:
select
job_id,
JOB_DESCRIPTION,
REGEXP_COUNT(JOB_Description, '(ABC|DEF)([[:digit:]]){5}') as occurrences
from smms.job
where TO_NUMBER(to_char(CREATE_DATE,'YYYY')) = 2017;
SQL 2:
select job_id, JOB_Description
from (
select
job_id,
JOB_DESCRIPTION,
REGEXP_COUNT(JOB_Description, '(ABC|DEF)([[:digit:]]){5}') as occurrences
from smms.job
where TO_NUMBER(to_char(CREATE_DATE,'YYYY')) = 2017
)
where occurrences > 0;
occurrencesis not used in the final select list, so I don't see why you don't just useREGEXP_COUNTdirectly in theWHEREclause. i.e.SELECT job_id, job_description FROM sims.job WHERE TO_NUMBER... =2017 AND REGEXP_COUNT... > 0TO_NUMBER(to_char(CREATE_DATE,'YYYY'))=2017would be changed toCREATE_DATE >= DATE'2017-01-01' AND CREATE_DATE < DATE'2018-01-01'?