Reputation: 6120
Is there a better way to watch for new entries in a table besides selecting from it every n ticks of time or something like that?
I have a table that an external program updates very often, and clients can watch for this new data as it arrive, how can I make that without having to set a fixed period of repeatable select statements?
Upvotes: 28
Views: 27832
Reputation: 41
You can enable binlog and read binlog for new insert/update/delete on any table.
For those who are doing this in go, there is a go package here that does the trick. And here is a sample implementation.
And for those doing this in python, here's a python library, which can work blocking, like a listener for events, which also can be filtered for event type, table etc.
Upvotes: 1
Reputation: 7447
It is possible to trigger behaviour outside the database, if you have control of the host.
A) This question's answer suggests writing to a file using 'select into outfile' with some system process watch that file for changes (e.g. inotify-based approach such as node-inotify or even a Grunt-watch might suffice)
B) For the brave: The answer to this question points out that if you can install C/C++ add-ons to your database server, you could use a User Defined Function (UDF) to call sys_exec() and thus trigger external processes, or presumably write the actual process directly in the UDF.
http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html#qandaitem-B-5-1-11
(I'm researching this at the moment, for an AWS RDS hosted application so unfortunately neither of these options are right for me.)
Upvotes: 6
Reputation: 17709
Another similar approach would be to add
add column Last_Modified TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP
to each table and preface your select queries to compare the last request date/time with the max(Last_Modified).
Databases are typically pull sources and not push so you'll still need to programmatically probe for changes no matter what.
Upvotes: 10
Reputation: 340171
In MySQL there's no best way than to poll (you create a specific table to simplify the polling though), in other databases you can have triggers that have impact outside the database. In MySQL triggers can only do stuff inside the database itself (for instance, populating the helper table).
Upvotes: 11
Reputation: 995
This is just a small improvement to your method. Write a trigger on the table(s) you are watching to update a Last_Changed table.
Upvotes: 1
Reputation: 31406
Here's what I do: I've got some triggers set up for the table (insert, delete, update) and those triggers increment a counter in another table. My DB access code keeps a local counter and compares it to the returned value, ultimately sending a bool back to the caller, answering the question IsDataCurrent().
Our programs that use this DB access code either poll or check it on-request and then make the appropriate calls to keep themselves up to date.
I'm sure there are other ways to solve this. It worked for me pretty well, though.
Upvotes: 8