MySQL分頁(yè)Limit的優(yōu)化過(guò)程實(shí)戰(zhàn)
在我們使用查詢(xún)語(yǔ)句的時(shí)候,經(jīng)常要返回前幾條或者中間某幾行數(shù)據(jù),這個(gè)時(shí)候怎么辦呢?不用擔(dān)心,mysql已經(jīng)為我們提供了這樣一個(gè)功能。
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用于強(qiáng)制 SELECT 語(yǔ)句返回指定的記錄數(shù)。LIMIT 接受一個(gè)或兩個(gè)數(shù)字參數(shù)。參數(shù)必須是一個(gè)整數(shù)常量。如果給定兩個(gè)參數(shù),第一個(gè)參數(shù)指定第一個(gè)返回記錄行的偏移量,第二個(gè)參數(shù)指定返回記錄行的最大數(shù)目。初始記錄行的偏移量是 0(而不是 1): 為了與 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
所以通常在查詢(xún)數(shù)據(jù)的時(shí)候,我們都會(huì)用到limit分頁(yè),因?yàn)檫@樣避免了全表查詢(xún),會(huì)提高查詢(xún)效率。但是在一個(gè)表的數(shù)據(jù)量多了之后,分頁(yè)查詢(xún)會(huì)明細(xì)的變慢,下面來(lái)一起看看詳細(xì)的介紹吧
MySQL分頁(yè)Limit優(yōu)化
創(chuàng)建測(cè)試表card 2000萬(wàn)數(shù)據(jù)
mysql> select count(*) from card; +----------+ | count(*) | +----------+ | 20000000 | +----------+ 1 row in set (0.00 sec)
-首先測(cè)試前1000行查詢(xún)速度
mysql> select * from card limit 1000,10; +---------+--------------------------------------+ | card_id | card_number | +---------+--------------------------------------+ | 1001 | 13fc90a6-2e3b-11e8-ae62-9c5c8e6e37cf | | 1002 | 13fc923e-2e3b-11e8-ae62-9c5c8e6e37cf | | 1003 | 13fc93d5-2e3b-11e8-ae62-9c5c8e6e37cf | | 1004 | 13fc956a-2e3b-11e8-ae62-9c5c8e6e37cf | | 1005 | 13fc9702-2e3b-11e8-ae62-9c5c8e6e37cf | | 1006 | 13fc9899-2e3b-11e8-ae62-9c5c8e6e37cf | | 1007 | 13fc9a31-2e3b-11e8-ae62-9c5c8e6e37cf | | 1008 | 13fc9bc6-2e3b-11e8-ae62-9c5c8e6e37cf | | 1009 | 13fc9d5e-2e3b-11e8-ae62-9c5c8e6e37cf | | 1010 | 13fc9ef5-2e3b-11e8-ae62-9c5c8e6e37cf | +---------+--------------------------------------+ 10 rows in set (0.00 sec)
-測(cè)試100萬(wàn)之后的查詢(xún)
mysql> select * from card limit 1000000,10; +---------+--------------------------------------+ | card_id | card_number | +---------+--------------------------------------+ | 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000010 | 2d871039-2e3b-11e8-ae62-9c5c8e6e37cf | +---------+--------------------------------------+ 10 rows in set (0.18 sec)
-測(cè)試1000萬(wàn)之后的查詢(xún)
mysql> select * from card limit 10000000,10; +----------+--------------------------------------+ | card_id | card_number | +----------+--------------------------------------+ | 10000001 | b11ad76c-2e49-11e8-ae62-9c5c8e6e37cf | | 10000002 | b11aefd5-2e49-11e8-ae62-9c5c8e6e37cf | | 10000003 | b11af868-2e49-11e8-ae62-9c5c8e6e37cf | | 10000004 | b11b0031-2e49-11e8-ae62-9c5c8e6e37cf | | 10000005 | b11b07ad-2e49-11e8-ae62-9c5c8e6e37cf | | 10000006 | b11b0f0f-2e49-11e8-ae62-9c5c8e6e37cf | | 10000007 | b11b1669-2e49-11e8-ae62-9c5c8e6e37cf | | 10000008 | b11b1db2-2e49-11e8-ae62-9c5c8e6e37cf | | 10000009 | b11b24fa-2e49-11e8-ae62-9c5c8e6e37cf | | 10000010 | b11b2c37-2e49-11e8-ae62-9c5c8e6e37cf | +----------+--------------------------------------+ 10 rows in set (1.29 sec)
可以看到越到后面查詢(xún)效率會(huì)越低。因?yàn)樵诓樵?xún)100萬(wàn)之后的數(shù)據(jù)的時(shí)候,mysql會(huì)首先查詢(xún)100萬(wàn)零10條數(shù)據(jù),然后截取后面的十條數(shù)據(jù)。這些就造成的性能的降低。
那么怎么去避免這個(gè)掃描100萬(wàn)條數(shù)據(jù)呢。我們可以明確的知道,100萬(wàn)之后的主鍵是大于100萬(wàn)的。所以我們可以將sql改寫(xiě),讓其用到索引,降低掃描的行數(shù)
mysql> select * from card where card_id>=1000000 limit 10; +---------+--------------------------------------+ | card_id | card_number | +---------+--------------------------------------+ | 1000000 | 2d870088-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000001 | 2d87021a-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000002 | 2d8703ac-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000003 | 2d87053b-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000004 | 2d8706cd-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000005 | 2d87085f-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000006 | 2d8709f1-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000007 | 2d870b83-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000008 | 2d870d18-2e3b-11e8-ae62-9c5c8e6e37cf | | 1000009 | 2d870eaa-2e3b-11e8-ae62-9c5c8e6e37cf | +---------+--------------------------------------+ 10 rows in set (0.00 sec)
這樣就可以很大的提高查詢(xún)效率
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)本站的支持。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信