Oracle SQL 子句评估顺序
在 Oracle 中,首先评估哪些子句类型?如果我有以下内容(假装..代表有效的表达式和关系名称),求值的顺序是什么?
SELECT ...
FROM .....
WHERE ........
GROUP BY ...........
HAVING .............
ORDER BY ................
我的印象是 SELECT 子句是最后评估的,但除此之外我一无所知。
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 ................
I am under the impression that the SELECT clause is evaluated last, but other than that I'm clueless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
选择列表不能总是最后评估,因为 ORDER BY 可以使用选择列表中定义的别名,因此必须在之后执行它们。例如:
我想说,一般来说,执行顺序可能是这样的:
GROUP BY 和 WHERE 子句可以交换而不改变结果,HAVING 和 ORDER BY 也可以交换。
实际上事情更复杂,因为数据库可以根据不同的执行计划重新排序执行。只要结果保持不变,执行顺序并不重要。
另请注意,如果为 ORDER BY 子句选择索引,则从磁盘读取行时,这些行可能已经处于正确的顺序。在这种情况下,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:
I'd say that in general the order of execution could be something like this:
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.
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 查询处理 Order
Oracle Query Processing Order
这就是执行计划的用途。但是,一般来说,只有一种方法可以做到这一点。我暂时忽略优化:
优化可能会导致一些“窥视”以做出更好的决策(例如,在扫描表之前检查 WHERE 子句是一个好主意 - 索引可能可用)。
我相信大多数 RDBMS 通过预传递优化器来解决这个问题,优化器基本上会重写查询以利用索引、删除冗余表达式等。然后,这个优化的查询将用于实际构建执行计划。还有并行性可能会改变细节——但基本原理是相同的。
That's what execution plans are for. But, generally, there's only 1 way to do it. I'll ignore optimizations for the moment:
Optimizations could cause some "peeking" to make better decisions (eg., it'd be a good idea to check the WHERE clause before scanning the table - an index may be available).
I believe most RDBMS solve this with a pre-pass through an optimizer which will basically rewrite the query to take advantage of indexes, remove redundant expressions, etc. This optimized query is then used to actually build the execution plan. There's also parallelism that could change the specifics - but the basics are the same.
下面是 SQL 查询处理顺序:
FROM
CONNECT BY
WHERE
GROUP BY
HAVING
Below is SQL Query Processing Order:
FROM
CONNECT BY
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
SELECT
语句的逻辑处理顺序这是以下逻辑顺序:用于编写/(逻辑思考)查询。数据库在实际执行过程中,为了提高效率,可能会采用不同的方式对查询进行优化,只要返回的结果与遵循此执行顺序相同即可。
参考
Microsoft T-SQL
Oracle 博客
Logical Processing Order of the
SELECT
statementThis is the logical order to be used in writing/(logically thinking out) the query. The database may optimize the query in different ways during actual execution for sake of efficiency, as long as the returned results are the same as if it followed this execution order.
References
Microsoft T-SQL
Oracle Blog