Results 1 to 6 of 6
  1. #1

    mysql: Can we assume sorted order

    Let's say we have a table with an id (which is auto-incrementing index). Let's then select some rows from this table-- nothing fancy, just simple selection. Can we assume that the rows are returned in the order defined by id, and not in some random fashion.

    We could employ "order by id asc" but I think that forces a temporary result table to be built (?) and would slow down the result set.

    Any thoughts?


  2. #2
    Join Date
    Apr 2003
    London, UK
    As far as i know you wont get any slowdown by adding the order by clause, but yes, by default MySQL sorts ascending from the lowest value first

  3. #3
    Join Date
    Oct 2002
    Mysql usually does return results in order they were entered but i believe it is in manual that you should not rely on that

  4. #4
    You can't rely on this. Try deleting some rows and then the data will not come out in order.

    If you need the data ordered, you must say so. If performance is an issue, build an index on it.

  5. #5
    I think I will stick to having sort, just to be sure.

    Actually now I think my performance issue related to trying to pull in a huge number of rows via PHP. PHP does not seem to handle it well if your row data exceeds the available memory.

    So I have resorted to pulling in a percentage of the row count at a time, and it seems to be performing well (more programming, of couse).

  6. #6
    Join Date
    May 2002
    Yes sometimes the order changes when you delete things from earlier on, you should always specify and order... if the order is important to your results

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts