Oracle SQL子句评估顺序 [英] Oracle SQL clause evaluation order

查看:77
本文介绍了Oracle SQL子句评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle中,首先评估哪种子句类型?如果我有以下内容(假装....表示有效的表达式和关系名称),则求值顺序是什么?

In Oracle, which clause types get evaluated first? If I had the following ( pretend .... represent valid expressions and relation names ), what would the order of evaluation be?

SELECT   ...
FROM     .....
WHERE    ........
GROUP BY ...........
HAVING   .............
ORDER BY ................

我的印象是SELECT子句最后被求值,但除此之外,我一无所知.

I am under the impression that the SELECT clause is evaluated last, but other than that I'm clueless.

推荐答案

选择列表不能总是最后被求值,因为ORDER BY可以使用选择列表中定义的别名,因此必须在以后执行.例如:

The select list cannot always be evaluated last because the ORDER BY can use aliases that are defined in the select list so they must be executed afterwards. For example:

SELECT foo+bar foobar FROM table1 ORDER BY foobar

我要说的是,通常执行的顺序可能是这样的:

I'd say that in general the order of execution could be something like this:

  • 在哪里
  • 组别
  • 选择
  • 拥有
  • ORDER BY

可以交换GROUP BY和WHERE子句而不更改结果,HAVING和ORDER BY也可以.

The GROUP BY and the WHERE clauses could be swapped without changing the result, as could the HAVING and ORDER BY.

实际上,事情更加复杂,因为数据库可以根据不同的执行计划对执行进行重新排序.只要结果保持不变,按执行顺序无关紧要.

In reality things are more complex because the database can reorder the execution according to different execution plans. As long as the result remains the same it doesn't matter in what order it is executed.

还请注意,如果为ORDER BY子句选择了索引,则从磁盘读取行时,它们可能已经按照正确的顺序排列了.在这种情况下,根本不会真正执行ORDER BY子句.

Note also that if an index is chosen for the ORDER BY clause the rows could already be in the correct order when they are read from disk. In this case the ORDER BY clause isn't really executed at all.

这篇关于Oracle SQL子句评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆