我的数据库中有100万条记录。如何更快地分页..?

时间:2013-11-28 11:30:49

标签: php jquery pagination

我在数据库中有100万条记录。

在索引页面上,多个过滤器通过数据库过滤创建where子句。 例如

从primaryinfo中选择*,其中category ='abc'和technology ='PQR'

我想表明 - : 1.找到的记录数量 2.页面。 3.页面上有10个100(有点东西)。

将过滤后的记录作为json对象发送到jquery,循环遍历记录并附加到特定的div。

下面是我的php分页代码

$selectQ = "select * from primaryinfo where  match(title,description,tags)       against('".$searchCombine."') and category='abc' and technology='pqr'";
$result = mysql_query($selectQ);
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
$start;
$end;
if (isset($_POST['pagecc']))
{
$show_page = $_POST['pagecc'];  
if ($show_page > 0 && $show_page <= $total_pages)
{
    $start = ($show_page - 1) * $per_page;
    $end = $start + $per_page;
} else
{ 
        $start = 0;              
    $end = $per_page;
}
 }
else
{ 
$start = 0;
$end = $per_page;
} 
if($end > $total_results)
    $end = $total_results; 
for($i=$start;$i<$end;$i++){
// here the json object is created
}

1 个答案:

答案 0 :(得分:3)

首先你可以得到总数:

select COUNT(*) from primaryinfo 
where  match(title,description,tags) against('searchCombine') 
       and category='abc' 
       and technology='pqr' 

然后,您可以使用LIMIT功能进行分页:

select * from primaryinfo 
where  match(title,description,tags) against('searchCombine') 
       and category='abc' 
       and technology='pqr' 
LIMIT 0 10; -- Start at offset 0 show ten items per page

请注意,mysql_*函数已弃用,将在以后的PHP版本中删除。请考虑使用mysqliPDO

要进一步提高性能,您可以查看在列上设置索引。特别是类别和技术专栏,但这取决于您的数据。