Does 'Select' always order by primary key?

前端 未结 6 974
无人共我
无人共我 2020-12-01 18:14

A basic simple question for all of you DBA.

When I do a select, is it always guaranteed that my result will be ordered by the primary key, or should I specify it wit

6条回答
  •  失恋的感觉
    2020-12-01 19:05

    When I do a select, is it always guaranteed that my result will be ordered by the primary key, or should I specify it with an 'order by'?

    No, it's by far not guaranteed.

    SELECT  *
    FROM    table
    

    most probably will use TABLE SCAN which does not use primary key at all.

    You can use a hint:

    SELECT  /*+ INDEX(pk_index_name) */
            *
    FROM    table
    

    , but even in this case the ordering is not guaranteed: if you use Enterprise Edition, the query may be parallelized.

    This is a problem, since ORDER BY cannot be used in a SELECT clause subquery and you cannot write something like this:

    SELECT  (
            SELECT  column
            FROM    table
            WHERE   rownum = 1
            ORDER BY
                    other_column
            )
    FROM    other_table
    

提交回复
热议问题