在oracle中使用order by子句创建功能索引

发布于 2024-11-15 06:22:16 字数 362 浏览 3 评论 0原文

我需要在表上按索引创建一个订单,

Student (
  roll_No, 
  name, 
  stream,
  percentage, 
  class_rank, 
  overall_rank  )

我希望查询类似的结果

SELECT * 
  FROM student 
 WHERE stream = 'science'

,预期结果是学生按排名降序排列。要求是我不能在查询本身中指定 order by 子句。

这应该通过 (stream , order by class_rank desc) 上的索引来实现。这个在oracle中可以实现吗?

I Need to create a order by index on a table

Student (
  roll_No, 
  name, 
  stream,
  percentage, 
  class_rank, 
  overall_rank  )

I wish to query something like

SELECT * 
  FROM student 
 WHERE stream = 'science'

The expected result would be the students arranged in descending order of their rank. A requirement is that I can not specify order by clause in the query itself.

This should be achieved by an index on (stream , order by class_rank desc). Is this achievable in oracle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-11-22 06:22:16

如果不指定 ORDER BY 子句,Oracle 不保证返回行的顺序。这个要求没有意义。

您可能会很幸运,发现 Oracle 选择的查询计划恰好按照您想要的顺序返回行。但这是一个运气问题——Oracle 明天可能会选择不同的查询计划,或者 Oracle 版本升级可能会导致结果发生变化。例如,当 Oracle 的新版本添加了更有效的分组算法(没有对结果进行排序的副作用)时,多年来依赖 GROUP BY 子句对结果进行排序的人们感到苦恼。

If you do not specify an ORDER BY clause, Oracle does not guarantee the order in which rows are returned. The requirement does not make sense.

You might get lucky and find that Oracle chooses a query plan that happens to return the rows in the order you want. But that would be a matter of luck-- Oracle could choose a different query plan tomorrow or an Oracle version upgrade may create the results to change. For example, folks that relied for years on the GROUP BY clause ordering the results as a side effect were distressed when a new version of Oracle added a more efficient grouping algorithm that didn't have the side effect of ordering the results.

我的鱼塘能养鲲 2024-11-22 06:22:16

我让它工作了,

  CREATE INDEX stream_rank_idx  ON Student (stream,class_rank desc);

所以当我从学生那里触发 select * 时,stream =?查询将使用上面的索引,它将返回我想要的结果。

我认为如果我不升级 Oracle,它几乎总是安全的。即使在升级之后,oracle 改变索引选择方式的可能性也很小。

I got it working

  CREATE INDEX stream_rank_idx  ON Student (stream,class_rank desc);

so when ever i fire a select * from student where stream =? query the above index will be used and it will return me the desired result.

and i think its almost always safe if i am not upgrading oracle. and even after upgrade there is very low probabilty that oracle will change the way indexes are picked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文