搜尋
首頁後端開發php教程MySQL分页性能问题,Limit 性能问题求解决!

众所周知,Mysql分页就要用到Limit进行分页,数据量/分页数小的时候Limit性能是可想而知的。 如:

SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 100,100;

上述SQL语句性能没有任何问题。

但是,如果当offset便宜了过大的情况就会出现性能瓶颈。 如:

SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 159000,100;

若要解决Limit offset过大的时候,可以采用子查询的方法进行分页。 如:

SELECT pid,author,hash,dateline FROM posts ORDER BY pid ASC LIMIT 159000,100;

等价于:

SELECT pid,author,hash,dateline FROM posts WHERE pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;

此时的性能是纯粹LIMIT 159000,100 的几倍至高。

但是问题又来了,子查询又必须是数据连续,所以无法加入WHERE查询条件,如:

SELECT tid,pid,author,hash,dateline FROM posts WHERE tid=10 AND pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;

此时的结果并非我想要的结果。

所以求高性能解决LIMIT分页的方法。 在线等。

============================ PS: pid 为主键

回复内容:

众所周知,Mysql分页就要用到Limit进行分页,数据量/分页数小的时候Limit性能是可想而知的。 如:

SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 100,100;

上述SQL语句性能没有任何问题。

但是,如果当offset便宜了过大的情况就会出现性能瓶颈。 如:

SELECT pid,author,hash,dateline FROM posts WHERE pid='123456' ORDER BY pid ASC LIMIT 159000,100;

若要解决Limit offset过大的时候,可以采用子查询的方法进行分页。 如:

SELECT pid,author,hash,dateline FROM posts ORDER BY pid ASC LIMIT 159000,100;

等价于:

SELECT pid,author,hash,dateline FROM posts WHERE pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;

此时的性能是纯粹LIMIT 159000,100 的几倍至高。

但是问题又来了,子查询又必须是数据连续,所以无法加入WHERE查询条件,如:

SELECT tid,pid,author,hash,dateline FROM posts WHERE tid=10 AND pid>=(SELECT pid FROM posts LIMIT 159000,1)ORDER BY pid ASC LIMIT 100;

此时的结果并非我想要的结果。

所以求高性能解决LIMIT分页的方法。 在线等。

============================ PS: pid 为主键

这篇PDF可以解决你的问题

Efficient Pagination Using MySQL

你的pid是啥,不是主键吗?最后一条 pid=10 AND pid>=(SELECT pid FROM posts LIMIT 159000,1) 有能同时成立吗

外面有的条件,子查询里面照样写就行了,和子查询的条件相同,order by相同,比如: select * from user where sex=2 and issingle=0 and id

<code>    ALTER TABLE `posts `  ADD INDEX `tid_pid` USING BTREE (`tid`,`pid`) comment '';
</code>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP中的依賴注入:一個簡單的解釋PHP中的依賴注入:一個簡單的解釋May 10, 2025 am 12:08 AM

依賴性(di)inphpenhancesCodeFlexibility andTestability by decouplingClassesscyclasses fromtheippentencies.1)UseConstructorientoctionTopAssDopassDectiesViactructors Viactructors

PHP DI容器比較:選擇哪一個?PHP DI容器比較:選擇哪一個?May 10, 2025 am 12:07 AM

推薦Pimple用於簡單項目,Symfony的DependencyInjection用於復雜項目。 1)Pimple適合小型項目,因其簡單和靈活。 2)Symfony的DependencyInjection適合大型項目,因其功能強大。選擇時需考慮項目規模、性能需求和學習曲線。

PHP依賴注入:什麼,為什麼以及如何?PHP依賴注入:什麼,為什麼以及如何?May 10, 2025 am 12:06 AM

依賴性注射(DI)InphpisadesignpatternwhereClassDepentenciesArepassedtotosedTosedTosedTotratherThancReateDinterally,增強codemodemodularityAndTestabily.itimprovessoftwarequalitybyby By:1)增強tosestabilityTestabilityTestabilityThroughityThroughEasyDepentyDepententymydependentymocking,2)增強Flexibilybya

PHP中的依賴注入:最終指南PHP中的依賴注入:最終指南May 10, 2025 am 12:06 AM

依賴性(di)InphpenhancesCodemodularity,可檢驗性和確定性。 1)itallowSeasysWappingOfComponents,AsseeninaPaymentGateWayswitch.2)dicanbeimimplementlededMermplemplemplemplemplemplemplemplemplempletallyororororerorviacontainers,withcontanersAddingComplexiteDcomplexiteDcomplexiteDcomplexitingCompleaDdingCompleAddingButaidLararArargerProprproproprys.3)

優化PHP代碼:減少內存使用和執行時間優化PHP代碼:減少內存使用和執行時間May 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP電子郵件:分步發送指南PHP電子郵件:分步發送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

如何通過PHP發送電子郵件:示例和代碼如何通過PHP發送電子郵件:示例和代碼May 09, 2025 am 12:13 AM

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

高級PHP電子郵件:自定義標題和功能高級PHP電子郵件:自定義標題和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境