返回首页
当前位置: 主页 > MySql > 性能优化 >

mysql分页查询

时间:2009-04-20 08:13来源: 作者: 点击:
MySQL中一般的分页作法大多利用Limit限制回传的资料笔数来达成分页效果 例如下面的代码 Select * From news limit 0, 100第一页 Select * From news limit 100,100第二页 Select * From news limit 200,100第三页
  
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";
}
使用百度搜索:mysql分页查询百度中搜索:mysql分页查询      使用Google搜索:mysql分页查询Google中搜索:mysql分页查询
顶一下
(2)
13.3%
踩一下
(13)
86.7%
收藏到网摘:
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
推荐内容
赞助商广告