How to get total rows in mysql if there was no `LIMIT` clause?
from pe1uca@lemmy.pe1uca.dev to programming@programming.dev on 16 Aug 2024 15:46
https://lemmy.pe1uca.dev/post/1523687

I was using SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS();
But this has been deprecated dev.mysql.com/doc/…/information-functions.html#fu…

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?

#programming

threaded - newest

solrize@lemmy.world on 16 Aug 2024 16:40 next collapse

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

Maybe I misunderstand what you are trying to do.

marcos@lemmy.world on 16 Aug 2024 17:56 collapse

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

xmunk@sh.itjust.works on 16 Aug 2024 18:09 next collapse

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

TehPers@beehaw.org on 16 Aug 2024 22:23 next collapse

Would it work to write the query as a common table expression, then select your columns from that table and join it with a count(*) aggregation of the table?

[deleted] on 21 Aug 2024 21:41 collapse

.