语句:
select a.id,
a.title,
a.content,
a.fname,
a.fphone,
a.ftype,
a.stype,
a.bh,
d.name,
a.result,
a.re,
a.cdate,
a.pdate,
a.state,
a.userid
from n_flow_service a
left outer join dept d on a.deptid = d.id
where 1 = 1
and STATE = 6
and cdate>to_date('2000-1-1','yyyy-MM-dd')
and a.stype <>15 and a.stype<>16 and a.stype<>17
AND BH like '3309%'
order by cdate descn_flow_service表索引:id(unique),cdate,state,stype,bh,state-stype-bh
dept表索引:id(unique)执行计划:select statement,goal=choose
sort order by object name cost
hash join outer
table access full n_flow_service 21
index range scan state-stype-bh 10
table access full dept 4执行需要时间0.453s当我在语句中再加一个条件 and cdate>to_date('2000-1-1','yyyy-MM-dd')再执行时
执行计划:select statment,goal=choose object name cost
nested loops outer
table access by index rowid n_flow_service 6
index range scan descending cdate 2
table access by index rowid dept 1
index unique scan id
执行时间:0.094s现在想请问:为什么我加入一个and cdate>to_date('2000-1-1','yyyy-MM-dd')后连接方式变了,执行计划也变了.执行时间更是大大的缩短了.搞不懂.
哪位大大给我讲解下这条语句在加那个 cdate>....语句的前后的执行顺序变掉和是否走索引变掉的原因.
在下不胜感谢啊!!!!当我在语句中加如下面一句:
select a.id,
a.title,
a.content,
a.fname,
a.fphone,
a.ftype,
a.stype,
a.bh,
d.name,
a.result,
a.re,
a.cdate,
a.pdate,
a.state,
a.userid
from n_flow_service a
left outer join dept d on a.deptid = d.id
where 1 = 1
and STATE = 6
and cdate>to_date('2000-1-1','yyyy-MM-dd')
and a.stype <>15 and a.stype<>16 and a.stype<>17
AND BH like '3309%'
order by cdate descn_flow_service表索引:id(unique),cdate,state,stype,bh,state-stype-bh
dept表索引:id(unique)执行计划:select statement,goal=choose
sort order by object name cost
hash join outer
table access full n_flow_service 21
index range scan state-stype-bh 10
table access full dept 4执行需要时间0.453s当我在语句中再加一个条件 and cdate>to_date('2000-1-1','yyyy-MM-dd')再执行时
执行计划:select statment,goal=choose object name cost
nested loops outer
table access by index rowid n_flow_service 6
index range scan descending cdate 2
table access by index rowid dept 1
index unique scan id
执行时间:0.094s现在想请问:为什么我加入一个and cdate>to_date('2000-1-1','yyyy-MM-dd')后连接方式变了,执行计划也变了.执行时间更是大大的缩短了.搞不懂.
哪位大大给我讲解下这条语句在加那个 cdate>....语句的前后的执行顺序变掉和是否走索引变掉的原因.
在下不胜感谢啊!!!!当我在语句中加如下面一句:
不加之前 那写where后的字段也都是有索引的啊 为什么table access full n_flow_service
1, state上如果有索引的话就是这个索引的选择性太差,比如10000条记录只有10个不同值,oracle不会使用这样的索引。
and STATE = 6
2, <> 这种连接符不会使用索引,同样因为这样的条件过滤性太低
and a.stype <>15 and a.stype<>16 and a.stype<>173,这个条件倒是有可能导致使用索引,但要看有没有以BH为前导列的索引存在,也就是说BH上要有索引,并且必须是BH为索引的第一个列。
AND BH like '3309%'
请问这未大哥 你说的第三条是什么意思,什么叫要有BH为前导列的索引存在,并且必须是BH为索引的第一个列?我的BH上已经有索引了,还要有什么条件?能具体解释下么?
2、第二种写用到了索引并且明显通过索引来直接访问rowid,效率最高
晕死 我就是不知道第二种写法为什么会走索引啊.问了半天......哎
第一张human表97万数据 第二张dept表2000左右数据