本帖最後由 xeon0541 於 2015-10-23 12:17 編輯
select max(patient_id) from patient_history
where record_date <= to_date(:p_cur_date, 'ddmmyyyy')
and patient_id = :pid);
the execution plan for above sql
Sort(AGGREGATE)
FIRST ROW
INDEX (RANGE SCAN(MIN/MAX)
access predicates
AND
PATEINT_ID=TO_NUMBER(:PID)
PRECORD_DATE<=TO_DATE(:P_CUR_DATE,'DDMMYYYY')
The index of PRECORD_DATE column is used.
Because the optimizer of database don't know your business logic, it always used wrong index or performing full scan table. That's why SQL performance tuning is needed. |