How to get total rows in mysql if there was no `LIMIT` clause?

I was using SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS();
But this has been deprecated https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_found-rows

The recommended way now is first to query with limit and then again without it selecting count(*).
My query is a bit complex and joins a couple of tables with a large number of records, which makes each select take up to 4 seconds, so my process now takes double the time compared to as I just keep using found rows.

How can I go back to just running the select a single time and still getting the total number of rows found without the limit?

10 points · 3 comments · view on lemmy.world

3 Comments

solrize@lemmy.world · 6 pts · 2y (1 reply)

Select count(*) from (select ...) ? See if the query optimizer pipelines that.

Maybe I misunderstand what you are trying to do.

marcos@lemmy.world · 3 pts · 2y

The OP wants the paginated table data and the count. With a single query execution.

xmunk@sh.itjust.works · 2 pts · 2y

In normal SQL you'd use a window function to do this but I don't know if mysql supports that.

hitwright@lemmy.world · 1 pts · 2y
[ removed ]