Oracle SQL clause evaluation order

后端 未结 5 1986
名媛妹妹
名媛妹妹 2020-12-28 11:18

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

5条回答
  •  眼角桃花
    2020-12-28 12:02

    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.

提交回复
热议问题