简体   繁体   中英

Oracle 10g - Using max on date column that is both partition and index

I have a table that is both partitioned by and indexed on a DATE column d_dly_label .

I'm trying to understand the impact of this on max(d_dly_label) . Why does it produce different values for the below two queries:

Query 1:

select 
  max(d_dly_label) 
from 
  ECP.TSPT105
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy');
--result: 13/JUL/14

Query 2:

select 
  max(d_dly_label) 
from 
  ECP.TSPT105
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy')
  and d_dly_label >= to_date('12/06/2015', 'dd/mm/yyyy') - 1;
--result: 11/JUN/15

How does adding more conditions to the where clause result in a higher max(d_dly_label) ?


UPDATE: I've grabbed the DDL from SQL Developer and cut it down into a short example that replicates the issue.

drop table tmp_105_copy;

CREATE TABLE tmp_105_copy
(
  D_DLY_LABEL DATE NOT NULL 
)
PARTITION BY LIST (D_DLY_LABEL) 
(
  PARTITION JUL2014_13 VALUES (TO_DATE(' 2014-07-13 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) 
  , PARTITION JUN2015_11 VALUES (TO_DATE(' 2015-06-11 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) 
);

CREATE INDEX tmp_I1_TSPT105 ON tmp_105_copy(D_DLY_LABEL ASC) 
LOCAL 
(
  PARTITION JUL2014_13 
, PARTITION JUN2015_11 
);

insert into tmp_105_copy values (to_date('13jul2014','ddmonyyyy'));
insert into tmp_105_copy values (to_date('11jun2015','ddmonyyyy'));

commit;

select 
  max(d_dly_label) 
from 
  tmp_105_copy
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy');
-- 13/JUL/14

analyze table tmp_105_copy estimate statistics;  

select 
  max(d_dly_label) 
from 
  tmp_105_copy
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy');
-- still 13/JUL/14

select 
  max(d_dly_label) 
from 
  tmp_105_copy
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy')
  and d_dly_label >= to_date('12/06/2015', 'dd/mm/yyyy') - 1;
--11/JUN/15

select 
  max(to_date(d_dly_label))
from 
  tmp_105_copy
where 
  d_dly_label <= to_date('12/06/2015', 'dd/mm/yyyy');
-- also 11/JUN/15

Maybe this is an indexed field in a table? If statistics are wrong, such incorrect results can appear. You can try to update the statistics of table and check your query again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM