加快PDO查询速度

时间:2015-11-18 18:55:01

标签: php mysql pdo

我有以下PHP代码:

public function indexload(){

        $pdo = DB::connection()->getPdo();
        $pdo->setAttribute($pdo::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

        $count = $pdo->query('select count(id) from clans')->fetchColumn();

        $stmt = $pdo->prepare('SET @rownum = 0');
        $stmt->execute();

        $selectSql = 'SELECT @rownum := @rownum+1 AS rank, id, clanid, name, location, level, exp, warslost, warstied, warwinpercent, warswon, playercount, score
                      FROM clans
                      ORDER BY clanid ASC';
        $statement = $pdo->prepare($selectSql);
        $statement->execute();

        $clan = $statement->fetchAll();

        return  [
            'draw' => 1,
            'recordsTotal' => $count,
            'recordsFiltered' => count($clan),
            'data' => $clan,
        ];
    }

我用它来生成DataTables表。

上面的代码输出:http://clashdata.tk/clans/load但正如您所看到的那样需要很长时间。有没有办法让我的代码更快?

0 个答案:

没有答案