PHP排序大数据量分页

大数据量分页,除了使用mysql的limit 排序,也可以使用php排序,来实现业务。

limit a,b 的取数据方式是,先取出a+b条数据,再把a条筛选掉,剩b条,相当于一次性要取a+b条的数据,而a条其实是无用的

代码:使用到的函数   usort、array_slice  

  public function getPage(int $pageSize, int $offset){

        $sql = 'select scores,count(id) as num from poster_info group by scores';
        $db = $this->getDataBase(true);
        $rs = $db->createSql($sql)->query($sql)->fetchAll();
        // 结果进行排序
        usort($rs, [$this, 'sortfunction']);

        if ( !$pageSize ) {
            return $rs;
        }
        return array_slice($rs, $offset, $pageSize);
    }
public function sortfunction(array $a, array $b): int
    {
        if ( $a['num'] == $b['num'] ) {
            return 0;
        }
        return $a['num'] > $b['num'] ? -1 : 1;
    }

   usort :



array_slice  :分割数组,获取当前需要展示的页数

当然也可以使用mysql大数据分页,先查找出当前要查找的起始行id,然后执行

select from where id>最后一个的id limit 10

我为人人,人人为我;美美与共,天下大同;

猜你喜欢

转载自blog.csdn.net/qq_37837134/article/details/80417731