查询索引?

ruanwxh 2009-08-27 09:05:11
有个字段stu_id,并为它建立了索引
在select查询语句中
where子句,索引字段要放在第一个,如:where stu_id=1 and deleted<>2 而不是where deleted<>2 and stu_id=1
听说只有这样才会走索引,这是为什么?
...全文
48 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
inthirties 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 ruanwxh 的回复:]
有个字段stu_id,并为它建立了索引
在select查询语句中
where子句,索引字段要放在第一个,如:where stu_id=1 and deleted <>2 而不是where  deleted <>2 and stu_id=1
听说只有这样才会走索引,这是为什么?
[/Quote]

这里如果没有走索引的话,不是因为你说的这个原因,而是因为oracle的优化器决策出,走fts比索引的cost更小,这样才选择做了fts。
majy 2009-08-27
  • 打赏
  • 举报
回复
哪里有这种事情的,这么傻的话,oracle老早被抛弃了

你试一下就可以了

你的这个查询,索引里要包含两个字段才行

create index idx_id on t(stu_id, deleted);

dbms_stats.gather_table_stats(user, 'TABLE_NAME', method_opt=>'for all indexed columns', cascade=>true);

然后你做测试吧

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧