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.