建立索引: user_id, create_date 可create_date带有时间: 2008-01-25 09:16:15 查询时加了函数TRUNC 会不会影响查询速度? 该如何解决? ------------------------------- create table jax_t1 as select rownum user_id,sysdate create_date from dba_objects where rownum < 100;如果你建立的是复合索引, create index jax_t1_Ind1 on jax_t1(user_id,create_date); 1,检索谓词中如果没有user_id的话,不会使用该索引; select * from jax_t1 where create_date = trunc(sysdate) SELECT STATEMENT, GOAL = CHOOSE TABLE ACCESS FULL 对象所有者=SYS 对象名称=JAX_T1 2,如果检索谓词出现user_id,并且使用了trunc格式化索引的第二个字段,那么执行时会对索引进行范围扫描。这种情况下对第二个字段create_date是否使用函数的唯一区别在于不使用函数时可以准确定位,而使用函数必须对满足第一个条件的所有索引块逐一扫描,在user_id重复记录较少的情况下几乎没什么影响。 select * from jax_t1 where user_id = 20 and create_date = trunc(sysdate) SELECT STATEMENT, GOAL = CHOOSE INDEX RANGE SCAN 对象所有者=SYS 对象名称=JAX_T1_IND1 如果你是单独建立的索引 create index jax_t1_Ind2 on jax_t1(user_id); create index jax_t1_Ind3 on jax_t1(create_date); 这时的影响就会较大,如果你在create_date上使用函数,那么会导致无法使用对应索引。可能的解决方式是,建立函数索引。 select * from jax_t2 where trunc(create_date) = trunc(sysdate)
如果要查询具体的天(不带时间),用TRUNC , to_char都差不多.
可create_date带有时间: 2008-01-25 09:16:15
查询时加了函数TRUNC 会不会影响查询速度? 该如何解决?
-------------------------------
create table jax_t1
as
select rownum user_id,sysdate create_date from dba_objects where rownum < 100;如果你建立的是复合索引,
create index jax_t1_Ind1 on jax_t1(user_id,create_date); 1,检索谓词中如果没有user_id的话,不会使用该索引;
select * from jax_t1 where create_date = trunc(sysdate) SELECT STATEMENT, GOAL = CHOOSE
TABLE ACCESS FULL 对象所有者=SYS 对象名称=JAX_T1
2,如果检索谓词出现user_id,并且使用了trunc格式化索引的第二个字段,那么执行时会对索引进行范围扫描。这种情况下对第二个字段create_date是否使用函数的唯一区别在于不使用函数时可以准确定位,而使用函数必须对满足第一个条件的所有索引块逐一扫描,在user_id重复记录较少的情况下几乎没什么影响。
select * from jax_t1 where user_id = 20 and create_date = trunc(sysdate) SELECT STATEMENT, GOAL = CHOOSE
INDEX RANGE SCAN 对象所有者=SYS 对象名称=JAX_T1_IND1
如果你是单独建立的索引
create index jax_t1_Ind2 on jax_t1(user_id);
create index jax_t1_Ind3 on jax_t1(create_date);
这时的影响就会较大,如果你在create_date上使用函数,那么会导致无法使用对应索引。可能的解决方式是,建立函数索引。
select * from jax_t2 where trunc(create_date) = trunc(sysdate)
象你这样个表如果user_id已经是主键,那么可能考虑单独对create_date创建索引,
如果user_id不是主键,可以创建user_id,create_date 联合主键
create index idx_trunc on yourTable(trunc(create_date));
(user_id, TRUNC(create_date));谢谢大家!