MySQL中一般的分页作法大多利用Limit限制回传的资料笔数来达成分页效果
例如下面的代码
Select * From news limit 0, 100第一页
Select * From news limit 100,100第二页
Select * From news limit 200,100第三页
今天突然来了一个思路
和前作上下页查询优化
的思路略同
定位到id值后再用id值作条件
优化的作法
第一页
Select * From news Where id >=(
Select id From news Order By id limit 0,1
) limit 100
第二页
Select * From news Where id >=(
Select id From news Order By id limit 100,1
) limit 100
第三页
Select * From news Where id >=(
Select id From news Order By id limit 200,1
) limit 100
经测试,一万条数据以内一般的分页作法比较快
超过一万条后优化过的作法优势就呈现出来
当数据量愈多,优化的分页查询速度愈快 本文来自Sqlclub
所以在第一次查询总资料笔数后可以增加一个判决
检查资料量是否超过一万笔
Select count(*) AS `count` From news //制作分页的前置作业
if($rows['count'] <10000)
{
$sql = "Select * From news limit 0, 100 ";
}
else
{
$sql = "Select * From news Where id >=( Select id From news Order By id limit 0,1) limit 100";
}
例如下面的代码
Select * From news limit 0, 100第一页
Select * From news limit 100,100第二页
Select * From news limit 200,100第三页
今天突然来了一个思路
和前作上下页查询优化
的思路略同
定位到id值后再用id值作条件
优化的作法
第一页
Select * From news Where id >=(
Select id From news Order By id limit 0,1
) limit 100
第二页
Select * From news Where id >=(
Select id From news Order By id limit 100,1
) limit 100
第三页
Select * From news Where id >=(
Select id From news Order By id limit 200,1
) limit 100
经测试,一万条数据以内一般的分页作法比较快
超过一万条后优化过的作法优势就呈现出来
当数据量愈多,优化的分页查询速度愈快 本文来自Sqlclub
所以在第一次查询总资料笔数后可以增加一个判决
检查资料量是否超过一万笔
Select count(*) AS `count` From news //制作分页的前置作业
if($rows['count'] <10000)
{
$sql = "Select * From news limit 0, 100 ";
}
else
{
$sql = "Select * From news Where id >=( Select id From news Order By id limit 0,1) limit 100";
}
百度中搜索:
Google中搜索: