dongluo6343 2019-05-04 12:32
浏览 61

在MySQL中使用PDO :: FETCH_ORI_ABS

I am running some performance tests on several ways of getting both limited and offsetted data and total number of rows from a query. The naive way is to run the query twice, once for total number of rows, and a second time with LIMIT OFFSET clause to get the actual data.

I am trying to do it now with a single query, where I receive all data and use fetch with FETCH_ORI_ABS to select only the data I need, but it doesn't seem to work. I get data starting from row 1 to "offset + limit".

This is the code:

function method2($pdo, $query, $limit, $offset) {
    $stmt = $pdo->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
    $stmt->execute();
    $totalrows = $stmt->rowCount();

    $data = [];
    for ($i = $offset; $i < max($totalrows, $offset + $limit); $i++) {
        $data[] = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $i);
    }

    return array("totalrows"=>$totalrows, "data"=>$data);
}

I found this question, telling me there is no support for cursors, but it is from 2014. I didn't find any other resource on possible support.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大