SQL Help

SQL_CALC_FOUND_ROWS is deprecated: how to paginate with a total count in MySQL 8

Updated 18 November 2026

SQL query with SELECT, FROM and WHERE clauses and a nested subquery shown on a dark blue code screen, with a hand pointing at it.

The short answer: SQL_CALC_FOUND_ROWS and FOUND_ROWS() have been deprecated since MySQL 8.0.17 (they still work, with a warning, but are slated for removal). Replace them with a separate SELECT COUNT(*) using the same WHERE, or COUNT(*) OVER () in the page query itself, and for big tables, stop counting altogether.

A 2000s-era MySQL idiom, revisited for the modern archive. If you learned pagination from a PHP tutorial written when this site's original SQL Help series was, this one is for you.

This guide draws on the MySQL reference manual and release notes, read on 18 November 2026. We have not benchmarked these queries ourselves; the chart below is a worked example, not a measurement.

Jump to: Side by side · Which to use · Keyset pagination · FAQ

What it did, and why everyone used it

Every paginated listing needs two things: this page's rows and the total number of rows, so you can render "Page 3 of 41". The classic MySQL trick got both from one query plus a follow-up call:

SELECT SQL_CALC_FOUND_ROWS id, title
FROM articles
WHERE status = 'published'
ORDER BY published_at DESC
LIMIT 20 OFFSET 40;

SELECT FOUND_ROWS();      -- total rows the query WOULD have returned without LIMIT

Elegant on the surface. Underneath, it forced MySQL to compute the entire result set, every matching row, fully sorted, just to count it, then throw all but 20 away. On small tables nobody noticed. On large ones, the MySQL 8.0.17 release notes say "COUNT(*) is subject to certain optimizations. SQL_CALC_FOUND_ROWS causes some optimizations to be disabled." A bare count can be answered from an index without touching the rows. That, plus the awkward stateful second call, is why it was deprecated.

MySQL 8.0.17 was released on 22 July 2019. The MySQL 8.0 manual warns to "expect them to be removed in a future version of MySQL". The current MySQL 9.7 manual still lists FOUND_ROWS() with the same deprecation warning.

Replacement 1: two queries (the default)

SELECT COUNT(*) AS total
FROM articles
WHERE status = 'published';

SELECT id, title
FROM articles
WHERE status = 'published'
ORDER BY published_at DESC
LIMIT 20 OFFSET 40;

Same WHERE in both: factor it into one place in your code so they can't drift apart. With an index on (status, published_at), the count is answered from the index and the page query seeks straight to its rows. This is what the MySQL team themselves recommend, and for most applications it's the end of the story.

If you're moving old code: the change is mechanical: replace the FOUND_ROWS() call with the count query, drop the modifier. In PHP PDO terms, one extra fetchColumn().

Replacement 2: one query with a window function

Since MySQL 8.0, COUNT(*) OVER () attaches the total to every row of the page:

SELECT id, title,
       COUNT(*) OVER () AS total
FROM articles
WHERE status = 'published'
ORDER BY published_at DESC
LIMIT 20 OFFSET 40;

Every returned row carries total = 813 (say); read it off the first one. One round-trip, one WHERE, no drift. Two honest caveats: the server still has to identify the whole matching set to count it (it just avoids the sort-then-discard waste), so it isn't magically cheaper than the two-query version on huge tables, and if the page is empty (offset past the end) you get zero rows and therefore no total, so handle that case.

Replacement 3: don't count at all

Ask what the count is for. Very often it's only to decide whether to show a "Next" button. Then fetch one row more than the page size:

SELECT id, title
FROM articles
WHERE status = 'published'
ORDER BY published_at DESC
LIMIT 21 OFFSET 40;      -- page size 20, +1 sentinel

Got 21 back? There's a next page, display 20 and show the button. Got 20 or fewer? You're on the last page. No count query, no scan, and it's what most large sites actually do, the exact "of 41 pages" is quietly dropped, or replaced with a cached approximate ("about 800 results") that's refreshed every few minutes.

Side by side: old pattern, COUNT(*) and keyset

Point Old: SQL_CALC_FOUND_ROWS + FOUND_ROWS() Offset + separate COUNT(*) Keyset pagination
Status in MySQL 8.0 and later Deprecated since 8.0.17 Recommended replacement in the manual Standard SQL, no deprecation
Queries per page 2 (stateful, must run straight after) 2 (independent) 1 (plus an optional count)
Exact total Yes Yes Only with an extra count
"Jump to page 37" Yes Yes No, next and previous only
Cost on deep pages Reads all rows before the offset Reads all rows before the offset About one page of rows
Stable when rows are added between clicks No No Yes
Index needed Helps (filter, sort) index (filter, sort, id) index

Decision table: offset and COUNT(*) or keyset

Your situation Use Why
Admin list, a few thousand rows, page numbers wanted Offset + separate COUNT(*) Simple, exact, and fast enough at this size
Same, but you want one round-trip COUNT(*) OVER () One query and one WHERE; handle the empty-page case
Only a "Next" button is shown LIMIT n+1 No count at all
Infinite scroll, "load more" or an API with cursors Keyset pagination Constant work per page, no skipped or repeated rows
Large table and users want "Page 3 of 41" Offset for the first pages, cached or approximate total Exact counts on every request get expensive
Deep pages (page 1,000 and beyond) are common Keyset pagination Offset reads and discards every earlier row
Legacy code still on SQL_CALC_FOUND_ROWS Offset + separate COUNT(*) The smallest change that removes the deprecated calls
Bar chart of rows MySQL must read to show one 20-row page: OFFSET pagination reads 20 rows on page 1, 2,000 on page 100 and 40,020 on page 2,001, while keyset pagination reads about 20 rows on every page. Worked example, not a benchmark.
Bar chart of rows MySQL must read to show one 20-row page: OFFSET pagination reads 20 rows on page 1, 2,000 on page 100 and 40,020 on page 2,001, while keyset pagination reads about 20 rows on every page. Worked example, not a benchmark.

While you're here: OFFSET has the same disease

LIMIT 20 OFFSET 40000 makes MySQL walk 40,020 rows and discard 40,000: page 2,001 of anything is slow for exactly the reason SQL_CALC_FOUND_ROWS was. The fix is keyset pagination: remember where the last page ended and seek from there:

-- first page
SELECT id, title, published_at
FROM articles
WHERE status = 'published'
ORDER BY published_at DESC, id DESC
LIMIT 20;

-- next page: pass back the last row's (published_at, id)
SELECT id, title, published_at
FROM articles
WHERE status = 'published'
  AND (published_at, id) < ('2026-08-10 09:14:00', 5123)
ORDER BY published_at DESC, id DESC
LIMIT 20;

Constant time per page, however deep you go, and results don't shift when rows are inserted between clicks. The trade-off: no "jump to page 37": which, again, users almost never do. Infinite scroll, "load more", and API cursors are all keyset pagination wearing different clothes.

The id tie-breaker is important. Without it, rows sharing the same published_at value could be skipped or repeated.

If EXPLAIN shows that the row comparison isn't using your index, expand the condition to published_at < ? OR (published_at = ? AND id < ?).

Summary

NeedUse
Exact total, ordinary tableseparate COUNT(*) + LIMIT query
Exact total, one round-tripCOUNT(*) OVER ()
Just "is there a next page?"LIMIT n+1
Deep pages / big tableskeyset pagination, approximate or cached totals
SQL_CALC_FOUND_ROWSnothing new: migrate before it's removed

FAQ

Is SQL_CALC_FOUND_ROWS faster than COUNT(*)?

Not necessarily. The manual says it's faster than running the full query again without LIMIT, because the complete result isn't sent to the client.

However, it also explains that COUNT(*) "is subject to certain optimizations" which SQL_CALC_FOUND_ROWS disables. MySQL recommends a separate count query, ideally supported by a suitable index.

What replaces FOUND_ROWS()?

Use a second query with SELECT COUNT(*), keeping the same WHERE clause and removing LIMIT.

That's the replacement given in both the MySQL manual and the 8.0.17 release notes. Alternatively, COUNT(*) OVER () can return the total as part of the page query.

Does SQL_CALC_FOUND_ROWS still work in MySQL 8.4 and 9?

Yes, at the time of writing. The MySQL 8.4 manual and the MySQL 9.7 manual still document it as deprecated and warn that removal is expected in a future version. It's sensible to plan its replacement now.

How do I paginate large tables in MySQL?

Use keyset pagination. Sort by a unique key, or by a sort column combined with id, then pass the last row's values into the next query.

Use WHERE (sort_col, id) < (?, ?) with LIMIT to fetch the next page. Avoid large OFFSET values and running exact counts on every request.

Does this affect MariaDB?

This article covers MySQL. MariaDB is a separate project with its own documentation. Check the MariaDB knowledge base for its current position before making changes to MariaDB code.