Can a MySQL query be run every second? - mysql

I like to update my table every second. I know this is possible in AJAX but is this possible in MySQL query? I found MySQL event, can I get solution from this?

" i wanna check condition **if date_time > now() ** then update status as 1 . is this possible"
it does not seems like you need special status to be setup...
this condition can be checked when data is pulled (if need to be marked execute UPDATE and SELECT when pull),
also it can be done as cron job every minute (not sure can be done every second), however if it very related with user being on page - ajax could be the way to do it and downgrade performance at this same time

It is possible to write an sql query that will set an update status equal to 1 when the date value for that record is out of date, however you will still need a scheduled task to run this sql query from time to time.
If you are able to run code on your server, then you should write up a script that periodically runs your query against the database.

so cron is a scheduler in linux to run anything periodically.
So let's say your script that contains "**if date_time > now() ** then update status as 1" is called updateIfOld.php
then you should make crontab runs "php updateIfOld.php" every second.
Here are the manuals to use crontab: http://www.manpagez.com/man/5/crontab/Ta3HK4KosQPd8aT8BQ&usg=AFQjCNErp1Hz19N7xJwVY1wisQNxmtgpQQ&sig2=D4NQu19AJnBZil9J54V8ww

If you could actually tell a bit more about this situation and why u need this to be done, it will help us to give a better solutions.
With assumptions on what you are trying to achieve , we can give the best.
Anyway sending too many ajax and updating query every second is not an good option.
Here is an idea,
if you can store the expiry time for each row and you can set them set status to 1 where your condition matches.
Anyway i think there must be some reason to change the status to 1 (may be for making them not to display/consider).. If we know the exact reason, i think i can give a better solution..

Related

Is it ok to read a mysql table every 100ms?

I made a node app that reads every 100ms a table in a MySQL database, which get updated from a other app (lua), and compares the result with the last result. If the table has changed the app will perform an action. The reading process needs ~0.05ms and it works all perfect. But now am I asking myself wehter this is an acceptable solution or very unprofessional.
If you are limited with only the database, I would use two timestamp columns like updated_at and deleted_at (with indexes maybe - should ask a DB professional) and store the last read time on the client, and then only query the changes made since. This way it'll be more comfortable when using a table with millions of rows.
And except that, It comes to mind that you can use something like websockets to update the client when an update/insert/delete will occur/had occured.
There's another question about this matter:
Which is more efficient to send WebSocket updates with a MySQL database change
Late Edit
Your "If the table has changed the app will perform an action." sentence is probably the key here. You can just check for something like this:
select count(*) from table
where updated_at > last_update_time or deleted_at > last_update_time
then you can decide for to do that action or not.

How can I run a query in a specific time using MySQL or PHP?

is it possible to run a query in a specific time like for example every seconds/minutes. By the way I am building a simple auction system and the query I am talking about is to check if there are products that are already expired. Means their datetime_end is less than the current date and time.
So I have a query like this:
SELECT id, datetime_end FROM auction_product WHERE datetime_end < NOW() AND `status` = 0;
And after identifying if there are products that are already expired I will create a query that will change their status to 1 means the status is cancelled/closed.
Is there a function in MySQL that can run a certain query? Like automatic query. Or should I make an Ajax that will run every seconds and check if there is an expired product? Can you give me some idea about this?
Because in my current setup I just put the AJAX in product page and homepage. Means if the user is in the other page my AJAX will not run. So I think I will put my created AJAX somewhere in the header or footer or in a global JS. If I did that does it affect the performance of my page load? Because it will run every seconds.
That's all thanks. I hope you can give me some idea about this. :)
You could do this in MySQL using scheduled events e.g.:
CREATE EVENT IF NOT EXISTS expire_products
ON SCHEDULE
EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
DO
<insert your update statement here>;
You need to enable the event scheduler first either via the command line argument:
--event-scheduler=ON
or the my.cnf/my.ini option:
event_scheduler=ON
or by running the following in MySQL:
SET GLOBAL event_scheduler = ON;
See https://dev.mysql.com/doc/refman/5.7/en/create-event.html for more details.
You don't need to check it every second on the client side. In case someone visits the page of the product, you'll run an ajax there to check if there are enough products left or not. If there are no products left you can update the database on the page itself.
Now you also want to make sure it is regularly updated, so you can run a script on the server side on a Cron Job. But, you also need to make sure you don't run some heavy resource intensive scripts on it. You can run a cron job about every hour or two hours to regularly update it from the server side. And in case any of the users views a product, you will update it automatically with the ajax, so the next time a user visits, in between two cron jobs, they will see the page being already updated because of the earlier user. This will keep the pressure out of your server by distributing the work.
So the idea is somewhat like this:
1)user enters-> visits page-> runs ajax to check if products are left -> update db if products are over
2)cron job checks if products are left every two hours-> updates db if products are over

Can mysql send signals to the outer world

I need to execute a system command every time a specific mysql table is updated. Is it possible? Is mysql able to somehow let the system know that a table has been updated?
Thanks in advance.
You can take a look at this: http://bytes.com/topic/mysql/answers/424231-trigger-run-external-program
However, I recommend that you just include it in the programs that access your database. At least, you can customize it better than just putting them in your query.
Use this UDF.
There is a possible to get last updated date of the table.
select update_time from information_schema.TABLES where table_schema='<database>' and table_name='<table>';
at a specific time interval you can fetch the updated time and check with last updated time.

Automation on SQL (MySQL)

I would like to ask a question for connoisseurs of SQL (MySQL, to be specific).
I have a table reservation schedules. And when a customer makes a reservation there is a time to let the client to use my service. Therefore, the reservation that he did have to leave the table reservations.
Once the time limit of use is reached, there is some method (trigger,
I believe), which automatically erase the record of this book on the
table?
If so, can someone give me some idea of ​​how to start my search for it, or it is also totally welcome some help as some more advanced lines of code.
There is also the possibility that this only be possible to be implemented via Server-Side (PHP, ASP ...), which does not believe is so true because SQL is a language very complete (to my knowledge).
Edit1: The problem is that I believe this is a task of the DBMS, so I wanted to leave this responsibility to the MySQL The problem is: how?
A trigger is triggered by either before or after an insert , update or delete event (at least in MySQL according to the docs)
What you want is some sort of scheduled job either through your application be it php, asp.net, etc.. or cron job that calls some sort of SQL script.
So to answer, it can't be done purely with triggers.
You can use SQL jobs, but if the removal logic is to complex to manage it with queries I suggest you to use a PHP script that does all that work for you.
Just write down the data check/remove logic in PHP and set up a simple cron operation for it.
The advantage of this solution is that you can access to your scripts/classes/db providers and save your time and your can log all the operations separately (instead of logging to MySQL logs, no matter what script language you are relying on).
If you have a full control of your server the scheduled operation will look like this (if you want to check your DB entries every day at 00:01):
cat /etc/cron.d/php5
0 1 * * * php /path/to/your/script.php >> /path/to/your_script.log
..otherwise you will have to check the control panel of your hosting account and figure out how to manage
You can create one more column in your table where you will create the expiration date. Then you can on your sql server create the job that will erase all records that have expiration date less than curent date.
CREATE EVENT db_name
ON SCHEDULE EVERY 10 SECOND
DO
DELETE FROM myschema.mytable WHERE expiration_date < NOW()
I hope that will help.

MySQL daily check and update

Is possible to create a routine or procedure that can automatic everyday check a date in a table.
To be simpler : I want to check if the date on a user have permitions to enter a site and when the date pass want to make the user field activated false.
routine daily
check if (todaydate < dateclient) then
client.activated= false
Thanks for all the help.
Yes, this should be a simple UPDATE query. Something like the following:
UPDATE Client
SET Activated = 'false'
WHERE NOW() < dateclient
You would obviously need to modify this for your schema and then schedule it to run daily using cron or an alternative scheduler of your choice.
Creating the routine is simple enough, but to run it on a daily basis you'll need to wrap it in a cronjob (*nix) or Scheduled Task (Windows). The routine, unfortunately, cannot execute itself - and to the best of my knowledge MySQL server does not possess the ability to run routines at scheduled intervals.