Cited common ways to solve your problem, but here everything depends on the architecture used by you and the requirements for the relevance of the data.
Let me give you an analogy, there is a store that sells goods and sold goods are recorded in the database, but there is a manager who wants to know for what amount the goods were sold, for this he has several solutions:
- Run counting every n-time (it doesn't matter to him that this minute is not quite accurate)
- Receive a message about the goods sold and independently run the count of goods sold
- When adding a product, immediately aggregate the statistics automatically through various ways:
- Send data or tasks to a remote server to run automatic counting.
- Aggregation on the side of the program that performs the addition (with large data it will slow down if it is not just an atomic operation)
Counting all lines in the database for output
In your case, probably the best solution and a simple solution is to run the counting script (page.php) every 1-5 minutes by CRON, if you don’t need to know the exact number of added lines every second.
The second solution, which allows you to accurately count the number of lines, is on the side of the script, which adds data to the database (if it is a single point of addition), an increase in the value of a cache on the number of added rows and then output this value . Here you need to think a bit about the architecture, because lines can still be deleted, etc. You can also hang a trigger on the table, which, when adding a record, would perform some kind of atomic operation to increase the value in another table.
Also, a script can receive a message from another script through a queue and, upon receipt of a message, perform actions related to row counting - this is already demonization.
Counting lines for a specific client
Further, if your problem consists precisely in counting not all the rows, but counting the rows for the client, here it is already a bit more complicated. If the base is small, then counting every minute for everyone is not difficult, but if the base is huge and you need to count for all clients with grouping, this query can be executed longer and every 1-5 it can simply not be executed, it is possible to use counting here on request.
The client accesses the script continuously through the site, but the script saves the data only 1 time, after which it stores the value in the cache for 5 minutes, if the client has added some data, they can be disabled in the cache.