How does mysql make order by and mysqlorderby?

Source: Internet
Author: User

How does mysql make order by and mysqlorderby?



Order by clses
In general, the optimizer will skip the sort procedure for the order by clause if it sees that the rows
Will be in order anyway. But let's examine some predictional situations.


For the query:
SELECT column1 FROM Table1 order by 'X ';


The optimizer will throw out the order by clause. This is another example of dead code elimination.

For the query:
SELECT column1 FROM Table1 order by column1;

The optimizer will use an index on column1, if it exists.

For the query:
SELECT column1 FROM Table1 order by column1 + 1;

The optimizer will use an index on column1, if it exists. But don't let that fool you! The index is only
For finding the values. (It's cheaper to do a sequential scan of the index than a sequential scan of the table,
That's why index is a better join type than ALL-see Section 3.2.2.4, "The index Join Type ".)
There will still be a full sort of the results.

For the query:
SELECT * FROM Table1
WHERE column1> 'X' AND column2> 'X'
Order by column2;
I

F both column1 and column2 are indexed, the optimizer will choose an index on... column1.
Fact that ordering takes place by column2 values does not affect the choice of driver in this case.
See:/SQL/SQL _select.cc, test_if_order_by_key (), and/SQL/SQL _select.cc,
Test_if_skip_sort_order ().

Order by Optimization [http://dev.mysql.com/doc/refman/5.1/en/order-by-optimization.html],
Provides a description of the internal sort procedure which we will not repeat here, but urge you to read,
Because it describes how the buffering and the quicksort mechanic ISMs operate.
See:/SQL/SQL _select.cc, create_sort_index ().


From: http://kambing.ui.ac.id/onnopurbo/library/library-sw-hw/linux-howto/mysql/internals-en.pdf

Related Article

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.