select /*+index_ffs(tn code索引名)*/
count(1)
from t_tablename tn
where tn.code like dep.depcode || '%'
and round(to_number(tn.date1-tn.date2)* 1440) > 10
and tn.date1< to_date('2009-12-30', 'yyyy-MM-dd') + 1
and tn.date1>= to_date('2009-12-1', 'yyyy-MM-dd')我在date1和code2个字段上都建有索引,可为什么,他只走date1索引,不走code的索引呢。
如果我把round(to_number(tn.date1-tn.date2)* 1440) > 10,这个条件给去掉,他就会走code和date1索引。
tn.code like dep.depcode || '%'我在写测试语句的时候,是用tn.code like ‘xx%’但是也不行。code字段里可能会有空值,但是为什么我去掉第二个条件的时候也会走Code索引呢。
这是为什么?
在线等。我现在的目的就是要他走2个索引,code 和 date1。
count(1)
from t_tablename tn
where tn.code like dep.depcode || '%'
and round(to_number(tn.date1-tn.date2)* 1440) > 10
and tn.date1< to_date('2009-12-30', 'yyyy-MM-dd') + 1
and tn.date1>= to_date('2009-12-1', 'yyyy-MM-dd')我在date1和code2个字段上都建有索引,可为什么,他只走date1索引,不走code的索引呢。
如果我把round(to_number(tn.date1-tn.date2)* 1440) > 10,这个条件给去掉,他就会走code和date1索引。
tn.code like dep.depcode || '%'我在写测试语句的时候,是用tn.code like ‘xx%’但是也不行。code字段里可能会有空值,但是为什么我去掉第二个条件的时候也会走Code索引呢。
这是为什么?
在线等。我现在的目的就是要他走2个索引,code 和 date1。
2、如果想查询到的字段都用上索引,建议date和code建立一个组合索引;
比如:date索引和code索引 再建一个date-code索引
除楼上讲的外,建议建个函数索引:
create index indexname on t_tablename ( round(to_number(tn.date1-tn.date2)* 1440))
不指明index_ffs方式,直接select /*+ index(tn code索引名)*/ 试试?
走该索引,ORACLE不一定才用ffs方式,可能是RANGE SCAN
你指明ffs方式,可能ORACLE经过评估,觉得cost太大,不如不走。
ORACLE选择的执行计划较优。
那我可以建
create index indexname on t_tablename ( tn.code like dep.depcode || '%' )
索引吗?
或者条件改成instr(tn.code ,dep.depcode) dep.depcode是其他表的字段。
create index indexname on t_tablename ( instr(tn.code ,'') )
可以吗?
是可以直接使用在code上的索引的。这个不需什么函数索引。执行计划不走code索引,是认为代价高。你把详细的执行计划贴出来,包括统计信息。另外你在同一表上同时使用两个索引,要加index_join这个hint才行,但是同时使用两个索引有一个merge过程,不一定比你用联合索引效率高。你先贴执行计划。
根据楼主的情况,建议使用联合索引,另外select时也可以指定索引,举个简单例子:
select * from test with(index(idx1)) where id=911;