Home >Database >Mysql Tutorial >快速掌握“Mysql Limit”的操作流程_MySQL

快速掌握“Mysql Limit”的操作流程_MySQL

WBOY
WBOYOriginal
2016-06-01 13:56:531104browse

  Mysql Limit操作:

  select * from table LIMIT 5,10; #返回第6-15行数据

  select * from table LIMIT 5; #返回前5行

  select * from table LIMIT 0,5; #返回前5行

  性能优化:

  基于MySQL5.0中limit的高性能,大家可以对数据分页有一个新的认识.

  1.

  Select * From cyclopedia Where ID>=(

  Select Max(ID) From (

  Select ID From cyclopedia Order By ID limit 90001

  ) As tmp

  ) limit 100;

  2.

  Select * From cyclopedia Where ID>=(

  Select Max(ID) From (

  Select ID From cyclopedia Order By ID limit 90000,1

  ) As tmp

  ) limit 100;

  同样是取90000条后100条记录,第1句快还是第2句快?

  第1句是先取了前90001条记录,取其中最大一个ID值作为起始标识,然后利用它可以快速定位下100条记录

  第2句择是仅仅取90000条记录后1条,然后取ID值作起始标识定位下100条记录

  第1句执行结果.100 rows in set (0.23) sec

  第2句执行结果.100 rows in set (0.19) sec

  很明显第2句胜出.看来limit好像并不完全像我之前想象的那样做全表扫描返回limit offset+length条记录,这样看来limit比起MS-SQL的Top性能还是要提高不少的.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn