MySQL 如何利用做排序
利用索引優(yōu)化排序:order by
單表查詢:
索引列的順序和order by的字句的順序完全一樣,并且所有列的排序方向都一樣,
MySQL才能使用索引對(duì)結(jié)果進(jìn)行排序。order by 子句與查找型查詢的的限制是一樣的,必須滿足索引的最左前綴
創(chuàng)建測(cè)試表:
drop table ?t_index ;
create table t_index(
tid ?int ?not null PRIMARY key ?auto_increment ,
tname varchar(100) not null ,
tage TINYINT ?default 0 ,
tadd varchar(100) default ?'' ,
tel int default ?0,
tmob varchar(20) DEFAULT '' ,
tsfz varchar(100) default ?''?
)?
ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入測(cè)試數(shù)據(jù):
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('張三風(fēng)',110,'恒山' ,18099001122,'012-46319976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('朱元璋',56,'北京' ,18112401122,'012-40119976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('楊過(guò)',25,'武漢' ,18099112122,'012-46340116','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('郭靖',45,'長(zhǎng)沙' ,13149001122,'012-46900176','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('黃老邪',100,'河北' ,13129001122,'012-49001976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('周伯通',102,'河南' ,15679001122,'012-46319001','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('洪七公',78,'合肥' ,11243001122,'012-46319976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('歐陽(yáng)峰',67,'廣西' ,13214001122,'012-14009976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('歐陽(yáng)可',27,'深圳' ,15123001122,'012-46314006','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('尼瑪',10,'上海' ,13125001122,'012-41400976','') ;
insert into ?t_index(tname,tage,tadd,tel,tmob,tsfz)
VALUES('楊康',30,'西藏' ,15798001122,'012-46311400','') ;
創(chuàng)建一個(gè)測(cè)試索引:
mysql> alter table ?t_index ?add key tage(tage,tname,tel) ;
1.查詢使用到索引并且order by的字段和索引中的字段完全一樣(列的順序和排序方向)
mysql> explain ?select * from t_index ?force index (tage) ? where tage <> 120 ?and ?tname ='張三風(fēng)' ?order by tage ,tname,tadd ? ?;
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table ? | type ?| possible_keys | key ?| key_len | ref ?| rows | Extra ? ? ? |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | range | tage ? ? ? ? ?| tage | 5 ? ? ? | NULL | ? 10 | Using where |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
mysql> explain ?select * from t_index ?force index (tage) ? where tage > 120 ? ?order by tage ,tname,tadd ? ?;
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table ? | type ?| possible_keys | key ?| key_len | ref ?| rows | Extra ? ? ? |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | range | tage ? ? ? ? ?| tage | 5 ? ? ? | NULL | ? ?2 | Using where |
+----+-------------+---------+-------+---------------+------+---------+------+------+-------------+
這樣的查詢?nèi)魏螘r(shí)候都能使用到索引進(jìn)行排序,與索引列給定的值無(wú)關(guān)
2.查詢使用了索引并且order by的字段包含索引中的最左列或最左幾列
mysql> explain ?select * from t_index ?force index (tage) ? where tage = 120 ? ?order by tage ? ?;
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table ? | type | possible_keys | key ?| key_len | ref ? | rows | Extra ? ? ? |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | ref ?| tage ? ? ? ? ?| tage | 5 ? ? ? | const | ? ?2 | Using where |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
mysql> explain ?select * from t_index ?force index (tage) ? where tage = 120 ? ?order by tage ,tname ? ;
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table ? | type | possible_keys | key ?| key_len | ref ? | rows | Extra ? ? ? |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | ref ?| tage ? ? ? ? ?| tage | 5 ? ? ? | const | ? ?2 | Using where |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
這樣的查詢?nèi)魏螘r(shí)候都能使用到索引進(jìn)行排序,與索引列給定的值無(wú)關(guān)
3.查詢使用了索引并且order by的字段只包含索引中的非最左列或最左幾列,
第三種類型的查詢要想使用到索引排序添加比較嚴(yán)格
mysql> explain ?select * from t_index ?force index (tage) ? where tage = 120 ? ?order by tname ? ? ;
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table ? | type | possible_keys | key ?| key_len | ref ? | rows | Extra ? ? ? |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | ref ?| tage ? ? ? ? ?| tage | 5 ? ? ? | const | ? ?2 | Using where |
+----+-------------+---------+------+---------------+------+---------+-------+------+-------------+
比如上面的查詢,查詢根據(jù)索引的得到數(shù)據(jù),那么根據(jù)索引查到的數(shù)據(jù)的順序已經(jīng)根據(jù)tage,tname,tadd的順序排列了。
在上面這個(gè)查詢中tage是一個(gè)給定的值,無(wú)需排序,那么查詢出來(lái)的結(jié)果集的順序是按照tname,tadd的順序排列,
即使按照order by tname,tadd 排序,查詢也不要MySQL另外排序
所以以上查詢不需要MySQL另外作排序。
以下查詢的也可以根據(jù)索引來(lái)排序,原理和以上相同
mysql> explain ?select * from t_index ?force index (tage) ? where tage ?= 100 and ?tname ='張三風(fēng)' ? order by tadd ? ? ? ;
+----+-------------+---------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table ? | type | possible_keys | key ?| key_len | ref ? ? ? ? | rows | Extra ? ? ? |
+----+-------------+---------+------+---------------+------+---------+-------------+------+-------------+
| ?1 | SIMPLE ? ? ?| t_index | ref ?| tage ? ? ? ? ?| tage | 307 ? ? | const,const | ? ?1 | Using where |
+----+-------------+---------+------+---------------+------+---------+-------------+------+-------------+
多表關(guān)聯(lián):
如果查詢中需要關(guān)聯(lián)多張表,并且order by中的字段全部來(lái)源于查詢中最外面的表時(shí),才能使用索引做排序
轉(zhuǎn)載于:https://blog.51cto.com/dwchaoyue/1556398
總結(jié)
以上是生活随笔為你收集整理的MySQL 如何利用做排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux学习笔记2-文件读写操作
- 下一篇: CentOS 5.X用第三方源安装PHP