What potential pitfalls should be considered when using the SQL_CALC_FOUND_ROWS statement in PHP for pagination?

Using the SQL_CALC_FOUND_ROWS statement in PHP for pagination can potentially lead to performance issues, as it requires MySQL to calculate the total number of rows matching the query before returning the actual result set. This can be resource-intensive, especially for large datasets. To mitigate this, it's recommended to use alternative methods for pagination, such as caching the total row count or using a separate query to retrieve the count.

// Example of using a separate query to retrieve the total row count for pagination
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Query to retrieve the total row count
$totalRowsQuery = "SELECT COUNT(*) as total_rows FROM your_table";
$totalRowsResult = $conn->query($totalRowsQuery);
$totalRows = $totalRowsResult->fetch_assoc()['total_rows'];

// Query to retrieve the actual result set with pagination
$query = "SELECT * FROM your_table LIMIT $limit OFFSET $offset";
$result = $conn->query($query);

// Process the result set
while ($row = $result->fetch_assoc()) {
    // Process each row
}

// Pagination links
$totalPages = ceil($totalRows / $limit);
for ($i = 1; $i <= $totalPages; $i++) {
    echo "<a href='?page=$i'>$i</a> ";
}