我们有一个问题,包括(但还有其他加入、表格和条件):
SELECT
o.contact_id,
o.completed_date,
o.submitted_date
FROM
orders o /* 860,000 row table */
WHERE
? <= o.submitted_date
AND o.submitted_date < ? + 1
called诉。
<代码>? paramaters允许核对两个日期之间的订单。
然而,这种询问进展缓慢。
我们从PL/SQL转来测试如下:
SubmittedDateFrom date:=to_date( 2011-07-15 , yyyy-mm-dd );
SubmittedDateTo date:=to_date( 2011-07-15 , yyyy-mm-dd );
CURSOR c_orgs IS
SELECT
o.contact_id,
o.completed_date,
o.submitted_date
FROM
orders o
WHERE
SubmittedDateFrom <= o.submitted_date
AND o.submitted_date < SubmittedDateTo + 1;
BEGIN
FOR c_o IN c_orgs LOOP
DBMS_OUTPUT.put_line( Submitted date = ||c_o.submitted_date);
END LOOP;
END;
IF we either:
- convert the
SubmittedDateTo
value toto_date( 2011-07-16 , yyyy-mm-dd )
(i.e. do the arithmetic outside the query), - make
SubmittedDateTo
a string and use "to_date( SubmittedDateTo , yyyy-mm-dd )
+ 1" as the second condition in theWHERE
.
THEN, the query speeds up dramatically ( < 1 second vs. 44+ seconds).
Further information:
- running an explain plan on the query gives the error
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
- the
submitted_date
column has an index and stats, etc. have been run - wrapping the
SubmittedDateTo + 1
in atrunc()
call does not affect performance - We do not have a non-9i database with similar data volumes, etc. to test if it s the version of Oracle or not.
The question is: we are unable to find any information which clearly states that the Oracle 9i Optimizer has an issue with this kind of date arithmetic. Is that what s happening here or is there something else going on?