网站地图    收藏   

主页 > 后端 > mysql数据库 >

mysql如何做orderby - mysql数据库栏目 - 自学php

来源:自学PHP网    时间:2015-04-16 10:50 作者: 阅读:

[导读] ORDER BY ClausesIn general, the optimizer will skip the sort procedure for the ORDER BY clause if it sees that the rowswill be in order anyway But let s examine so...

ORDER BY Clauses
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 exceptional 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. The
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 mechanisms operate.
See: /sql/sql_select.cc, create_sort_index().

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论