thinkphp使用mysql百万数据分表快速分页查询

<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

class Lable extends Base
{

    public function index()
    {
        $data = $this->request->param();
        if (isset($data[‘ajax‘]) && $data[‘ajax‘] == 1) {
            $page = isset($data[‘page‘]) ? $data[‘page‘] : 1;
            $page = intval($page);
            $limit = isset($data[‘rows‘]) ? $data[‘rows‘] : 10;
            $limit = intval($limit);
            // $start = $limit * ($page - 1);
            
            $lists = $this->getLable($page, $limit);

            return $this->json_data($lists[‘count‘], $lists[‘list‘]);
        }
        return $this->fetch();
    }


    public function getLable($page, $limit)
    {
        
        $list = [];
        $count = 0;
        $k = 0;
        for ($i=0; $i < 10; $i++) {
            $k = $i;
            if($k == 0){
                $k = ‘‘;
            }
            $temp = Db::name(‘product_lable‘.$k)->count();
            $count += $temp;

            $start = ($page - 1)*$limit;
            
            $pre_page = ($count - $temp)/$limit;

            $start = ($page - $pre_page - 1) * $limit;

            $temp1 = Db::name(‘product_lable‘.$k)->limit($start, $limit)->select();

            $list = $list + $temp1;
            
            if($count >= $page*$limit){
                break;
            }
            
        }

        if(intval($k) < 10){
            for ($j=$k+1; $j < 10; $j++) { 
                $temp = Db::name(‘product_lable‘.$j)->count();
                $count += $temp;
            }
        }

        return [‘list‘=>$list, ‘count‘=>$count];
    }


}

  

thinkphp使用mysql百万数据分表快速分页查询

上一篇:Android Studio 如何在TextView中设置图标并按需调整图标大小


下一篇:手机uc浏览器打不开本地网页,网页一片空白