douyun3631 2013-11-28 11:30
浏览 117
已采纳

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

I have 1 million+ records in database.

On the index page, multiple filter to filter through database create where clause. eg

select * from primaryinfo where category='abc' and technology='PQR'

I want to show--: 1. number of records found 2. pages. 3. 10 out 100(kind of thing) on the page.

Am sending the filtered records as json object to jquery, am looping through the records and appending to a particular div.

below is my php pagination code

$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条回答 默认 最新

  • dongmu7335 2013-11-28 11:41
    关注

    First you can get the total:

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

    Then you can paginate using the LIMIT feature:

    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
    

    Please note that the mysql_* functions are deprecated and will be removed in a future PHP version. Please consider using mysqli or PDO.

    To further improve performance you could look at setting up indexes on columns. Particularly the category and technology columns but this will depend on your data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题