Skip to Content
author's profile photo Former Member
Former Member

Does "ORDER BY PRIMARY KEY" make any sense inside SELECT..ENDSELECT in HANA?

Hello All,

I try to understand if there is any difference in result between a) and b) in HANA:

a)
SELECT * FROM table INTO work_area
WHERE field = field
ORDER BY PRIMARY KEY.
ENDSELECT.

b)
SELECT * FROM table INTO work_area
WHERE field = field
ENDSELECT.

In other words - does "ORDER BY PRIMARY KEY" sort the result only (not the DB table before the SELECT...) ?

Thanks, Alex.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Dec 04, 2015 at 03:36 PM

    Not the same. If, for example, you use MARA, the first one is identical to

    SELECT * FROM mara INTO work_area

    WHERE field = field

    ORDER BY matnr.

    ENDSELECT.


    The second one has no defined order. I can't see why this would be any different in HANA. The underlying mechanism may be different. The performance may vary according to database.


    If you get different results according to your database, that would make a mockery of open SQL.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Matthew Billingham

      OK, this makes all clear: ORDER BY sorts the content of a table before data from table is selected, that means ORDER BY inside SELECT..ENDSELECT can give not just another way sorted lines, but even completelly different result, for example in such case:

      SELECT * FROM vbrk UP TO 10 ROWS
      ORDER BY fkart.
      WRITE vbrk-vbeln.
      ENDSELECT.

      subject closed.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 04, 2015 at 01:13 PM

    Hi Alexander,

    from my understanding of the columnstore and the "ORDER BY PRIMARY KEY" is as follows:

    Order by Primary Key, does not sort the results, it fetches the data sorted as they are sorted on the database according to the primary key, if the database is sorting the table, if not, the results are sorted according to the primary keys.

    As a database using columnstore you can not just fetch the data and have them sorted by primary key, so the openSQL syntax is probably emulated and so the results are sorted.

    TL:DR on HANA the ORDER BY PRIMARY KEY should not be faster than a manual ORDER BY "keys" due to emulation

    You can also read the following link for general information.

    Specifying a Sort Order - ABAP Programming (BC-ABA) - SAP Library

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.