Static DB Query for all clients

On my apache server, there is a php script to query mysql db.
Front-end is handled by react and I run this script via axios.
the query is static, i get same result for all clients, for example I show last 2 hour record of temperature data. Front-end page is refreshed in every 5 seconds to update the data.
According to this design, if I have 10 clients, in each 5 seconds 10 clients are queriying my db for the same data.
Since the query is static, I think that this system is not well optimised.
I dream that on server, there should be one connection to db in each 5 seconds and all clients (10 or more) should fetch the data from server buffer periodically.
Since i am not an experienced web developer, I dont know the terminolgy to achieve this.
Any suggestion or thought is appreciated.

Depends on how long the query takes.
Do you have any performance issues? If not, leave it as it is.
Do not optimize what is not needed to be.
If the query is very slow, I would check if I can optimize the query at a first step.

So if you have a simple query like “SELECT … FROM … WHERE …” it makes no sense to safe the data somewhere else to send it to the client, because normally you will not read the data from this other place more fast then from the DB itself.
If you have a complicated query with many JOIN or, more worse, many sub selects, you can create a new table in the database which you fill with the result of your query. In the server site script you then read this table and check if the data in the table is older then 5 seconds. If so, you refresh the table before you read it and send the data back to the client.

Thanks Tahllius,
In fact query is a simple select where query, data amount is very small.
But there is a performance issue of the server itself. It is a VM on obsolute virtualisation server and there are some intensive calculations continuously on the back end. Therefore I try to optimise it better.
I am not trying to make it faster but reduce processes.
And btw I am trying to learn an alternate method to improve myself.

Is this calculations made by your application or other tenants? Calculating on the same database or any other non db calculation?

Application is my made (c++). It runs on same server and it must be.
It processes some logs and occupies the same db schema in which a couple of tables are monitored by web clients.

Depending on database and what is logged, you can maybe use database triggers to create logs? You may also move some calculation on the SQL side to reduce traffic.

As long as everything is running on the same virtual server it will not help to move work from backend to database. the only solution would be to have own hardware for database or own hardware for the C++ calculation etc.

1 Like

Thanks all for their valuable recommendations. I noted.

Isn’t that a good idea to make the clients talk with a server worker thread (as a controller) to get information, instead of making the clients directly fire the db query (via server script) ?
At least for my case.

This will bring more control on client connections and security imo.

A Client MUST NEVER EVER connect directly to a database!!!

this is my code:
axios.get(“”).then(response => {

I hope I had followed your warning. This is the only way I know.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.