Default sorting for Database Select

Source: Internet
Author: User

Oracle does not sort the results returned by statements without an order by, and Oracle is then processed to read data in the order in which it is physically stored. Because ROWID is the address of each row of data, it sometimes looks like it is sorted using ROWID. But this order can be disrupted, and after the table's data has been deleted, ROWID will be consumed by the newly inserted data. So the result of an order-by query may seem messy.

If the SELECT statement does not add "order BY," How will MySQL sort it.

* Cannot rely on MySQL for default sorting
* If you want to sort, always add order by
* GROUP by imposes an order by,

For MyISAM tables
The MySQL Select default sort is displayed in the physical storage order. (no extra sorting is done).
Other words
The SELECT * from tbl– produces a "table scan." If the table does not have a delete, replace, update operation, the records appear in the order in which they were inserted.

InnoDB table
In the same situation, they are arranged in the order of the primary key. But this is not a reliable one. Unspoken rules.

When "Select" does not add "order by," MySQL tries to return the data in the quickest way possible (the MySQL actual method is not fast).
Because access to primary key, index most of the situation will be faster (in the cache) so the returned data may be in the primary key, the order of the index output,
This does not really sort, mainly because the primary key, the index itself is sorted into memory, so the continuous output may be some sort of sequence.
In some cases, the data that consumes the shortest hard drive seek time is returned first.
If you query only a single table, it is regular in special cases.

Final summary

"Order BY IS added"

We have to do this by default and order by sort, and if the field of order by has duplicate values, you must specify a second sort field, and if the second sort field has duplicate values, you must specify more fields until all the sorted fields can specify a unique order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.