Oracle SQL 子句评估顺序

发布于 2024-09-01 21:00:00 字数 250 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(5

北斗星光 2024-09-08 21:00:00

选择列表不能总是最后评估,因为 ORDER BY 可以使用选择列表中定义的别名,因此必须在之后执行它们。例如:

SELECT foo+bar foobar FROM table1 ORDER BY foobar

我想说,一般来说,执行顺序可能是这样的:

  • FROM
  • WHERE
  • GROUP BY
  • SELECT
  • HAVING
  • 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:

SELECT foo+bar foobar FROM table1 ORDER BY foobar

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

  • FROM
  • WHERE
  • GROUP BY
  • SELECT
  • 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.

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.

初见你 2024-09-08 21:00:00

Oracle 查询处理 Order

  • FROM 子句
  • WHERE 子句
  • GROUP BY 子句
  • HAVING 子句
  • SELECT 子句
  • ORDER BY 子句

Oracle Query Processing Order

  • FROM clause
  • WHERE clause
  • GROUP BY clause
  • HAVING clause
  • SELECT clause
  • ORDER BY clause
尝蛊 2024-09-08 21:00:00

这就是执行计划的用途。但是,一般来说,只有一种方法可以做到这一点。我暂时忽略优化:

  • FROM 获取涉及的表
  • 开始扫描 FROM 中的表,保留那些通过 WHERE 子句的
  • 表 SELECT 未聚合
  • 列 使用 GROUP BY 计算聚合列
  • 保留那些通过 HAVING 子句的分组结果
  • 使用 ORDER BY 排序结果

优化可能会导致一些“窥视”以做出更好的决策(例如,在扫描表之前检查 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:

  • FROM to get the table involved
  • Start scanning the table in FROM, keeping those that pass WHERE clause
  • SELECT unaggregated columns
  • Calculate aggregated columns with GROUP BY
  • Keep those grouped results that pass HAVING clause
  • order results with ORDER BY

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.

梦里南柯 2024-09-08 21:00:00

下面是 SQL 查询处理顺序:

  1. FROM
  2. CONNECT BY
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. <代码>选择
  7. > <代码>排序依据

Below is SQL Query Processing Order:

  1. FROM
  2. CONNECT BY
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. SELECT
  7. ORDER BY
苯莒 2024-09-08 21:00:00

SELECT 语句的逻辑处理顺序

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE 或WITH ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY

这是以下逻辑顺序:用于编写/(逻辑思考)查询。数据库在实际执行过程中,为了提高效率,可能会采用不同的方式对查询进行优化,只要返回的结果与遵循此执行顺序相同即可。

参考

Microsoft T-SQL
Oracle 博客

Logical Processing Order of the SELECT statement

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE or WITH ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY

This 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

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