當(dāng)數(shù)據(jù)庫(kù)的數(shù)據(jù)量很大時(shí),一次性查詢(xún)結(jié)果就會(huì)變得很慢,為了提高查詢(xún)效率,我們可以使用MySQL的分頁(yè)查詢(xún)功能。本文就為大家?guī)?lái)MySQL分頁(yè)查詢(xún)方法及優(yōu)化。
推薦閱讀:
分頁(yè)查詢(xún)方法:
在MySQL中,分頁(yè)查詢(xún)一般都是使用limit子句實(shí)現(xiàn),limit子句聲明如下:
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT子句可以被用于指定 SELECT 語(yǔ)句返回的記錄數(shù)。需注意以下幾點(diǎn):
1、第一個(gè)參數(shù)指定第一個(gè)返回記錄行的偏移量
2、第二個(gè)參數(shù)指定返回記錄行的最大數(shù)目
3、如果只給定一個(gè)參數(shù):它表示返回最大的記錄行數(shù)目
4、第二個(gè)參數(shù)為 -1 表示檢索從某一個(gè)偏移量到記錄集的結(jié)束所有的記錄行
5、初始記錄行的偏移量是0(而不是 1)
下面是一個(gè)應(yīng)用實(shí)例:
select * from orders_history where type=8 limit 1000,10;
該條語(yǔ)句將會(huì)從表 orders_history 中查詢(xún)第1000條數(shù)據(jù)之后的10條數(shù)據(jù),也就是第1001條到第1010條數(shù)據(jù)。
數(shù)據(jù)表中的記錄默認(rèn)使用主鍵(一般為id)排序,上面的結(jié)果相當(dāng)于:
select * from orders_history where type=8 order by id limit 10000,10;
三次查詢(xún)時(shí)間分別為:
3040 ms
3063 ms
3018 ms
針對(duì)這種查詢(xún)方式,下面測(cè)試查詢(xún)記錄量對(duì)時(shí)間的影響:
select * from orders_history where type=8 limit 10000,1;
select * from orders_history where type=8 limit 10000,10;
select * from orders_history where type=8 limit 10000,100;
select * from orders_history where type=8 limit 10000,1000;
select * from orders_history where type=8 limit 10000,10000;
三次查詢(xún)時(shí)間如下:
查詢(xún)1條記錄:3072ms 3092ms 3002ms
查詢(xún)10條記錄:3081ms 3077ms 3032ms
查詢(xún)100條記錄:3118ms 3200ms 3128ms
查詢(xún)1000條記錄:3412ms 3468ms 3394ms
查詢(xún)10000條記錄:3749ms 3802ms 3696ms
另外我還做了十來(lái)次查詢(xún),從查詢(xún)時(shí)間來(lái)看,基本可以確定,在查詢(xún)記錄量低于100時(shí),查詢(xún)時(shí)間基本沒(méi)有差距,隨著查詢(xún)記錄量越來(lái)越大,所花費(fèi)的時(shí)間也會(huì)越來(lái)越多。
針對(duì)查詢(xún)偏移量的測(cè)試:
select * from orders_history where type=8 limit 100,100;
select * from orders_history where type=8 limit 1000,100;
select * from orders_history where type=8 limit 10000,100;
select * from orders_history where type=8 limit 100000,100;
select * from orders_history where type=8 limit 1000000,100;
三次查詢(xún)時(shí)間如下:
查詢(xún)100偏移:25ms 24ms 24ms
查詢(xún)1000偏移:78ms 76ms 77ms
查詢(xún)10000偏移:3092ms 3212ms 3128ms
查詢(xún)100000偏移:3878ms 3812ms 3798ms
查詢(xún)1000000偏移:14608ms 14062ms 14700ms
隨著查詢(xún)偏移的增大,尤其查詢(xún)偏移大于10萬(wàn)以后,查詢(xún)時(shí)間急劇增加。
這種分頁(yè)查詢(xún)方式會(huì)從數(shù)據(jù)庫(kù)第一條記錄開(kāi)始掃描,所以越往后,查詢(xún)速度越慢,而且查詢(xún)的數(shù)據(jù)越多,也會(huì)拖慢總查詢(xún)速度。
使用子查詢(xún)優(yōu)化
這種方式先定位偏移位置的 id,然后往后查詢(xún),這種方式適用于 id 遞增的情況。
select * from orders_history where type=8 limit 100000,1;
select id from orders_history where type=8 limit 100000,1;
select * from orders_history where type=8 and
id>=(select id from orders_history where type=8 limit 100000,1)
limit 100;
select * from orders_history where type=8 limit 100000,100;
4條語(yǔ)句的查詢(xún)時(shí)間如下:
第1條語(yǔ)句:3674ms
第2條語(yǔ)句:1315ms
第3條語(yǔ)句:1327ms
第4條語(yǔ)句:3710ms
針對(duì)上面的查詢(xún)需要注意:
1、比較第1條語(yǔ)句和第2條語(yǔ)句:使用 select id 代替 select * 速度增加了3倍
2、比較第2條語(yǔ)句和第3條語(yǔ)句:速度相差幾十毫秒
3、比較第3條語(yǔ)句和第4條語(yǔ)句:得益于 select id 速度增加,第3條語(yǔ)句查詢(xún)速度增加了3倍
這種方式相較于原始一般的查詢(xún)方法,將會(huì)增快數(shù)倍。
使用 id 限定優(yōu)化
這種方式假設(shè)數(shù)據(jù)表的id是連續(xù)遞增的,則我們根據(jù)查詢(xún)的頁(yè)數(shù)和查詢(xún)的記錄數(shù)可以算出查詢(xún)的id的范圍,可以使用 id between and 來(lái)查詢(xún):
select * from orders_history where type=2
and id between 1000000 and 1000100 limit 100;
查詢(xún)時(shí)間:15ms 12ms 9ms
這種查詢(xún)方式能夠極大地優(yōu)化查詢(xún)速度,基本能夠在幾十毫秒之內(nèi)完成。限制是只能使用于明確知道id的情況,不過(guò)一般建立表的時(shí)候,都會(huì)添加基本的id字段,這為分頁(yè)查詢(xún)帶來(lái)很多便利。
還可以有另外一種寫(xiě)法:
select * from orders_history where id >= 1000001 limit 100;
當(dāng)然還可以使用 in 的方式來(lái)進(jìn)行查詢(xún),這種方式經(jīng)常用在多表關(guān)聯(lián)的時(shí)候進(jìn)行查詢(xún),使用其他表查詢(xún)的id集合,來(lái)進(jìn)行查詢(xún):
select * from orders_history where id in
(select order_id from trade_2 where goods = 'pen')
limit 100;
這種 in 查詢(xún)的方式要注意:某些 mysql 版本不支持在 in 子句中使用 limit。
關(guān)于數(shù)據(jù)表的id說(shuō)明
一般情況下,在數(shù)據(jù)庫(kù)中建立表的時(shí)候,每一張表強(qiáng)制添加 id 遞增字段,這樣更方便我們查詢(xún)數(shù)據(jù)。
如果數(shù)據(jù)量很大,比如像訂單這類(lèi),一般會(huì)推薦進(jìn)行分庫(kù)分表。這個(gè)時(shí)候 id 就不建議作為唯一標(biāo)識(shí)了,而應(yīng)該使用分布式的高并發(fā)唯一 id 生成器來(lái)生成,并在數(shù)據(jù)表中使用另外的字段來(lái)存儲(chǔ)這個(gè)唯一標(biāo)識(shí)。
首先使用范圍查詢(xún)定位 id (或者索引),然后再使用索引進(jìn)行定位數(shù)據(jù),即先 select id,然后在 select *;這樣查詢(xún)的速度將會(huì)提升好幾倍。
原文地址:http://uusama.com/458.html
更多建議: