remove mysql data, cron vs Mysql Event Scheduler - php

I am using codeigniter framework, and I want to remove some rows when one column have a specific value (e.g read =1).
Is it better to use PHP CRON, or Mysql Event Scheduler?

Related

How to know if SQL table has been updated

I have a table in SQL Server. Its data is about call details of Cisco IP-Phones.
I have a script in PHP.
The data of this table updates whenever a new call occurred.
Once the table updated, I want to fetch new data.
How can I know when the table is updated?
It is not in certain time and depends on when a new call is made.
Fields of table are:
1. id
2. WaitingTime(This is the value that I needed)
3. dateTime (It is the date and the time of updating and inserting the new value)
Technically you could write an ON INSERT trigger that could call a PHP script. However, it's not recommended. There is more information regarding this here: Calling a php file by using mysql trigger
A better approach might be to simply run your PHP script in a cron job to poll the database and process any new calls.

MySQL to refresh memcached

We have a web application running on LAMP stack, the application depends on various services. These services gets data from cache (memcached) which is being refreshed using cron (from MySQL). Cron processes are running every 5 mins.
In this approach we can not serve data which is updated recently as cache is getting refreshed every 5 mins.
Is there any mechanism exists which can trigger cache refresh as soon as data gets updated in MySQL?
I don't know if this is the best solution, but what you can do is create MySQL trigger which gets executed on insert/update/delete.
Inside that MySQL trigger execute a UDF. From that UDF you can execute any PHP script using sys_exec().
Read about Triggers
Read about UDF
Read about using sys_exec() and more
My solution for this problem was to have the MySQL query in a function with the memcached query, for example (in Python, because I don't know PHP, you can look at it an change it to PHP):
def insert(user,value):
#execute on memcached first
key="user:"+user
memcClient.set(key,value)
#then execute it on MySQL
cursor.execute("INSERT INTO tables VALUES (%s,%s)",(user,value))
db.commit()
And you will do the same for delete and update, sorry I couldn't do it in PHP, I'm just a teen coder, good luck.
What about identifying the mysql updation at php level only & refresh the memcache accordingly ?
<?php
if($update_db){ // if we need to update the db then update db & memcacache together !
// Code to Update the database ....
// Code to Reset the memcached keyvalue pair.
}
?>
In the approach suggested by Manu, we are using expensive db trigger which is actually not needed to achieve the cache update.

Event Scheduler to multiple MySQL databases

I am trying to figure out if its possible to have EVENT SCHEDULER take data from one database and update it to another. I checked mysql site and google but there isn't any info on this. I know how to create events and understand the process. I am thinking of using php and mysql but not sure if that would work.
Why I need this:
Database A - gets a lot of traffic
Database B - gets little traffic
I want to run certain queries (counts mostly) to run once a day from Database A. Then store the results inside Database B. This way when some loads a page that has to do a count, it wont run the count query but instead just SELECT * FROM database B. Making it load faster.
I think what you need to do is to replicate your databases!
For example you can specify Database 'A' as your publisher and Database 'B' as your subscriber. There are lot of articles on the internet regarding replicating MySQL databases. Google and find out. Anyway have a look at this question over here MySQL Event Scheduler on a specific time everyday. This might give you an idea on what to achieve.

trigger SOLR update after MySQL update

I'm planning to use SOLR for full-text search. Question: when I update my MySQL-database, is there any way to trigger a SOLR-update after I updated my database, so the data will be exactly the same in both MySQL and SOLR? DataImportHandler? Or UpdateHandler? I'm using PHP, and sometimes about 100 insert/update/delete-queries/minute (not always).
Either you call Solr to update data from PHP when you are updating DB or you can set up the cron job do do so.
They are planning to add scheduler do Solr: http://wiki.apache.org/solr/DataImportHandler#Scheduling
Run a cron job in the background to perform a delta index of the database.

MYSQL moving information with php

I am wondering if it is possible to automate or by button press to move mysql table information from one table to another table deleting it from the first table and putting it in another table? Using php.
My mysql table is big and the page that adds the information to that table has 70 query's on it which slows the page refresh times. I need to move information from the first table to the second at a certain time of day everyday so that those querys don't have to look through all of my giant 27k row table.
Is this possible?
Also if someone could help me with my comment on this page I would be grateful.
link text
PHP doesn't have a constantly running server you can schedule background tasks with.
If you have access to the server you can set up a cron job (or scheduled task under windows) to run the PHP script for you.
Or (and this isnt so nice) you can put the script on the webserver and call it manually at the appropriate time by entering the URL in your browser.
A 27k row table is small by SQL standards, as long as it is properly indexed.
For instance, if you don't care about data from yesterday, you can add an indexed date column and filter with WHERE myDate > NOW() - INTERVAL 1 DAY, and SQL will automatically restrict the query to the rows younger than 24 hours.
I am wondering if it is possible to automate or by button press to move mysql table information from one table to another table deleting it from the first table and putting it in another table? Using php.
You can initiate it from PHP, but what you ask is effectively MySQL's domain.
It can be accomplished in two statements:
Use an INSERT INTO statement to copy the rows from the old table to the new one
Delete the old table
My preference would be that this occurs in a stored procedure for sake of a transaction and ease of execution (in case you want it initiated by CRON/etc) because it would be easier to call one thing vs a couple or more.
27k is not very big table and MySQL should work ok with that. Do you have all the required indexes? Did you used EXPLAIN on your slow queries?
As for the question about moving data from one table to another - create a php script that will be run by CRON and will move rows one by one. What's the problem here?

Categories