Reza Amya
Reza Amya

Reputation: 1724

What is the best way to check MySQL table's update continuously?

For some reasons (that I think it is not the point of my question, but if it help, ask me and I can describe why), I need to check MySQL tables continuously for new records. If any new records come, I want to do some related actions that are not important now.

Question is, how I should continuously check the database to make sure I am using the lowest resources and getting the results, close to the realtime.

For now, I have this:

$new_record_come = false;

while(! $new_record_come) {
   $sql = "SELECT id FROM Notificatins WHERE insert_date > (NOW() - INTERVAL 5 SECONDS)";
   $result = $conn->query($sql);
   if ($result)
   {
      //doing some related actions...
      $new_record_come = true;
   }
   else
   {
      sleep(5); //5 seconds delay
   }
}

But I am worry that if I get thousands of users, it will make the server down, even if the server is a high price one!

Do you have any advice to make it better in performance or even change the way completely or even change the type of query or any other suggestion?

Upvotes: 1

Views: 1904

Answers (3)

Aaron Belchamber
Aaron Belchamber

Reputation: 1698

This might not be possible with your current system design but how about instead of using triggers or a heartbeat to poll the database continuously that you go where the updates, etc happen and from there execute other code? This way, you can avoid polling the database continuously and code will fire ONLY IF somebody initiates a request?

Upvotes: 0

Erwin Moller
Erwin Moller

Reputation: 2408

Another angle (much simpler than message queue I think):

I once implemented this on a website by letting the clients poll AND compare it to their latest id they received. For example: You have a table with primary key, and want to watch if new items are added. But you don't want to set up a database connection and query the table if there is nothing new in it.

Let's say the primary key is named 'postid'.

I had a file containing the latest postid.

I updated it with each new entry in tblposts, so it contains alsways the latest postid.

The polling scripts on the clientside simply retrieved that file (do not use PHP, just let Apache serve it, much faster: name it lastpostid.txt or something).

Client compares to its internal latest postid. If it is bigger, the client requests the ones after the last one. This step DOES include a query.

Advantage is that you only query the database when something new is in, and you can also tell the PHP script what your latest postid was, so PHP can only fetch the later ones.

(Not sure if this will work in your situation becuase it assumes an increasing number meaning 'newer'.)

Upvotes: 0

Bill Karwin
Bill Karwin

Reputation: 562871

Polling a database is costly, so you're right to be wary of that solution.

If you need to scale this application up to handle thousands of concurrent users, you probably should consider additional technology that complements the RDBMS.

For this, I'd suggest using a message queue. After an app inserts a new notification to the database, the app will also post an item to a topic on the message queue. Typically the primary key (id) is the item you post.

Meanwhile, other apps are listening to the topic. They don't need to do polling. The way message queues work is that the client just waits until there's a new item in the queue. The wait will return the item.

A comment suggested using a trigger to invoke a PHP script. This won't work, because triggers execute while the transaction that spawned them is not yet committed. So if the trigger runs a PHP script, which probably needs to read the record from the database. But an uncommitted record is not visible to any other database session, so the PHP script can never read the data that it was notified about.

Upvotes: 3

Related Questions