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?

6 points

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

Maybe I misunderstand what you are trying to do.

permalink
report
reply
3 points

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

permalink
report
parent
reply
2 points

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

permalink
report
reply
2 points
*

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?

permalink
report
reply
1 point
Deleted by creator
permalink
report
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.6K

    Monthly active users

  • 1.6K

    Posts

  • 26K

    Comments